I'm trying to do serial communication between two Arduinos.
I used this circuit diagram and this code works perfectly;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
switch(Serial.read())
{ case 'A':
digitalWrite(13,HIGH);
break;
case 'B':
digitalWrite(13,LOW);
break;
}
}
}
But I don't want to use switch-case. I want to use if conditions. I'm trying this code;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
if(Serial.read()=='A')
{
digitalWrite(13,HIGH);
}
else if(Serial.read()=='B')
{
digitalWrite(13,LOW);
}
}
}
But it doesn't work. My question is; why is works with switch-case but not with if conditions. Where is my fault? Can you fix it?
Thanks.