Trouble with blinking LED

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 :*)

int index;
char bufferArray[5];
String menuChoice = "X";
char LEDfunction;
int LEDstate;
int blinkState = 500;
char blinkSwitch;


void setup() 
  {
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(13, OUTPUT);
    Serial.begin(9600);  
    Serial.print("Setting up");
  }

void byteConstructor(byte inByte)
  {
    Serial.print("Building Array string");
    delay(100);
    if(menuChoice != "LED") 
    {
      switch (inByte)
      {
        case '\n':
        bufferArray[index] = 0;        
        menuChoice = bufferArray;          
        index = 0;
        break;

        default:
        if(index < 4)
          bufferArray[index++] = inByte;
        break;
      } 
    }    
  }
void loop() 
  {
    delay(500);
    Serial.println(blinkSwitch);
    delay(500);
    Serial.println(menuChoice);
    if (Serial.available()>0)
    {
      if(menuChoice == "X") byteConstructor(Serial.read());
      if(menuChoice == "LED")
      {
          LEDfunction = Serial.read();
          if(LEDfunction == '0')
            LEDstate = 0;
          if(LEDfunction == '1')
            LEDstate = 1;
          if(LEDfunction == '2')
            blinkSwitch = '0';
          if(LEDfunction == '3')
            blinkSwitch = '1';
      }

  if(blinkSwitch == '0')
    {
    digitalWrite(LED_BUILTIN, LEDstate);
    }  

  if(blinkSwitch == '1')
    {
      Serial.print("blinking \n");
      delay(blinkState);
      digitalWrite(LED_BUILTIN, 0);
      delay(blinkState);   
      digitalWrite(LED_BUILTIN, LEDstate);
    }

  if(blinkSwitch != '0' && blinkSwitch != '1') 
    {
      digitalWrite(LED_BUILTIN, LEDstate);
    }   
  }
}

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");
  }
}

Welcome

I've tried your code (the simplified version) and it "works" as expected, it's probably not behaving as you want.

Your Serial.print("end of the loop\n"); is within if (Serial.available()>0)

You may have set your Serial terminal to send newline characters so you need to add code to ignore those characters

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.

As per my previous reply, below based on your code in post #2.

void loop()
{
  delay(50);

  // handle serial input
  if (Serial.available() > 0)
  {
    LEDfunction = Serial.read();
  }

  // handle selected mode
  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");
}

Or something like this : SerialLED.ino - Wokwi Arduino and ESP32 Simulator

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?

It tells you if bytes are available for read.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.