"for" loop into a "for" loop

Hello,

After solving my number transfert from Nextion to Arduino (see: Arduino Forum), I have another question.

I want to add a progress bar to have a nice looking screen but I’m wondering where to add the progress bar’s code.
Here is what I’m thinking about:

//Remplissage echantillon client
    for (int n=1;n<=nombreBouteille;n++)
         { 
           Serial.print("remplissage bout n°:");////pour debug, a effacer
           Serial.println(n);//pour debug, a effacer         
           Remplissage.setValue(n);
  /**                    
  *        j0.setValue(0);             
  *        for(int z=0;z<=TempsAllumagePompel;z++)
  *        {
  *        x=map(z,0,TempsAllumagePompel,0,100);
           j0.setValue(x);
  *        }
  */       
           digitalWrite(13,HIGH);
           delay(TempsAllumagePompel); 
           digitalWrite(13,LOW);
           delay(1000);
         }

The code I want to add is the commented part. If I do that, I’m think the program will loop in my progress bar then will set the pin 13 HIGH. Thus the filling will be done after the progress bar is completed.
Thus, how to add the progress bar “for” loop and have it working together with the “remplissage” loop?

Thanks

If you have a loop in a loop, the inner loop will execute each time the outer loop executes once. if you want both loops simultaneous, you cannot use for, as you cannot exit a for loop until the condition is met. Or, you can try to do both tasks within a single for loop.

Thanks for the reply. Unfortunately, you answered exactly what I did not want to read :slight_smile: .

I'll rethink completely my first loop to include the progress bar to have a single "for" loop. With my noob level in in C++, it won't be easy.

Do you think I could create a progress bar function (containing the progress bar "for" loop) and call it where needed? Does it stop the other loop until the function is executed?

Best regards

The processor can do only one task at a time, so if two processes are supposed to happen “at the same time”, you have to share that time between them somehow.

Much more difficult than expected.

I’ll try everything I know and if it is not working, I’ll just forget about the progress bar. It is just a nice to have feature…

Thanks for the help.

BR

You just have to implement your own sort of version of a for loop. Each portion will execute and increment the counter until the end condition is met. If both end conditions are met, ‘done’ never gets reset to false so the while() loop exits. With those delay()s in your code, this won’t happen very fast but should give you the idea.

//Remplissage echantillon client
bool done = false;
int n = 1;
int z = 0;
while ( !done ) {
  done = true;
  if (n <= nombreBouteille)
  {
    Serial.print("remplissage bout n°:");////pour debug, a effacer
    Serial.println(n);//pour debug, a effacer
    Remplissage.setValue(n);
    n++;
    done = false;
  }

  if (z <= TempsAllumagePompel )
  {
    x = map(z, 0, TempsAllumagePompel, 0, 100);
    j0.setValue(x);
    z++;
    done = false;
  }

  digitalWrite(13, HIGH);
  delay(TempsAllumagePompel);
  digitalWrite(13, LOW);
  delay(1000);
}

Hi,
@blh64, thanks for the code. i'll try that tomorrow.
I'm not familiar yet with C++ thus, these kind of ideas are not very evident for me. Maybe one day I would have come to that solution but after many many fails...

BR

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

Also note that there is no blocking FOR or WHILE loop.

...R

Hello,

I have more or less won.
First I wished my progress bar evolving with the delay but it is way too difficult for my level.
Thus I made the progress bar evolving with the number of the bottle I fill.

I’m glad to tell my project is done and working. Next steps are to create a nice enclosure and find a reliable and repeatable pump.

Here is the full code of the syrup dispenser I’ve made and the HMI file (remove .ino in the filename) for the nextion

Certainly not the state of the art in coding but for someone who, 12 months ago, was not knowing what is an arduino, it is not so bad.

Thanks to the one who helped.
Best regards

dosage_sirop_V4.HMI.ino (1.94 MB)

dosage_sirop_nextion_v4.ino (8.25 KB)