Using millis() for Case Switch

How to I store a snapshot of millis() as a variabe at the beginning of each case switch? I need to know the time of the internal clock at the beginning of each case switch and then add 10 more seconds so that the code knows to do something with that 10 sec or change to new case after the 10 secs. Something like:

if (snapshotOfmillis < snapshotOfmillis + 10000) {do this stuff} //do this while within the allotted 10 secs.

else if (millis > (snapshotOfmillis + 10000) ){break;} // if greater that ten seconds, then case switch

What Say You! Thanks in advance for any relies and help!

Taking the current value of millis() is as simple as assigning a variable.

Say, in a conditional statement or before a while loop.

Then you compare the difference in that variable and what millis() is when you want to do something else.

unsigned long snapshotOfTime = 0;

snapshotOfTime = millis();

 if (millis() - snapshotOfTime > 10000){


Thank you for the reply.

The while (1)…is the 1 just a placeholder for some other code or do I actually put 1 in the code?

I know know 1 to be TRUE or a state of HIGH as opposed to LOW. Could you explain?

If you have no condition for when a while loop should end, you put 1.

It means "I want this loop to loop forever".

Or you could put 2... or 3.. since any integer over 0 wont ever be 0, the condition for the while loop to end wont ever be met.

You could put while(true). Doesn't really matter.

This concept is less useful for infinite loops and more useful when reading data, and you don't want to do anything until the number of data bytes falls down to 0.

It's why while(!Serial.available()); works for halting a program until data is ready to be received. But I digress.

It really depends on what you want to do. If you don't want to exit your case until a condition is met, you could put a while loop with a break. The reason I used it was because I was just to give an arbitrary example of how millis() can be used with a loop. millis() can be used numerous ways.

Thanks Apex. I will pop this info into my code and see how it works. More specifically I'm trying to run an RGB LED for ten seconds then have it randomly pick one of the other cases (case switch). I will let you know how it works out for me - hopefully it will.

Almost there…I think. I can get it to switch from case to case, but the code within the cases (the code that changes each brightness) seems to be ignored. It defaults to just the lowest brightness and it won’t toggle to HIGH BRIGHTNESS or HIGHEST BRIGHTNESS. Here is a code snippet for just the first case (case 0):

  void loop()

     int ledcolor = random(2); //this randomly selects a number between 0 and 6

     switch (ledcolor) {
     case 0: //if ledcolor equals 0 return this color and these brightnesses

     buttonState1 = digitalRead(buttonPin1); // read the state of button 1
     buttonState2 = digitalRead(buttonPin2); //red the state of button 2

     unsigned long snapshotOfTime = 0;
     snapshotOfTime = millis();
     while (millis() <snapshotOfTime + 10000) 
      {if (buttonState1==LOW && buttonState2 == LOW){setColor(10,0,0);} //red low brightness
       else if(buttonState1== HIGH && buttonState2==LOW){ setColor(30,0,0);}//higher brightness

       else if (buttonState2==HIGH){setColor(255,0,0);} //jighest brightness

       if (millis() - snapshotOfTime > 10000) {break;}

I figured it out!

I had to add…

buttonState1 = digitalRead(buttonPin1); // read the state of button 1
buttonState2 = digitalRead(buttonPin2); //red the state of button 2

…within the while loop. It was outside before and now I see that the while statement will ignore everything outside of it.

THANK YOU APEX! - wouldn’t have figured out the while loop without you, but I do get it now. Many Many Thanks!

while (millis() <snapshotOfTime + 10000)

should be

while (millis() - snapshotOfTime < 10000)

to prevent overflow errors when millis() wraps arounf (but that is only once per 7 weeks)