Hi,
I'm new to Arduino, just started couple days ago and trying to find my way through so thanks in advance for any help and sorry if this question was asked before, I googled a lot a couldn't find an answer.
So I'm following one video tutorial, where the guy is switching led on and off using Swith case.
the code looks like this:
"int led = 7;
char ch;
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
ch = Serial.read();
{
switch (ch)
{
case 'a':
digitalWrite(led, HIGH);
Serial.println("LED is on");
break;
case 'b':
digitalWrite(led, LOW);
Serial.println("LED is off");
break;
default:
digitalWrite(led, LOW);
Serial.println("Invalid input by user");
break;
}
}
}
}
"
The idea is that when I type "a" the led should turn on, when I type "b" it should turn off, and when I type anything else it should stay off and display the message.
It works fine in video tutorial, but the problem I have is that when I type "a", the led turns on for a fraction of a second, then it turns off and displays the message "Invalid input by user" two times, like this:
"16:42:33.450 -> LED is on
16:42:33.450 -> Invalid input by user
16:42:33.483 -> Invalid input by user".
When I type anything else, it just displays the error message three times, like this:
"16:51:59.016 -> Invalid input by user
16:51:59.016 -> Invalid input by user
16:51:59.049 -> Invalid input by user".
With my very limited knowledge, it seems like the "break" is not doing its job, and the code keeps looping. Although I couldn't find any solution to fix this.
Please help!!!
Thank you very much in advance!!!