How to get out of the while loop?

Good morning, classmate

I have a problem with the following routine:
Writing from the computer keyboard to the serial console must do the following:

Letter A: Turn on three LED.

Letter B: LED 1 turns on for one second and off, LED 2 turns on for one second and off, LED 3 turns on for one second and off and restarts. This routine creates a sense of movement.

Letter C: Three LED off.

Deputy programming shipping.

Escritura_serial_teclado_PC.ino (942 Bytes)

You really need to read this, serial input basics The thread will tell you all you need to know about receiving and using serial data.

Then the "blink without delay" (BWOD) example in the IDE examples (File, Examples, Digital) will show how to do the timing. And notice that with BWOD there is no blocking while loop.

groundfungus:
You really need to read this, serial input basics The thread will tell you all you need to know about receiving and using serial data.

Then the "blink without delay" (BWOD) example in the IDE examples (File, Examples, Digital) will show how to do the timing. And notice that with BWOD there is no blocking while loop.

Could I explain? I'm new and I do not understand programming

Have a look at this approach to solve your problem

const byte Led_1 = 2;
const byte Led_2 = 3;
const byte Led_3 = 4;

enum {
  allOn,
  moveOne,
  moveTwo,
  moveThree,
  allOff
};
byte cState = allOff;

unsigned long loopTop;
unsigned long lastEvent;

void setup() {
  Serial.begin(9600);
  pinMode(Led_1, OUTPUT);
  pinMode(Led_2, OUTPUT);
  pinMode(Led_3, OUTPUT);
  setState(allOff);
}

void loop() {
  loopTop = millis();
  if (loopTop - lastEvent > 1000) { // do something 1 second after last action
    tickState();
  }
  if (Serial.available()) {
    byte leer = Serial.read();
    if (leer == 'a') {
      setState(allOn);
    } else if (leer == 'b') {
      setState(moveOne);
    } else if (leer == 'c') {
      setState(allOff);
    }
  }
}

void setState(byte setTo) {
  Serial.print(loopTop);
  Serial.print(": in ");
  Serial.print(cState);
  Serial.print(" enter ");
  Serial.print(setTo);
  Serial.println("");

  switch (setTo) {
    case allOn:
      digitalWrite(Led_1, HIGH);
      digitalWrite(Led_2, HIGH);
      digitalWrite(Led_3, HIGH);
      break;
    case moveOne:
      digitalWrite(Led_1, HIGH);
      digitalWrite(Led_2, LOW);
      digitalWrite(Led_3, LOW);
      break;
    case moveTwo:
      digitalWrite(Led_1, LOW);
      digitalWrite(Led_2, HIGH);
      digitalWrite(Led_3, LOW);
      break;
    case moveThree:
      digitalWrite(Led_1, LOW);
      digitalWrite(Led_2, LOW);
      digitalWrite(Led_3, HIGH);
      break;
    case allOff:
      digitalWrite(Led_1, LOW);
      digitalWrite(Led_2, LOW);
      digitalWrite(Led_3, LOW);
      break;
    default:
      return;
  }
  cState = setTo;
  lastEvent = loopTop;
}

void tickState() {
  switch (cState) {
    case moveOne:  setState(moveTwo);
      break;
    case moveTwo:  setState(moveThree);
      break;
    case moveThree: setState(moveOne);
      break;
    default:
      return;
  }
  lastEvent = loopTop;
}

Whandall:
Have a look at this approach to solve your problem

const byte Led_1 = 2;

const byte Led_2 = 3;
const byte Led_3 = 4;

enum {
  allOn,
  moveOne,
  moveTwo,
  moveThree,
  allOff
};
byte cState = allOff;

unsigned long loopTop;
unsigned long lastEvent;

void setup() {
  Serial.begin(9600);
  pinMode(Led_1, OUTPUT);
  pinMode(Led_2, OUTPUT);
  pinMode(Led_3, OUTPUT);
  setState(allOff);
}

void loop() {
  loopTop = millis();
  if (loopTop - lastEvent > 1000) { // do something 1 second after last action
    tickState();
  }
  if (Serial.available()) {
    byte leer = Serial.read();
    if (leer == 'a') {
      setState(allOn);
    } else if (leer == 'b') {
      setState(moveOne);
    } else if (leer == 'c') {
      setState(allOff);
    }
  }
}

void setState(byte setTo) {
  Serial.print(loopTop);
  Serial.print(": in ");
  Serial.print(cState);
  Serial.print(" enter ");
  Serial.print(setTo);
  Serial.println("");

switch (setTo) {
    case allOn:
      digitalWrite(Led_1, HIGH);
      digitalWrite(Led_2, HIGH);
      digitalWrite(Led_3, HIGH);
      break;
    case moveOne:
      digitalWrite(Led_1, HIGH);
      digitalWrite(Led_2, LOW);
      digitalWrite(Led_3, LOW);
      break;
    case moveTwo:
      digitalWrite(Led_1, LOW);
      digitalWrite(Led_2, HIGH);
      digitalWrite(Led_3, LOW);
      break;
    case moveThree:
      digitalWrite(Led_1, LOW);
      digitalWrite(Led_2, LOW);
      digitalWrite(Led_3, HIGH);
      break;
    case allOff:
      digitalWrite(Led_1, LOW);
      digitalWrite(Led_2, LOW);
      digitalWrite(Led_3, LOW);
      break;
    default:
      return;
  }
  cState = setTo;
  lastEvent = loopTop;
}

void tickState() {
  switch (cState) {
    case moveOne:  setState(moveTwo);
      break;
    case moveTwo:  setState(moveThree);
      break;
    case moveThree: setState(moveOne);
      break;
    default:
      return;
  }
  lastEvent = loopTop;
}

thanks for your help THIS was what I wanted without my knowledge but still get back there and I really would like you to tell me what You did understand paragraph .

It is very easy to handle the serial input of single letter commands without parameters,
simply by ignoring everything else and changing the state of the LED animation.

If you look at the different states your animation has, there are 5 distinct ones.

You wanted to have automatic switching between states in 1 second intervals for three of them.

setState() switches the hardware, tickState() moves through the three round robin,
both record the time of the last event only if some action was taken.

DiegoMillan:
Could I explain? I'm new and I do not understand programming

Maybe it would be a good idea to start with some of the simple examples that come with the Arduino IDE.

If there is a specific piece of Serial Input Basics that you don't understand please tell me and I will try to help.

From looking at your code it is probably more important to get rid of the delay()s and use millis() to manage timing as illsutrated in Several Things at a Time. The Arduino can do nothing during a delay().

...R

This is crossposted in the Spanish section.

Pete

Pretend you are your sketch.

What are you doing right now? Well, you are either doing nothing, waiting for a keypress, or you are flashig the LEDS, waiting for a kepress. If you are flashinf the leds, then most of the time you are watching the clock, waiting for the moment when one second has expired since last time you turned one of the LEDs on.

If there is a kepress, what do you do NOW?
If you are flashing the LEDs, and the one second has expired, what do you do NOW?