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?
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).
}
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
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.
}
}
}