Go Down

Topic: Can someone explain millis (Read 4763 times) previous topic - next topic

mOskit

Hi.
Need someone who explain millis... but the way me, an Polish guy will understand.
Simply need a button that once pressed and hold more than 3 second will write sensor data to eeprom but if hold less than free second will use eeprom data to drive output.

I`m not asking for code. just need to know how to use millis, or anything that will count time.
thanks

AWOL

#1
Dec 15, 2014, 05:10 pm Last Edit: Dec 15, 2014, 05:11 pm by AWOL
millis () simply returns the number of milliseconds (1/1000 of a second) that have passed since the device was last reset.
A simple, human-scale way of thinking of it is the number of seconds since midnight.
As such, it can be used as a timestamp; at a very simple level a higher millis value will have occurred later than a lower millis value.
Subtracting the smaller from the larger will give the number of milliseconds that have elapsed between the two events.

mOskit

Does make sense.
So it should go along with function "long" to get the correct amount of time since the button been pressed?
(millis()-long)==time elapsed since button pressed?

AWOL

"long" is a datatype, like "int", not a function, so "millis() - long" is going to give you a compilation error.

"millis()" returns an "unsigned long"  (32 bit, unsigned) value

mOskit


unsigned long buttonOn = millis();
unsigned long buttonOff = millis();

if(buttonOn-buttonOff>3000)do something
if(buttonOn-buttonOff<3000)do not react

damn
will this work?

AWOL

Well, in the example you gave, the values of "buttonOn" and "buttonOff" will almost certainly be the same.

Why don't you try it?
Play around, print some values, see how it goes.

mOskit

I`m just at work now and cannot plug my arduino to the local pc. I`ll try at home and after i`ll post some results.
Thanks

MrDropsy

Hi,

This is a very good thread to read from Robin 2

http://forum.arduino.cc/index.php?topic=223286.0

Its good to use things like unsigned long currentMillis

we use long to prevent overflow

GoForSmoke

#8
Mar 01, 2015, 04:17 am Last Edit: Mar 01, 2015, 04:19 am by GoForSmoke
millis () simply returns the number of milliseconds (1/1000 of a second) that have passed since the device was last reset.
A simple, human-scale way of thinking of it is the number of seconds since midnight.
As such, it can be used as a timestamp; at a very simple level a higher millis value will have occurred later than a lower millis value.
Subtracting the smaller from the larger will give the number of milliseconds that have elapsed between the two events.
I just had to pick my jaw back up after reading that last line because You made it.

Always always subtract the start time from the end (or current) time to get the elapsed time, same as a clock. If my start hour is 11 and my end hour is 2 then by unsigned base-12 math 2 - 11 = 3. Binary is another base and unsigned math works the same for the 32-bit unsigned long that millis() returns. 

Rollover of millis() takes 49.7-some days.
If you use micros() instead of millis() then rollover takes a little over 90 minutes.
Do the always rule and rollover is not a problem. Same code handles all cases.

Mr.Dropsy -- there is no need to use long instead. All that does is break the algorithm.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up