I'm having trouble getting my LED to keep blinking after a certain input has been given, it will only repeat this once and then just ignore the 'if' statement it seems.
To replicate this problem; Type LED in the serial monitor (this will allow led control) > Next type: 1 to turn the LED on > Next type 3 to start the blinker.
Any idea whats going on? I'm probably missing something obvious here and need to sleep but its driving me nuts :*)
After some experimenting i'm still having no luck....
Also this code will not loop and ignore my if statements... just thought i'd make a simpler version to see where it's going wrong.
int LEDfunction;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.print("Setting up\n\n");
}
void loop()
{
delay(50);
if (Serial.available()>0)
{
LEDfunction = Serial.read();
if(LEDfunction == '0') {
digitalWrite(13, 0);
Serial.print("Led is off\n");
}
if(LEDfunction == '1') {
digitalWrite(13, 1);
Serial.print("LED is on!\n");
}
if(LEDfunction != '1' && LEDfunction != '2') { Serial.print("Are my if statements ignored?\n"); }
delay(500);
Serial.print("end of the loop\n");
}
}
The problem is that you only take action if you receive something. So you receive something and blink. Next you don't receive anything so you don't blink.
You need to separate the receive logic from the action logic.
Wow.. as i said i'm missing something obvious probably haha.
Thanks a lot both of you my code is now working!
Probably should not be doing this stuff when i'm this sleepy...
Thanks again!
EDIT:
I was under the impression by the way that " if (Serial.available() > 0) ", meant that it should check if it's connected via serial. but if i understand correctly it checks for a signal?