HizonPastor:
Hello I am new here to this programming world
and I really want to learn
can someone explain to me what this programs means
while(Serial.available())
{
//read the input
delay(50);
char c=Serial.read();
readString+=c;
}
if(readString.length()>0){
Serial.println(readString);
if (readString =="ON"){
digitalWrite(led, HIGH);
}
readString="";
}
I am using it to light an LED using Bluetooth and I really want to figure how it works
I will take the help of the following conceptual level diagram (Fig-1) to explain how L (the built-in LED of Arduino UNO) becomes ignited when UNO receives the ON string/message coming from the InputBox of the Serial Monitor.
Figure-1:
1. You enter ON in the InputBox of the Serial Monitor of Fig-1 and then click on the Send button. As a result, two asynchronous (async) frames (one after another) are transmitted towards UNO over the TXD line. An async frame is a 10-bit data frame that contains a STARTBit (LOW), 7-bit ASCII code for a character (1001111 for O), 1-bit padding bit (LOW), and a STOPBit (HIGH). In Fig-2, we have depicted the async frame for the character O.
Figure-2:
2. When a frame arrives at the UNO/MCU, the MCU discards the START and STOP bits and the remaining 8-bit data (01001111 = 0x4F) of the 1st frame is automatically saved in the 1st location (location-0) of the FIFO (first-in first-out) type BUFFer of the UNO/MCU.
3. Because the message/string ON has been sent from the InputBox of the Serial Monitor, the BUFF will contains ASCII codes of two charcaters which are O and N.
4. The Arduino IDE has the following instruction to check if one or more charcaters has/have arrived/stored in the BUFFer of the UNO/MCU.
byte n = Serial.available(); //n = 0 or non-zero
5. Because 2 charcaters have been sent from the Serial Monitor, the value of n in Step-4 would be 2. If the value of n = 2, then we will check that the arrived string is ON; if so, we will ignite L.
6. The task of Step-5 can be done by executing the following codes:
byte n = Serial.available();
if(n == 2)
{
myData[0] = Serial.read(); //bring out O from BEFF and save in a char type array named myData[]
myData[1] = Serial.read(); //bring out N from BUFF
myData[2] = '\0'; //add null character with the array
//---------check if the received string is ON----------------------
if((strcmp(myData, "ON") == 0) //if matching occurs strcmp() returns 0
{
digitalWrite(13, HIGH); //ignite L
memset(myData, 0, 4); //reset the BUFFer
}
}