3 LED's controlled by visual basic

I have to control 3 LED's on my leonardo using visual basic.
Green
Yellow
Red

In my visual basic program i have a button "Auto" wich sends the message "Auto" to the arduino.
When i press the button the LED's have to light up like in a certain order with a delay (loop).
I've written a code that works. But when i puch the button again it have's to stop. How do i put in in my code? Or is this wrong?

int Rood =  2;
int Oranje = 3;
int Groen = 4;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(Rood,OUTPUT);
pinMode(Groen,OUTPUT);
pinMode(Oranje, OUTPUT);
}

void loop(){
if(Serial.available() > 0)
  {
     String Val = Serial.readStringUntil('\n');
  if (Val == "Auto")
  {
    
    changeLights();
    
  }
  }
  
void changeLights(){
    // green off, yellow on for 3 seconds
    digitalWrite(Groen, LOW);
    digitalWrite(Oranje, HIGH);
    delay(3000);

    // turn off yellow, then turn red on for 5 seconds
    digitalWrite(Oranje, LOW);
    digitalWrite(Rood, HIGH);
    delay(5000);

    // red and yellow on for 2 seconds (red is already on though)
    digitalWrite(Oranje, HIGH);
    delay(2000);

    // turn off red and yellow, then turn on green
    digitalWrite(Oranje, LOW);
    digitalWrite(Rood, LOW);
    digitalWrite(Groen, HIGH);
    delay(3000);
}

But when i puch the button again it have's to stop.

So you want the "Auto" button to act like a toggle? Toggle the sequence on an off?

Yes, When I use the command "auto" again, I want to get out of the loop.

you can put a boolean variable in your arduino code that is true the first time the button is pushed then false the next time the button is pushed. If the variable is true the code turns on the lights if the variable is false it turns the lights off.

maybe something like :

//pseudo code
if(Val =="Auto")
  //toggleFlag=~toggleFlag
       toggleFlag=!toggleFlag;

   if (toggeFlag ==1)
     //turn lights on
   else
     //turn lights off

alexanderalen:
Yes, When I use the command "auto" again, I want to get out of the loop.

How long are you prepared to wait for the program to respond, bearing in mind your use of delay() which introduces a 13 second delay between readings of the Serial input ?

You need to use millis() for non blocking waiting periods

See Using millis() for timing. A beginners guide

the 'toggleflag' coding works when you have LED's whithout delay. I managed to turn them on and of. But i want to keep the loop going. So this problem isn't solved yet?

UPDATE!!

I figgured out how to use to "Auto" button (command coming from visual basic), to toggle my LED's.
I used following code. But how do i loop a code with delay when the counter is odd?

int Red =  2;
int Orange = 3;
int Green = 4;
int pressCnt = 0;  //counter for "Auto" command         
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(Red,OUTPUT);
pinMode(Green,OUTPUT);
pinMode(Orange, OUTPUT);
}

void loop(){
  //code to read the visual basic input
if(Serial.available() > 0)
  {
     String Val = Serial.readStringUntil('\n');
  // command "Auto" wil increase counter
  if (Val == "Auto"){
    pressCnt++;
    //odd will start loop
    if (pressCnt % 2){
  digitalWrite(Red,HIGH);
  delay(200);
}//even stops loop
   if (!(pressCnt % 2)){
    digitalWrite(Red,LOW);
    delay(200);
   }
   

}
  }
  }

But how do i loop a code with delay when the counter is odd?

Not by using the delay() function if you want the program to remain responsive. Use millis() for timing instead.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

The principle used is to to break the actions to be performed into discrete steps and to use millis() to determine when a period, such as an LED being on, is finished and to move onto the next step.

can you give me an example with my code ?

alexanderalen:
can you give me an example with my code ?

Have a look at the BlinkWithoutDelay example

it doesn't work

alexanderalen:
it doesn't work

Post "it" here and describe what's wrong.