While, if ... question

Hello everyone, I am running a code very simple in order to drive 2 motors from zero to 90 degrees. If I replace "while" by "if" the prgramme is running well. However, if I keep "while" nothing happens. I do not see why I cannot use "while" as well as "if".
Thanks

#include <Servo.h>

Servo servo1;
Servo servo2;
int initialPos = 0;
int targetPos = 100;
unsigned long startTime;
unsigned long duration = 8000;  // Durée totale en millisecondes
float acceleration = 0.05;  // Accélération par milliseconde
float pos;

void setup() {
  servo1.attach(10);  // Connectez le servo au pin 10
  servo2.attach(11);  // Connectez le servo au pin 11
  servo1.write(initialPos);  // Position initiale du servo
  servo2.write(initialPos);  // Position initiale du servo
  startTime = millis();  // Enregistrer le temps de départ
}

void loop() {
  unsigned long currentTime = millis();
  unsigned long elapsedTime = currentTime - startTime;

  while (elapsedTime <= duration) {
    // Calculer la progression en fonction du temps écoulé
    float progress = (float)elapsedTime / duration;

    // Calculer la position en fonction de la progression
    pos = initialPos + (targetPos - initialPos) * progress;

    // Calculer la vitesse en fonction de l'accélération
    float speed = acceleration * elapsedTime;

    // Écrire la position au servo moteur
    servo1.write(pos);
    servo2.write(pos);

    // Délai en fonction de la vitesse
    delay(speed);
  }
}


    while (elapsedTime <= duration)
    {
        // Calculer la progression en fonction du temps écoulé
        float progress = (float)elapsedTime / duration;

        // Calculer la position en fonction de la progression
        pos = initialPos + (targetPos - initialPos) * progress;

        // Calculer la vitesse en fonction de l'accélération
        float speed = acceleration * elapsedTime;

        // Écrire la position au servo moteur
        servo1.write(pos);
        servo2.write(pos);

        // Délai en fonction de la vitesse
        delay(speed);
    }

In order for this while loop to start elapsedTime must be smaller or equal to duration and it will not end whilst that is true. However, neither value is changed inside the while loop so it will never end

You don’t update elapsedTime when you use the while loop.

When you have an if, the loop loops and the variables are updated

thanks

This is about why if() instead of while() inside of loop().
It is because with if() you can run many functions together smoothly,
but with locked-in inner loops the smooth is less to jitters and jerks.

Ask yourself what is the purpose of a while loop inside of void loop()?

void loop()
{
  BlockingLoop();  //  while-driven time delay runs by itself
}
void loop()  // non blocking loop() runs 10's of times per milli, misses little.
{
  NonBlockingIf();  //  allows other code in loop to execute very soon
  StopButtonIf();
  StatusLedBlinkIf();  // shows that the main loop is running, stuttering or stopped.
}

When I first added a counter to loop() that added 1 to an unsigned long count variable then printed the count and set it = 0 every second..

I was amazed at how fast simple button and led code ran even using Arduino foolsafe pin functions lke digitalRead(). With PC's the clock is the keyboard clock that scans the keys 18.2 times per second only so working to the second was cool. Now with Arduino I see one millisecond like it is one minute. Are you game for that?

1 Like

Not at all. Thanks for your insight. Appreciate very much and interesting.

Here is a link to a well explained tutorial, with code, on the subject. You can know why and what and how, mess with the code to lock that in with practice. It will make you sharp.

Nick Gammon's Do many things at once tutorial.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.