turn RGB blinker via serial

have a BLUno bluetooth arduino based on uno.

the logic kind of works, I am connecting ok to the phone, getting and sending messages via blue tooth

but with the serial data, Im having problems using it, and with the logic.

I want to have the option to turn ON/OFF some RGB LEDs, the problem is that the state is sometimes erratic.

here the code

int redLED = 2;
int greenLED = 4;
int blueLED = 3;

static bool redRunning;
static bool greenRunning;
static bool blueRunning;
static bool freq = 34;


void setup() {
  Serial.begin(115200);               //initial the Serial
  Serial.write("started");

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(blueLED, OUTPUT);

  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, HIGH);
  digitalWrite(blueLED, HIGH);

  redRunning = false;
  greenRunning = false;
  blueRunning = false;
  freq = false;
}


void loop()
{

  if (Serial.available() > 0)
  f{
String s = Serial.readStringUntil('\n');

//    String s = Serial.readStringUntil("#");   // Until CR (Carriage Return)
//    s.replace("#", "");
    Serial.println("s:" + s);


if (s == "red_ON")
{
  redRunning = true;
}

if (s == "red_OFF")
{
  redRunning = false;

}  if (s == "green_ON")
{
  greenRunning = true;

}  if (s == "green_OFF")
{
  greenRunning = false;

}  if (s == "blue_ON")
{
  blueRunning = true;

}  if (s == "blue_OFF")
{
  blueRunning = false;

}

 }

 //



  if (redRunning) {
//    Serial.println("redRunning");

digitalWrite(redLED, LOW);
delay(125);
digitalWrite(redLED, HIGH);
delay(125);
  }

  if (greenRunning) {
//    Serial.println("greenRunning");

digitalWrite(greenLED, LOW);
delay(125);
digitalWrite(greenLED, HIGH);
delay(125);
  }

  if (blueRunning) {
    //    Serial.println("blueRunning");

    digitalWrite(blueLED, LOW);
    delay(125);
      digitalWrite(blueLED, HIGH);
    delay(125);
  }


  if (!redRunning && !greenRunning && !blueRunning) {
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, HIGH);
    digitalWrite(blueLED, HIGH);
  }

   }

So what is the proper way to handle state? so the LED stay blinking? but also can turn off all LEDs also if I can turn LEDs combined, at the moment, I'm turning on one, then the other,

All these problems come from not understanding how to handle state internally, and manage changes that come via serial.

Thanks!

why don't you replace a terminating linefeed, '\n', with a NUL, 0, like you did when using '#'

not sure how you want to handle state. the delays may be affecting reading, allowing the buffer to overflow because you're not reading from it.

instead of having individual variables for each LED, you could use a single variable with individual bits representing the which LEDs you want active as well as the state of the LED if not a separate variable

a single piece of code could handle controlling the state of each LED and explicitly comparing timestamps to determine when to do something without using delay. see example below using millis() and toggling the state of an LED

// toggle pin every half period
void
pulse (
    unsigned long period,
    byte          pin )
{
    static unsigned long usecLst = 0;
           unsigned long usec    = micros ();

    if (usec - usecLst > (period / 2))  {
        usecLst = usec;
        digitalWrite (pin, ! digitalRead (pin));
    }
}