Hi,
I am working on the Voice controlled app of the book titled Arduino step by step projects. The code is
int relay = 11; // pin Digital 11
int state; // saves the state
int flag=0; // makes sure that the serial only prints once
the state
void setup() {
// sets the Relay as output:
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is ‘0’ the relay will turn OFF
if (state == ‘0’) {
digitalWrite(relay, HIGH);
if(flag == 0){
Serial.println(“Relay Off!”);
flag=1;
}
}
// if the state is ‘1’ the relay will turn ON
else if (state == ‘1’) {
digitalWrite(relay, LOW);
if(flag == 0){
Serial.println(“Relay On!”);
flag=1;
}
}
//Uncomment For debugging purpose
//Serial.println(state);
}
As far as I understand, the smarthphone sends via bluetooth “48” and “49” to say “off” and “on” but there is no clue about that in the code above.Only checks state whether it is 1 or 0 and this doesn’t make sense for me. I appreciate clarification.
Thanks .
Luix
Hi.
The app sends ASCII values, that then the Arduino converts automatically to decimals.
In ASCII, 48 refers to 0, and 49 to 1.
https://theasciicode.com.ar/ascii-printable-characters/number-one-ascii-code-49.html
Regards,
Sara