This tutorial, we will show how to use Strings with the Arduino. A String is a char array terminated by the null char (0x00, or ). You can define a String as a char array and can use many functions straight on it. The string library define a class, that can also be used for comparing 2 strings,
like this example:String sentence = "Hello World";
String str;char c;
char matrix[20];
int x=0;
void setup(){
Serial.begin(9600);
}
void loop(){
if(Serial.available()){
do{
c=Serial.read();
matrix[x]=c;
Serial.print(matrix[x],DEC);
x++;
delay(1); //some delay
}while(c!='
');
matrix[x-1]='';
Serial.print(matrix);
str=matrix;
if (str==sentence){
Serial.println("OK");
} else {
Serial.println("Error");
}
}
}
On this sample, we defined a temporary array to store the characters before sending it to a string variable (str). Then, we could compare it with our desired sentence (Hello Word) using the = sign. Also, this program outputs the ascii values before sending back the received data:
Source Garagelab