Page 1 of 1
Forum

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 pro...
 
Share:
Notifications
Clear all

Millis function programming

4 Posts
2 Users
0 Likes
743 Views
 Anonymous
Joined: 53 years ago
Posts: 0
Topic starter  

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

thanks

david


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 9 years ago
Posts: 2311
 

Hi David,

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

void setup()
{
  millisStart = millis();  // initial start time
}

void loop()
{
  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.


   
ReplyQuote
 Anonymous
Joined: 53 years ago
Posts: 0
Topic starter  

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 

David 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 9 years ago
Posts: 2311
 

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?

Note:

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.


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: