Go Down

Topic: Color change timer (Read 3966 times) previous topic - next topic


It may help you to see what is happening if you familiarize yourself with millis by playing around with a simpler sketch.

Perhaps try a sketch that just turns on an LED after a few seconds using millis.

Then try adding code some millis code to turn the led off  a few seconds after its turned on.

Although using delay would be a more natural choice in this exercise, doing this using millis should give you insight as to how to apply it in your timer application.

Try it and see how you get on


Thanks so much everyone!! ;) and
thanks, mem for your "millis" information again!!

I used "while" function into the loop in the last part, like below.
Code: [Select]

 while ( digitalRead(switchPin) == LOW){
    if( digitalRead(switchPin) == HIGH)
  setColor(0,0,0,0);  // turn LEDs off      
      if( digitalRead(switchPin) == HIGH)
  setColor(0,0,0,0);  // turn LEDs off

It probably looks a strange sketch for you guys (professionals),
but the timer is somehow working well that what I want to do.
Mills function is still diffucult for me at this stage :(
But I will keep learn more about the program!!

Thanks again ;) You guys helped me a lot!!
(But I will ask another question sometime in new topic. c u then!)


Oct 05, 2008, 10:43 am Last Edit: Oct 05, 2008, 10:46 am by mem Reason: 1
Using delay instead of millis is cheating  ;)
It's a good thing programming is one of the few places where cheating is not only acceptable, its often highly desirable.

Delay works because you don't need to do anything during the delay period. But if you enhance your application and do need to do something like flash LEDs within the delay period then you may want to reconsider using millis.

Think of that millis code as if it was like telling time using a clock. You look at the clock (call millis)  to get the current time and save it in a variable. You can then always tell how much time has elapsed by looking at the clock again (calling millis) and taking the difference between the current time and the time you saved. You will know the desired time has elapsed when the difference between the current time and the start time is at least as big as your desired elapsed time.

Hope that makes sense. Have fun!


Keep having fun with Arduino and pretty soon, this will all be second nature.  ;)


Oct 06, 2008, 04:51 am Last Edit: Oct 06, 2008, 04:52 am by mikalhart Reason: 1
Using delay instead of millis is cheating.

Mem's commentary is right on, eclipse.  As soon as you decide you want to make just the tiniest extension to your program's function, you're going to facing some complicated surgery.  

Any but the most trivial of my Arduino programs usually end up looking something like this:

Code: [Select]
static unsigned long old_millis = 0;
... // other state variables

void loop()
 unsigned long new_millis() = millis();

 Based on new_millis, old_millis, input pins and the state variables
   do some stuff and modify the state variables
 old_millis = new_millis;

And as Mathieu said, keep at it!  This can be really fun.


Go Up