array operations in arduino programming. || basics of array in array operations.
We write the different codes for different projects, we interface with different sensors and different actuators with our arduino board, but there are few things to uh include here in this tutorial series: they are the basic programming tools or basic operations that are needed for any project. If you are, if you want to read the reading of a sensor and store it into any variable, for example, say array or if you want to store them permanently, you can store it in an sd card module or if you want to read the user input Or to analyze the user input different messages, you can use the string operations etc. Keeping this thing in mind. I am im going to explain some array operations in arduino, so this is an ad, so todays video is based on the array operations. So array is basically a set of data collections, a set of data collections, which stores the different elements in just one array. If you want to store the different elements or different values to one variable, you can use an array. The syntax for declaring an array is first thing: is the data type? Is this an integer type of variable, floating type of variable, etc? And second thing is array name, this arr im indicating this arr. As my array, this is the array name here, which is a variable and in square brackets, you have to mention the size of array. There are few methods to declare an array you can ignore this size.
If you have elements here and you can ignore the elements if you want so second thing is you have to include the elements in the curly braces and elements in storing the elements is different for different type of elements. For example, if you are storing the character array, then you have to include them in the single course. If you are storing an integer, then you have to include them in double quotes and if you are storing any floating type of variable or integer type of variable, then you have to skip that coded commas. So this is a simple definition for an array syntax for declaring an array. The second thing is, for example, you have an array im, creating here an array array with elements im having an array arr array, its. I leave total number of element or size of this array is 10 and the elements are stored from in reverse size from 1 to 10, so 10, 9 8 up to in reverse order. These elements are arranged here first row here that you are seeing from 10 to 1 is the list of elements that are stored in this array. Element array variable, and the second thing that you are having here. Our second row is, is the index of that particular element? Index means location of that element in an array, and these indexes are indices are obviously whole numbers. Whole numbers. That means they are always starting from zero and their end depends upon the number of elements in an array, for example, in this array, in this particular array, which has the 10 element in it, and there are 10 different index numbers for different elements, starting from 0 Up to 9.
, i am trying to say that these index are the different position: values for different elements. For example, my first element is 10 and its index number r or its position in this particular array is zero. So if you want to get this array, if you want to get this element on the serial monitor or as your output, so you have to call like this array in the square bracket and then in the square brackets when youre calling an array you have to Give the index number of that particular array so, for example, im not interested in printing or getting the first element of my array, so first element is having the index 0. So i have to give the index size 0 and remember that first, the time of declaring of this variable, you are entering the size of element, but when you are calling this array or when you are using that same array further in your program, the thing that You are entering in the size in in place of size is actually the index number of that array. That index number is holding an element so, for example, im interested here to get my first element to get the first element of this array. So i have to enter 0 as my index number, and this will give me the output as 10, because 10 is the first element of my array. If you want to call if you want to get the last number of your array, you have to give the index number of 9.
just type index 9 and it will return. Result 1. lets open our arduino ide and write the code for this array. I hope you have understood that understand the basic logic behind this array operations. So here is our empathy arduino workspace, so i have to declare an array first, which is an integer type of array, um im setting the size of this array im entering just five elements here, so the length of my array will be five equals two im entering The elements in the reverse order, so my first element will be five, then second will be four and three then two and last element will be one, and you know that you have to terminate this command using these semicolons then lets call one. Another lets declare one another variable to get different elements of my array, so int l e e element, so the void setup. First, we have to start the serial communication and to receive the data on the to print the data data on the serial monitor and to receive the data from the user. So for that you have to start the wide loop right setup. You have to write the void, setup function and inside void setup function. You have to write serial.begin command to start the serial communication, so just write serial dad begin command and set a bad rate im setting it 900 to 600, because this is the normal battery and people mostly use this battery.
Second thing is the white loop wide loop im keeping this white loop empty because twilight, loop will repeat itself again and again and well print the same result again and again on the serial monitor. I do not want im not interested to print the same result again and again. I just want to print the thing only once so im interested here to get the second element of my array, which is a array, so i have to write dle element element equal to a r index of my second element is 0, and one im interested in Printing four, on my serial monitor and for a second element of this array and its index number is zero and one. So i have to write index one here now. I just want to put this result on serial monitor, so you have to write period, dot, print, im, printing, the value of this element here, because the element that i have fetched from my array array variable is stored inside this element. So when im printing this element here, this will print the required result or the fourth element or the element that is placed on this index number so lets just compile this code im using the protein software for this circuit simulation at this explanation. Click on ok and start the simulation here you can see that 4 is printed on my virtual terminal or you can see the serial monitor and four is the fourth element of my second element of my array now lets change the index number from zero to four.
So our so now im now we we must get the fourth fifth element of this array. The fifth element is first, second, third, fourth and fifth, so we so by changing this array from one to four, we must get one on our serial, monitor so compile this code again and open protis software to verify the result here you can see here. You can see we are getting one on our cl monitor and this. This is the accurate result if, for example, if you do not know how many elements are there in this variable or in this array – and you want to know the size of the elements in this array – a total number of elements in in this array, so you Can do this, you can do a thing like declare. A variable table im naming here it as size. Size is equal to size of and enter the name of array inside this function, but array im having here is a r, so you do not need to enter the index number or the square brackets here, just type a r here, and this will this will return. The result in number of bytes, for example, integer type of data, is having a size of two bytes, so to get so, this will print, and this will return 10, because our array element is having total of 5 element and each element is having a size size Of 2 bytes, so 5 multiplied by 2 is equal to 10.
So this size of element, this size of function will return. The the size of this array in bytes is 10. To get the number of element, you have to divide output of the size function with the size of your data type. So for now im having integer type of data, i have to divide the this result with size up size of end, so this equation. Now this simple equation must return a value of 5 as our output, so lets write a command lets print. This value of the size variable on our serial, monitor and check for the result, so type serial dot print serial.println size out the output of this result. The result of this equation is stored in this variable size, variable and im printing. The value of size variable on serial, monitor just type here, ln, so that our result will be get printed in different lines. Now, after changing these things, compile your code and open the protest software to check again for results here, you can see that first thing that we are getting on the serial monitor is one which is the fourth element that we have printed in the previous and just A minute ago – and the second thing is the total number of element which is the result of the equation – that we use it to get the size or the number of elements in my array by calculating the size of the array and then dividing with the size Of my data type so lets lets create a loop here to print elements in separate lines, so lets use a for loop here for that we need to define one another variable i, and for i equal to 0, which will indicate our first element.
I is less than uh, because if we write i is less than 5 and i plus plus we are not including 5 here, because if we are including the five here, we are indirectly calling the sixth element, which is not a sixth uh element of our array And which is not in this array, our array is of five element having a size of five elements. Total five elements in it fifth element is not here just now: serial print, serial dot and ln, a r r, then uh. Now we have used the loop here. So i is changing its value again and again. For the first time, its value is zero when it, when this loop repeats itself, then when this for loop repeats itself again, the this value will change will be get incremented to one, and the second element will be printed here then. Third, then, fourth, and then fifth, so for that we have to start enter here, we have to enter i as the index value here it has index value here. When i is 0, it will print 5 l as our output. This will call the first element and when the value of i is 1, this will return.