Arrays



Arrays are variables that contain lists of values. Arrays allow you to access any one of those values by targeting its position in the list.

Arrays in Arduino, C, Java and many other programming languages begins its index position at 0. So the first element in the list has an index position of 0 and the last item in the array has an index position of the length of the array minus 1.

To create an array of ints:
int arrayName[]={value1, value2, value3...};
To access the second element:
arrayName[1];
To iterate over the list:
for(int i=0;i<arrayLength;i++){
    Serial.println(arrayName[i]);
}
Array values are stored in memory. When you define an array of 10 elements, the next available addresses in memory are reserved for it.The array variable is said to point at the first address in memory reserved for the array. Understanding memory allocation is important. If you referred to the 11th element of an array, you are out of bounds and you cannot be sure of what kind of value will be returned.



String

Strings are a sequence (or string) of characters. String Literals are enclosed in double quotation marks. In C, a string literal is an array of chars. The character may be a letter, a punctuation mark, or a special character (tab, line feed). Each char is associated with a number code that uses a standard called ASCII.
char arrayName[]="Hello";
looks like this in memory:
H(72)
e(101)
l(108)
l(108)
o(111)
\0(0)
Something to keep in mind. Arrays are pointers so you could write the above line as:
char *arrayName="Hello";
char* arrayName[]={"H","e","l","l","o"};
Your arrayName points to the first element in the char array—the memory location that contains the letter H.



Serial

When the computer sends data from the Serial Monitor to the Arduino, it is converted by a chip on the Arduino board. The data is received by a part of the microcontroller called the Universal Asynchronous Receiver/Transmitter (UART). The UART places the received data in a buffer—a special area in memory (128 bytes) that can hold the data that is removed as soon as it is read.

Serial.available() returns the number of bytes of data in the buffer that are waiting to be read. If there is no data, the function returns 0.
#define led ___
char val;

void setup(){
//set  your led pin
_______________
Serial.begin(9600);

}

void loop(){
    
    if(Serial.available()>0){
        val=Serial.read();
       if(val=='H' || val='h'){
           digitalWrite(led,HIGH);
        }else {
        	 digitalWrite(led,LOW); 
       }
    } 
 }


Create a "toy" that lights up a certain color depending on what is typed in the serial monitor.