Go Down

Topic: Can someone explain millis (Read 204 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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Go Up