Repeating switch ... case

Hello,
i need help with this code
In this code, I need case 'a' and case 'b' to repeat

int ledPin = 13;
int ledPin2 = 12;
void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
    digitalWrite(ledPin, LOW); 
    digitalWrite(ledPin2, LOW); 
}
void loop() {
    if (Serial.available() > 0) {
    int Read = Serial.read();
    switch (Read) {
      case 'a':
        digitalWrite(ledPin, HIGH); 
        delay(1000);
        digitalWrite(ledPin, LOW); 
        delay(1000);
        break;
      case 'b':
        digitalWrite(ledPin2, HIGH); 
        delay(1000);
        digitalWrite(ledPin2, LOW); 
        delay(1000);
        break;
  }
}

In this code, I need case 'a' and case 'b' to repeat

Take the switch/case out of the dependent code for the Serial.available()

Hi dj_nejk,

I'm not sure if I understand right what you want to achieve. Shall the LED blink all the time?

What your code actually does is
whenever a character was received the condition

if (Serial.available() > 0)

becomes true and the code below this condition will be executed.
So whenever an "a" is received LED_pin is switched on for one second then switched off and your code waits another second until the code-execution goes on

whenever a "b" is received LED_pin2 is switched on for one second then switched off and your code waits another second until the code-execution goes on

This seems to be not what you want to achive. But what exactly do you want to achieve?
Can you describe what should happen instead?

best regards Stefan

Make 'Read' a static variable so it retains its value between calls to loop(). Change 'Read' only when a character is available. Select "No line ending" in Serial Monitor so a line ending character doesn't overwrite the last character sent.

const int ledPin = 13;
const int ledPin2 = 12;


void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin2, LOW);
}


void loop()
{
  static char Read = 0;
  
  if (Serial.available())
  {
    Read = Serial.read();
  }
  
  switch (Read)
  {
    case 'a':
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      break;
      
    case 'b':
      digitalWrite(ledPin2, HIGH);
      delay(1000);
      digitalWrite(ledPin2, LOW);
      delay(1000);
      break;
  }
}

I will give another example that I need to have it on.
I have a switch section and I need that when I send "r" to start flashing red all the time, then when I send "g" it starts flashing green, but all the time, it doesn't stop

  switch (Read){
    case 'r':
      fill_solid(leds_motor4, 14, CRGB::Red);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      fill_solid(leds_motor4, 14, CRGB::Black);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      break;
      
    case 'g':
      fill_solid(leds_motor4, 14, CRGB::Green);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      fill_solid(leds_motor4, 14, CRGB::Black);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      break;
    case 'b':
      fill_solid(leds_motor4, 14, CRGB::Blue);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      fill_solid(leds_motor4, 14, CRGB::Black);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      break;
  }

dj_nejk:
I will give another example that I need to have it on.
I have a switch section and I need that when I send “r” to start flashing red all the time, then when I send “g” it starts flashing green, but all the time, it doesn’t stop

The sketch in Reply #3 will repeat ‘r’, ‘g’, or ‘b’ just as easily as it repeats ‘a’ or ‘b’. Just replace the contents of the ‘switch’ statement.

I have it that way, but when, for example, "r" is gone, the LED strip only lights up once, I need it to keep repeating until I send something else

void loop() {
  
  static char Read = 0;
  
  if (Serial.available())
  {
    Read = Serial.read();
  }
  
  switch (Read)
  {
    case 'r':
      fill_solid(leds_motor4, 14, CRGB::Red);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      fill_solid(leds_motor4, 14, CRGB::Black);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      break;
      
    case 'g':
      fill_solid(leds_motor4, 14, CRGB::Green);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      fill_solid(leds_motor4, 14, CRGB::Black);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      break;
    case 'b':
      fill_solid(leds_motor4, 14, CRGB::Blue);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      fill_solid(leds_motor4, 14, CRGB::Black);
      FastLED.setBrightness(255);
      FastLED.show();
      delay(1000);
      break;
  }
}

Did you remember this from Reply #3?

Select "No line ending" in Serial Monitor so a line ending character doesn't overwrite the last character sent.

If you don't have control over the line endings:

  if (Serial.available())
  {
    Read = Serial.read();
  }

should be changed to:

  if (Serial.available())
  {
    char c = Serial.read();
    // Only act on the characters we want.  Ignore everything else.
    if (c == 'r' || c == 'g' || c == 'b')
      Read = c;
  }

Oh yes! It works, thank you very much