Go Down

Topic: [newbie] timer countdown...basics (Read 1 time) previous topic - next topic

Luis Manson

Hi, im tring to do a miltiple timer... its has to do an action after some intervals...
there is also a button read involved and i have no idea on how to do something like:
if millis() == 14000
print 14 secconds...

any examples out there?
--

AlphaBeta

Code: [Select]
if (millis() % 1000 == 0) {
 Serial.print(millis()/1000);
 Serial.println(" seconds");
}


This will print time since startup, in seconds (at least I think so. snippet NOT TESTED).

PaulS

#2
Sep 17, 2010, 03:33 am Last Edit: Sep 17, 2010, 03:33 am by PaulS Reason: 1
In AlphaBeta's example the two calls to millis() may return different values. If that's OK, use his code.

If not, create a local variable:
Code: [Select]
unsigned long now = millis();
Then, use the local variable, now, in place of both calls to millis.
Code: [Select]
if (now % 1000 == 0) {
 Serial.print(now/1000);
 Serial.println(" seconds");
}


Big Oil

If you didn't know already, the % in their code is called modulus.  It's the remainder from division.  5 % 3 would equal 2.  7 % 3 equals 1.  The modulus is how much is left over.  
You could use if (now==14000) to print it just one time.

AlphaBeta

Actually... when I think about it, I think using % 1000 == 0 is a bad idea because then you need to run that code EXACTLY when the millis return 1000 2000 3000 ...
if ( (now % 1000) < 50 ) {/*second has passed*/ }
This is more likely to work, but then again, you might get more than one printout. You could store a previousSecond variable to check that you only print a second once.

Luis Manson

thanks for your replies, this seems to bit a bit more harder than i first tought
so im considering on trying this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1217881285

i dont need a high precision, but since the last time i played with electronics the most complex thing i learned was about the 555 this a totally different concept

my idea is to have an array or something of 3 times and 3 steps for a servo, so after pressing start those actions should occur
--

Go Up