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...};
arrayName[1];
for(int i=0;i<arrayLength;i++){ Serial.println(arrayName[i]); }
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";
H(72) |
e(101) |
l(108) |
l(108) |
o(111) |
\0(0) |
char *arrayName="Hello";
char* arrayName[]={"H","e","l","l","o"};
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.