Arduino jump out of do-while loop

Hi!

When i hit key "L" on the pc keyboard, do-while loop start, my question is how to get out of the loop?

else if(poz == 'L')
 {      
  
   do 
   {
     digitalWrite(led1, HIGH);
     delay(500);
     digitalWrite(led1, LOW);
     delay(500);        
   }while(poz == 'L');
 }

I made one solution but it isn't good enough.. pls help! ty

My bad solution:

 else if(pozicija == 'L')
 {      
   int gasenje2;
   do 
   {
    while (Serial.available() == pozicija);
    gasenje2=Serial.read();
    if(gasenje2 == 'U')
       {
          pozicija='E';
       }    
     digitalWrite(zmigac_desni, HIGH);
     delay(500);
     digitalWrite(zmigac_desni, LOW);
     delay(500);        
   }while(pozicija == 'L');
 }

Only with key "U" i can stop the do-while loop..

my question is how to get out of the loop?

Do something inside the body of the do/while loop that causes the conditional to no longer be true. Or, use the break statement.

else if(poz == 'L')
 {      
  
   do 
   {
     digitalWrite(led1, HIGH);
     delay(500);
     digitalWrite(led1, LOW);
     delay(500); 
     poz = Serial.read();
   }while(poz == 'L');
 }
 else if(pozicija == 'L')
 {      
   int gasenje2;
   do 
   {
    while (Serial.available() == pozicija);
    gasenje2=Serial.read();
    if(gasenje2 == 'U')
       {
          pozicija='E';
       }    
     digitalWrite(zmigac_desni, HIGH);
     delay(500);
     digitalWrite(zmigac_desni, LOW);
     delay(500);        
   }while(pozicija == 'L');
 }

i wrote this, Is there a better way to reach the void loop?

niky518:

    while (Serial.available() == pozicija);

The foreign variable naming is throwing me a bit, but it looks as if you're comparing the return value from available() against a character variable. Are you trying to spin until available() is non-zero, or what?

Serial.available() returns how many characters are waiting in Serial, not what the next is.

break;

The break command drops you out of the current loop or switch block.

Read my while loop tutorial here: http://hacking.majenko.co.uk/the-while-loop

PaulS:
Do something inside the body of the do/while loop that causes the conditional to no longer be true. Or, use the break statement.

As usual, PaulS has the correct answer.

"pozicija" apparently == "position".

I'm not sure what you are doing here.

Hi,

Maybe you want something along these lines...

This code would loop until another character was received down the serial port:

else if(pozicija == 'L')
{      
  do 
  {
     digitalWrite(zmigac_desni, HIGH);
     delay(500);
     digitalWrite(zmigac_desni, LOW);
     delay(500);        
   } while(Serial.available() == 0); // Loop until serial data received. 
   Serial.read(); // Read & discard the character that got us out of the loop.
 }

This code should loop until the 'E' character is hit.

else if(pozicija == 'L')
{      
  do 
  {
     digitalWrite(zmigac_desni, HIGH);
     delay(500);
     digitalWrite(zmigac_desni, LOW);
     delay(500);        
   } while(Serial.read() != 'E'); // Loop until E is hit. (Serial.read() returns -1 if no character available).
 }

Thank you all for your answers.

To clarify what I want to achieve. I made a car robot. When i press key 'W' carbot go forward, 'S' go back, 'A' go left, 'D' go right and so on... Then i made left and right turn indicators. When i press key 'K' left turn indicator start blinking, and when i press key 'L' right turn indicator start blinking. My problem is when i press key 'K' or 'L' i only can go out from do-while loop with key 'U'. So i cant move forward with my car bot if the turn indicators are working, i wont to normaly drive my car bot while turn indicators are working...I hope I explained it now, and again sorry on my bad English.

turn indicator = car blinkers (red led diode) - I do not know how to call them

Then what you need is a state machine.

So i cant move forward with my car bot if the turn indicators are working, i wont to normaly drive my car bot while turn indicators are working...

For example, when i press key 'W' car bot go forward and turn indicator (red blinker) blinking

majenko:
Then what you need is a state machine.

turn indicator = car blinkers (red led diode) - I do not know how to call them

Hi,

I think I'd separate the blinking and motion in that situation. Can't promise its free from errors, but you could try something along these lines perhaps:

int g_nIndicator; // Indicator that is on currently. -1 => left, 0 => none; 1=>right.
bool g_bBlinkState; // True=> light on; false=>off.
unsigned long g_ulLastBlink; // Time that last blink occurred.

void loop()
{
  char chKey;

  chKey = Serial.read();

  switch (chKey)
  {
    case 'W':
       // Start moving car forward.
       g_nIndicator = 0; // turn off indicator.
       break;
    case 'S':
        // Start moving car back
       g_nIndicator = 0; // turn off indicator.
        break;
    case 'A':
        // Start turning left
       g_nIndicator = 0; // turn off indicator.
        break;
    case 'D':
       // Start turning right
       g_nIndicator = 0; // turn off indicator.
       break;
    case 'L':
       // Enable left indicator.
       g_nIndicator = -1;
       g_bBlinkState = true;
       g_ulLastBlink = millis(); // time indicator state changed.
       // Turn on left indicator. 
       // turn off right indicator.
       break;
    case 'R':
       // Enable right indicator.
       g_nIndicator = 1;
       g_bBlinkState = true;
       g_ulLastBlink = millis(); // time indicator state changed.
       // Turn on right indicator. 
       // turn off left indicator.
       break;
  }

    // update indicator based on time passed
    if (g_nIndicator != 0 && millis() - g_ulLastBlink > 200) // time between blinks
    {
       g_bBlinkState = !g_bBlinkState; // Toggle illumination state.
       g_ulLastBlink = millis();
       if (g_nIndicator == -1)
          SetLeftIndicator(g_bBlinkState); // turn left indicator on if g_bBlinkState is true; otherwise off. 
       else
          SetRightIndicator(g_bBlinkState); // turn left indicator on if g_bBlinkState is true; otherwise off. 
    }
  }
}

Then what you need is a state machine.

Yes. Certainly doing nothing but blinking the LED is not the right thing to do.

You need to know whether to blink the LED while going forward, or not. While moving forward, you blink the LED, or not, as necessary.

To anticipate your next question, the delay() have got to go.

Delay must go because while in delay no user command is seen. Doing nothing is really nothing.

Paul_Martinsen does show one way to not use delay().