Pages: 1 [2]   Go Down
Author Topic: Color change timer  (Read 1365 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much everyone!! smiley-wink and
thanks, mem for your "millis" information again!!

I used "while" function into the loop in the last part, like below.
Code:
 while ( digitalRead(switchPin) == LOW){
    digitalWrite(ledPinR,HIGH);
  delay(250);
    digitalWrite(ledPinR,LOW);
  delay(250);
     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 smiley-sad
But I will keep learn more about the program!!

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using delay instead of millis is cheating  smiley-wink
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!
« Last Edit: October 05, 2008, 03:46:44 am by mem » Logged

New Jersey
Offline Offline
Full Member
***
Karma: 0
Posts: 193
Ard at work
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Keep having fun with Arduino and pretty soon, this will all be second nature.  smiley-wink
Logged

----------
Mathieu

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:
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.

Mikal
« Last Edit: October 05, 2008, 09:52:56 pm by mikalhart » Logged

Pages: 1 [2]   Go Up
Jump to: