Loading...
Logo
Processing Forum
I am running into a problem with millis()
 
I am simply trying to generate a loop that will give 5 seconds to calibrate a sensor then continue with the sketch...
 
I tried this:
 
void setup()
{
}
 
void draw()
{
   int x=millis();
   while (millis()-x<5000){
      println(x); //space for my calibration routine to collect sensor data
   }
}
 
It locks my pc up everytime...  I just get a stream of "64" displayed in the window...
 
What am I doing wrong???
 
 

Replies(6)

It is bad to do an (almost) empty loop to spend some time (CPU bound), and it is bad to flood the console too (here with 5000 lines), as it can bring down the PDE.
One solution is to use delay(), at least it won't lock the PC. But it will freeze the sketch, which isn't friendly.
Another solution is to do nothing in draw(): since it is called several times per second, it is the body of a loop itself.

Using the second solution:
Copy code
  1. int x;
  2. void setup()
  3. {
  4.   x = millis();
  5. }
  6.  
  7. void draw()
  8. {
  9.   if (millis() - x > 5000) { // Notice I inverted the test!
  10.       println(x); //space for my calibration routine to collect sensor data
  11.       x = millis(); // Reset the timer
  12.   }
  13. }
You should use a better name for the variable (x is often used for coordinates), like startingTime or similar.
a) You need to put a sleep inside the loop or else it will overload the computer with nonstop IO attempts. By the time the while loop is actually exited, there are so many calls to println that your computer has locked up.

 
b) You're printing the variable x, which you set as the time at the start of draw(). I think you want to actually print millis() - x.
The IF statement route is good but my problem is that I need it to sit in one loop reading sensor data and analyzing it for about 5 secs before moving on to the rest of the program.  I have been using flags to get around this...

Each piece of my code that needs to be run sequentially is encapsulated by an if (flag=#){ so that it skips everything except the section of my sketch that needs to be run.  When that section finishes it changes the flag to equal the next number and it moves into the next routine ignoring everything else.


I'm not sure if this is the right way to deal with
Linear programming needs.

I basically need to do the following:

1) grow window/ frame size to fill screen. 
2) Show a animated ball shrinking in size
3) when ball is  gone wait 5 seconds while collecting data and averaging results
4) shrink window size and continue begin main programm


I can't do it under setup because calibration can be called at any time by the user yet it has to be done linearly so the while loop seemed like a good approach since it stays in the loop until done then moves down the main sketch


I can post my code but I need time to clean it up and get comments in there... It's very very big now so I figured it would be confusing to show it
" I need it to sit in one loop reading sensor data"
Well, as I wrote, draw() is the loop you need...

Indeed, using flags is a way to do things sequentially. That, or a state variable, indicating by a number at which step you are.
Using functions can help in modularizing the code and making it more manageable / readable.
Thanks now when you say "put a sleep..." do you just mean a simple delay???
Im confused about delays because I thought it was removed from processing...
I am luckily using the robot class in my sketch so I am using robot.delay when I feel I need one but is the delay(#) command still available in some way????
delay() is back in latest versions of Processing. Not sure it was a good move, since I felt it should have been renamed to Serial.delay(): that's probably the only place where it is really needed...

Actually, delay() is just a convenient wrapper around the Thread.sleep() method.