Switching between loops/while statements from serial monitor

Hello guys, im new to this arduino thing and im really into it, so please bear with me

Im having a trouble figuring out how to do multiple infinite loops/while statements and being able to

switch between them using Serial Monitor

Like controlling 3 blinking leds, choosing which led should keep blinking while others are off

Lets say i have 3 leds named 1, 2 and 3

i want to type lets say 1 in serial monitor to make Led 1 start blinking infinitely while LED 2 and 1 are off

and while the led 1 is blinking i want to type 2 in serial monitor to stop led 1 from blinking and start led 2

to blink and so on, entering a number/word/letter should stop the current blinking led and start other led

blinking with the delays and the duty cycle i entered

I've been trying to figure this out for 12 straight hours :frowning:

tried alot of ways and codes, searched everywhere, but still no luck

i've had many problems like all the leds starts blinking when i enter any led number in serial monitor or

the correct one starts blinking but can't switch to other led, and sometimes switching works but without

loop/blinking, they just blink once when i enter their number

I hope this thing is actually possible because after all the attempts i've done i started to think like "is this even possible with arduino" :confused:

Three separate state machines, each using the blink without delay model.

I've been trying to figure this out for 12 straight hours

And yet, we can't see a single one of your attempts.

while the led 1 is blinking i want to type 2 in serial monitor to stop led 1 from blinking and start led 3

There's some logic to typing '2' and having LED 3 blink, but I don't see it.

PS Yes, it is completely possible

There's some logic to typing '2' and having LED 3 blink, but I don't see it.

[/quote]

Oh my bad

Annd

I haven't saved my attempts I know I shouldve =/, but i can redo some of them and post them here tomorrow if necessary

Ok, I'll come back tomorrow

Have a look at the demo Several Things at a Time.

...R

HOLY.. it worked!!!

void setup() {
Serial.begin(9600);


}

void loop() {

 if(Serial.available()> 0 ){
  char state = Serial.read();
  if (state == '1'){
    while(state == '1'){
      char state = Serial.read();
      Serial.println("ONE");
      delay(500);
      state;
      if(state == '2'){
        state;
        break;
    }
    }
  }

  if (state == '2'){
    while(state == '2'){
      char state = Serial.read();
      Serial.println("TWO");
      delay(500);
      state;
      if(state == '1'){
      state;
       break;
    }
    }
  }
 }
}

I can finaly switch between infinite "While" Loops from Serial monitor, but came up with some weird problems

when uploaded, typing '1' starts the first "While" loop which starts to keep printing "One" but i have to type 2

twice to switch to the second "While" loop, The first '2' stops the first loop and typing '2' again starts the

second "while" loop,

second problem is if i type 2222, it reads it as if i typed 2 4 times, i only want it to read '1' or '2'

Last code was for testing and was working almost as intendend , but when i inserted the functions i want in that code it stopped looping, it just does each "while" statement once =/

Will post the main code im trying to use after im done with some more attempts

adam95:
Will post the main code im trying to use after im done with some more attempts

Please post your program using the code button </> so it looks like this. See How to use the Forum. It makes it much easier for people to help you

…R

Robin2:
Please post your program using the code button

Done

Here some of the codes im trying to run

Using the same led but with different delays

void setup() {
 Serial.begin(9600);
  pinMode(13, OUTPUT);

}

void loop() {
   if(Serial.available()> 0 ){
    char state = Serial.read();
    if (state ==  '1'){
      Serial.println("LED #1 STARTS");
      while(state == '1'){
        char state = Serial.read();
        digitalWrite(13, HIGH);
        delay(100);
        digitalWrite(13, LOW);
        delay(100);
        state;
        if(state == '2' && '3'){
          state;
          return;
        }
      }
      
    }

    if (state == '2'){
      Serial.println("LED #2 STARTS");
      while(state == '2'){
        char state = Serial.read();
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500);
        state;
        if (state == '1' && '3'){
          state;
          return;
        }
      }
    }

    if (state == '3'){
      Serial.println("LED #3 STARTS");
      while(state == '3'){
        char state = Serial.read();
        digitalWrite(13, HIGH);
        delay(1000);
        digitalWrite(13, LOW);
        delay(1000);
        state;
        if (state == '1' && '2'){
          state;
          break;
        }
      }
    }
   }
}

Can switch between LED #1 And LED 2#, But once in LED 1# or 2# i cannot break out and switch to LED #3

im kind of sure the problem is the "&&"

 if (state == '1' && '2'){
          state;
          break;

i tried using or "||" instead "&&" and the switching works but they only loop once in each "while" statement

Edit: in (state == '1' && '2') it only reads the first letter which is 1, am i missing commas or something?

Edit: Got it working, Had to use or "||" like this (state == '1' || state == '2')

But i have to type each number twice to switch, the first input exits/breaks the loop and the seconds one chooses the desired loop

is there a way to break and store the input(that used to break the loop) and reuse it again to enter the desired loop automatically?

And is there a way to make it ignore the input if lets i typed 222 because it reads it as 3 separated "2"inputs

state; ?

Also, state cannot be simultaneously '1' && '2'.

This is not the correct syntax

if (state == '1' && '2'){

it should be

if (state == '1' && state == '2'){

And, as someone else said, that is an illogical test.

...R

Robin2:
This is not the correct syntax

if (state == '1' && '2'){

it should be

if (state == '1' && state == '2'){

And, as someone else said, that is an illogical test.

...R

Thank you, I'm good with that part now

Every time you write char state = you are creating a NEW variable. That variable is only valid inside the {} block where you created it. So changing one to '2' doesn't change any others.

Solution: put char state; once, outside any function, which makes it global. Assign a value to it by just writing state =

Every place you have state; nothing happens. Remove those lines.

You are going to have to remove all the delays, but that step can be done later.