0% found this document useful (0 votes)
24 views

Functions and Arrays in Arduino

Uploaded by

Altan Blu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Functions and Arrays in Arduino

Uploaded by

Altan Blu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Functions

1.1 Functions
4.1.1 A reusable block of code; Performs a specific task
4.1.2 Makes code organized, readable, and easier to maintain. Helps break down code into smaller,
manageable pieces.

1.2 Syntax

return_type function_name(input parameters)


{
// Code to perform the task
return result; // optional. Return a value if the function has a return type
}
• return_type: The data type of the value that the function returns. Use void if the function doesn't
return anything.
• function_name: A unique name for your function.
• Input parameters: Input values that the function needs to perform its task.

1.3 Examples:

unsigned int temp;

unsigned int adc_to_millivolts(byte pin_no)


{
unsigned int adc, mv;

//read from ADC and then get average

adc=0;

for(byte i=0;i<10;i++)
{
adc += analogRead(pin_no);
}
adc /= 10;

//compute millivolts
mv = adc * 5000 / 1023;

// return the computed value


return mv;
}

void setup()
{
Serial.begin(9600);
}

void loop()
{
temp = adc_to_millivolts(A0);
Serial.print(temp);
Serial.print("\t");
temp = adc_to_millivolts(A1);
Serial.print(temp);
Serial.print("\t");
temp = adc_to_millivolts(A2);
Serial.print(temp);
Serial.print("\r\n");
delay(1000);
}

2. Arrays

2.1 Definition
2.1.1 An array is a collection of elements of the same data type,
2.1.2 Identified by a unique index or position.
2.1.3 Makes it easier to manage and manipulate large datasets

2.1 Array Dimensions


2.2.1 Arrays may be one dimensional, 2 dimensional or multi-dimensional
• One-Dimensional Array: A linear collection of elements, like a list or a series of data points.
• Two-Dimensional Array: A grid or matrix of elements with rows and columns, suitable for storing data in
tables or grids.
• Multidimensional Array: Arrays within arrays, which can be thought of as matrices of higher dimensions.

2.2 Declaring and initializing arrays

2.2.1 Declaration:
datatype array_name[size];
datatype array_name[x-size] [y-size] ;
datatype array_name[x-size] [y-size] [z-size];

examples:
byte PINS_RELAYS[4];
unsigned int adc_values[3];
char names[10][17];
char names[6][10][17];

2.2.2 Declaration Initializing arrays

byte PINS_RELAYS[4] = {2,3,11,12}; //initializer


bool led_default_values[5] = {HIGH,LOW,HIGH,LOW,HIGH};
float temperature_levels[3] = {18.5, 25.0, 28.9};
char name[5] = {‘J’,’u’, ’a’, ’n’};
char name[5] = “Juan”;

char phone_numbers[4][11] = {
“09160000001”,
“09160000002”,
“09160000003”,
“09160000004”
}

2.2.3 Accessing elements

PINS_RELAYS[0] = 2;
PINS_RELAYS[2] = 11;

temperature_levels[2] = 25.56;

name[2] = ‘n’;
name[3] = ‘e’;

phone_number[1][3] = ‘9’;

name[2][4][0] = ‘J’;
name[2][4][1] = ‘O’;
name[2][4][2] = ‘H’;
name[2][4][3] = ‘N’;
name[2][4][4] = 0;

Serial.println(name[2][4]);

salengGSM.sendSMS(phone_number[2],"Hello User");

char num[3][11] =
{
"09161111111",
"09162222222",
"09163333333",
};

char msg[3][50] =
{
"hello first person",
"hello second person",
"hello last person",
};

void send_all_sms()
{
for(byte i=0;i<3;i++)
{
salengGSM.sendSMS(num[i],msg[i]);
delay(10000);
}
}

You might also like