I am having trouble with the lines being printed in the serial monitor. My intention is for the line stating "You're wrong" to only be printed when the user enters any key besides 1 or 0 in the serial monitor. Instead, no matter what character I enter in the serial monitor the "You're wrong" line is always printing even if it is a 1 or 0. For example, when I enter 1 the line "LED is ON" prints and a split second later the "You're wrong" line prints as well. As a side note the LED's are all working and lighting up properly given my input. Also, if it helps or matters, I have an Arduino Mega 2560. I appreciate any help or guidance you all may have for me. My code is listed below:
int switchvariable=1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(10,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()==0){
if (switchvariable==1){
digitalWrite(13,HIGH);
digitalWrite(10,LOW);
delay(1000);
digitalWrite(13,LOW);
digitalWrite(10,HIGH);
delay(1000);
}
}
{
char com346 = Serial.read();{
if (com346=='1'){
switchvariable=1;
Serial.println("LED is ON");
}
else if (com346=='0'){
switchvariable=0;
digitalWrite(10,LOW);
Serial.println("LED is OFF");
}
else{
Serial.println("You're wrong");
}
Serial.println("");
}
}
}
