Blink and return to loop()

Hello
Please, can someone tell me how to get out of the loop of toRight() function ?

 int greenLed = 10;
 int yellowLed = 4;
 int redLedDroite = 6;
 int redLedGauche = 7;
 
 

int ledState = LOW;        
long previousMillis = 0;        
unsigned long currentMillis = millis();
long interval = 1000; 
 
  char ch;
  
  void setup()
  {
    // LEDS  
     pinMode(greenLed, OUTPUT);
     pinMode(yellowLed, OUTPUT);
     pinMode(redLedDroite, OUTPUT);
     pinMode(redLedGauche, OUTPUT);
    
    Serial.begin(9600);
  }
  
  void loop()
  {
    if(Serial.available())
    {
      ch = Serial.read();
       if(ch == 'U'){
         avance();  //forward
       } 
       else if(ch == 'A'){
         recule();  // backward
       } 
       else if(ch == 'D'){
         do
         {
           toRight(); // right 
         } while(ch == 'D');
        } 
        else if(ch == 'G'){  
         toLeft(); // left
        }
    }
  }
  
  void avance()
  {
   digitalWrite(greenLed, HIGH);
   digitalWrite(redLedDroite, LOW);
   digitalWrite(redLedGauche, LOW);
   digitalWrite(yellowLed, LOW);  
  }
  
  void recule()
  {
   digitalWrite(greenLed, LOW);
   digitalWrite(redLedDroite, LOW);
   digitalWrite(redLedGauche, LOW);
   digitalWrite(yellowLed, HIGH);     
  }
  
  void toRight()
  {
    
   digitalWrite(greenLed, LOW);
   digitalWrite(redLedGauche, LOW);
   digitalWrite(yellowLed, LOW);  
   
         if(currentMillis - previousMillis > interval) {
            // save the last time you blinked the LED 
            previousMillis = currentMillis;   
            if(ledState == LOW)
              ledState = HIGH;
            else
             ledState = LOW;  
            digitalWrite(redLedDroite, ledState);
          } 
          
          currentMillis = millis();
 }
  
  void toLeft()
  {
   digitalWrite(greenLed, LOW);
   digitalWrite(redLedDroite, LOW);
   digitalWrite(redLedGauche, HIGH);
   digitalWrite(yellowLed, LOW);     
  }

My goal is to spend an android bluetooth commands to Arduino: the tablet is sending commands and receives arduino but it is at this level (void toRigth() )Led blink but does not return to the fontion loop ()

thank you very much.

         do
         {
           toRight(); // right 
         } while(ch == 'D');

How is ch supposed to get a new value?

Thank you for the quick response.
Yes, ch = Serial.read() at the beginning of loop () and with a println (ch) ch is always equal to 'D' as if there is more than receipt of datas...

Rivo:
Thank you for the quick response.
Yes, ch = Serial.read() at the beginning of loop () and with a println (ch) ch is always equal to ‘D’ as if there is more than receipt of datas…

The “beginning of loop ()” isn’t run as long as you are still within your while loop, so ch never gets updated.

It looks to me like you got stuck with the toRight() function. It's non-blocking so you wrapped it in that do while() loop to make it work. Now you're stuck there.

Yes, but unfortunately it's just that my problem .... poorly written program.... But how do that ? could you help me ?

Instead of calling toRight() when 'D' is sent, set a boolean flag. Then in loop() call toRight() conditional to the flag.

You may have to figure out a way to turn it off.

Rivo:
Yes, but unfortunately it's just that my problem .... poorly written program.... But how do that ? could you help me ?

Create a flag variable:

byte flag = 0;

When serial data is received, set that flag to 0:

If (Serial.available() > 0)
{
  flag = 0;
  ...
}

If the serial data you received was a 'D', then set the flag:

...
else if (ch == 'D')
{
  flag = 1;
}
...

In your loop() function (and not nested in any other if statement), only call toRight() if the flag is set:

if (flag == 1)
{
  toRight();
}

hello

thxs Arrch... you are the best...problem solved

 if (flag == 1)
{
  toRight();
}

this code should be at the top of loop and it works perfectly
thank you again