Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
Millis function programming
January 9, 2022 7:50 PM
Hi hope this is in the right place to post. Ian trying to make a timer use the millis function. The way I understand it you set the interval and every I’m you come to it it does what you want. Which means if you set the interval to 10 seconds then every 10 seconds it should repeat. What my program is waiting the 10 seconds then it performs the action every 2 or seconds.seems that the time line is not updating any info on this would be much appreciated
This topic was modified 2 weeks ago by Hans
January 10, 2022 5:08 AM
The millis() function returns the number of milliseconds passed after turning on your Arduino.
So this is not doing what you have in mind.
Reference: Arduino millis()
Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
You can however use millis to assist in making a timer doing something every 2 seconds (2 seconds = 2000 milliseconds).
Something like this:
#define millisInterval 2000 // 2000 millisceonds = 2 seconds
unsigned long millisStart; // a global variable so this remains accessible anywhere in the program
millisStart = millis(); // initial start time
int millisCurrent = millis(); // current value, so we stay as close as possible to the desired interval
if (millisCurrent - starmillisStarttMillis >= millisInterval) // Did 2 seconds or more pass?
millisStart = millisCurrent; // make sure we start counting 2 seconds from this moment
// do something here
Hope this helps.
January 10, 2022 9:41 AM
Thanks for your replay. That is not the understanding I have about millis function. According to all the videos I have watched your are supposed to be able trigger an event. Every tine the millis >= the interval . So you set the interval for 10 seconds and say lcd.print(“something”) then 10 sec later it is suppose to to it again. I am eating the first 10 sec. Then repeats every 2 or 3 sec after that. What I am trying to do is make a day counter so one every 24 hr,the bay increments .
Again thanks for your assistance
January 11, 2022 5:18 AM
I'm sorry David.
Millis() really doesn't work that way. Sorry, I truly wished it would ... 😊
(if I'm wrong and you have a working example: then please let me know - I'm not all-knowing either and would love to learn new stuff as well!)
I think those videos may be using a specific library, or some uncommon AVR functions that are not used by Arduino by default??
Do you have any links to those videos?
This article, which is very detailed on the millis() topic, confirms it.
Also note that the Arduino hardware is not event driven, and doesn't really do multi-tasking (on AVR level just modest task-switching).
You can however tinker with interrupts - a library like TimerInterrupt may be able to help you with this.
Or choose the more complex approsch with certain timer AVR function (see this Instructable).
This article may explain it better (easier to understand).
I know the FastLED library (for controlling LED strips) may have something like that as well. EVERY_N_MILLISECONDS - couldn't find any good documentation on this, I did find this brief question though - but I'm not sure it will do what you are having in mind.