Trying to keep LED lights blinking

Arduino mega with a 2.4 screen and a sting of leds.. The arduino receives an option from another unit 1-4 and it updates the screen and blinks the lcd to show option picked.. This is all working but the lights only blink twice.. I am using the int LedStat that is set through parseInt to pick which case to go through and also blink leds.. From what I am thinking if LedStat=1 it will run case 1 and longpressblink1 should run until it is changed.. I have tried using a different int set through the case but the results were the same

void loop() {
  Serial1.begin (9600);
  Serial.setTimeout(1000000);
 while (Serial1.available() == 0){
 }
  LedStat = Serial1.parseInt();
    blinks();

    switch (LedStat){
      case 1:
      longPressStart1();
      break;
      
      case 2:
      longPressStart2();
      break;
     
      case 3:
      longPressStart3();
      break;
     
      case 4:
      longPressStart4();
      break;

    }
   
}
void blinks (){
 if (LedStat ==1) LongPressBlink1();
 if (LedStat ==2) LongPressBlink2();
 if (LedStat ==3) LongPressBlink3();
 if (LedStat ==4) LongPressBlink4();
}

// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  
  Serial.println("Button 1");
  tft.fillScreen(BLACK);
  tft.setTextColor(RED, GREY);
  tft.setTextSize(5);
  tft.setCursor(50, 120);
  tft.print("Button 1 longpress");
  fill_solid (leds, NUM_LEDS, CRGB(255, 0, 0));
  FastLED.show();
  delay(500);
  // Now turn the LED off, then pause
  fill_solid (leds, NUM_LEDS, CRGB (0, 0, 0));
  FastLED.show();
  delay(500);
  LongPressBlink1();

  }

void LongPressBlink1 () 


  {
    fill_solid (leds, NUM_LEDS, CRGB(255, 0, 0));
    FastLED.show();
    delay(500);
    // Now turn the LED off, then pause
    fill_solid (leds, NUM_LEDS, CRGB (0, 0, 0));
    FastLED.show();
    delay(500);
   }


Did your debug code confirm your thoughts?

This code is blocking. That is, it will wait at this line until Serial.available() returns a number > 0.

So it can't get to the code that blinks the LED.

A couple of other things...

These should be in setup()

This code is repeated in longPressBlink1 so you don't need it in longPressStart1

  fill_solid (leds, NUM_LEDS, CRGB(255, 0, 0));
  FastLED.show();
  delay(500);
  // Now turn the LED off, then pause
  fill_solid (leds, NUM_LEDS, CRGB (0, 0, 0));
  FastLED.show();
  delay(500);

You should consider using millis() rather than delay() in your blinking routine.

You should consider replacing routine for each button like longPressStart1, longPressStart2, etc. with a single routine that takes the button as a parameter. longPressStart(1).

It confirmed im not sure why it isnt working

Right now, if I change the buttons 1-4 the screen changes and the lights do blink but only twice. The repeat of the code you pointed out was just me counting blinks but you are right and I will remove it. I removed the 0 from the serial1.available line and the screen stopped changing. I have read up on mills but the concept is just hard for me to grasp but I know I need to learn how. Since the int is a global int it should keep its setting on the next loop from what I understand its like thats not happening. Sorry only been playing with code less than a month..

You can't just remove it and expect things to work. Think about what you are trying to do...
loop
{
If there is something to read...
Then read it and put in LedStat.

always blink depending on LedStat.
}

Something like...

void loop()
{

  if (Serial1.available() > 0)
  {
    LedStat = Serial1.parseInt();

    switch (LedStat) {
      case 1:
        longPressStart1();
        break;

      case 2:
        longPressStart2();
        break;

      case 3:
        longPressStart3();
        break;

      case 4:
        longPressStart4();
        break;

    }
  }

  blinks();
}

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