element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum A question of Arduino timing
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 11 replies
  • Subscribers 388 subscribers
  • Views 2845 views
  • Users 0 members are here
  • arduino timer
Related

A question of Arduino timing

colporteur
colporteur over 3 years ago

I'm working on a small project that requires a three minute timer. Default with power up an LED comes on. Push a button an the LED goes out for three minutes and then lights again. It is a simple circuit using a few pins on an Arduino Nano and the code delay (180000) to get the three minutes. My middle school teaching daughter asked me to build the LED timer (I called it an egg timer) to assist a child in her classroom that has a learning disability.

 

The delay code is a brute force method of counting down time. I did some reading on millis() and thought maybe it is the better approach?

 

I don't have a need to manage other tasks while I wait for time, which is what I see millis() does (I think). I tried generating a block of code for the three minute timer using millis() with little success. I abandoned the exercise and used delay. I have a mental post it note to follow-up on the learning to create a working three minute timer using millis(). The code would count what ever time you set, in this case three minutes.

 

Any insight a member has to offer is welcome. I apologise up front, if my responses to any replies are dumb. I'm a resurrectionist rather than a programmer. I have enough knowledge to kludge together different code parts to create something. I lack the skills to actually program from scratch.

  • Sign in to reply
  • Cancel
Parents
  • colporteur
    colporteur over 3 years ago

    I've had such great success stuffing notes in a bottle and throwing them into the E14 Community lake looking for responses that I'm guilty of maybe abusing the privilege.

     

    When I was working, the coffee breaks where the people who worked in the same field sat around a chatted and shared insight is a resource I truly miss. There is nothing better than getting information from someone with experience from hoeing that row, than doing it without the insight.

     

    I haven't put the code to silicon yet but analyzing it to better understand I have a question:

    while(myFinishTime > millis());

    Should the conditional test be less than (<). The line myFinishTime = millis() + 180000UL; Always makes millis() true for a greater than. I should actually try it:) I'm seeing the similarity between

    jc2048 & Andrew J code and thought I would ask.

     

    Are there any timing like modules? I called this an egg timer routine. I'm thinking someone might have assembled a module that does this lifting. You want ten minutes just call this module and use the function. Again I may be over simplifying what is a more complex topic. The stuff I have been reading on millis() and how it can be used gives me that impression.

     

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 3 years ago in reply to colporteur

    millis() is the time in milliseconds since the Arduino booted, so always starts at zero when you turn it on and continually increments until you turn the Arduino off.  If you don't do that for a VERY long time, it will overflow back to 0 but still continue counting. 

     

    What Jon's code is doing is calculating a value that is 180000ms in the future from NOW.  The while loop is thus looping continually whilst its condition is true.  So imagine a start time of millis() = 0 = NOW.  So, While NOW + 3 minutes > current millis() holds good it loops; once the value of current millis() is more than 3 minutes since the start time, while evaluates to false and the loop exits.  Jon's loop is no different to doing delay(180000); in your simple scenario (neither is my version.)  I can see why you might get confused though: while loops continue to loop whilst its test evaluates to TRUE.  If you used finishTime < millis() it would immediately evaluate to false and thus the while loop would exit and you would have no delay.

     

    delay() is blocking, millis() isn't as it's just getting a value, so it allows the Arduino to do other things as long as you don't put a blocking loop in the code like the above does.

     

    I don't know if there is an 'egg timer' routine, I haven't looked but I wouldn't be surprised.  I can imagine it's something that would work on an interrupt basis: that is, you ask it to start timing then go off and do something more interesting than waiting.  When the timer elapses, it calls your Interrupt Service Routing (ISR) that you told it about.  You should look up ISR if you want more info on that topic!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Andrew J
    Andrew J over 3 years ago in reply to colporteur

    millis() is the time in milliseconds since the Arduino booted, so always starts at zero when you turn it on and continually increments until you turn the Arduino off.  If you don't do that for a VERY long time, it will overflow back to 0 but still continue counting. 

     

    What Jon's code is doing is calculating a value that is 180000ms in the future from NOW.  The while loop is thus looping continually whilst its condition is true.  So imagine a start time of millis() = 0 = NOW.  So, While NOW + 3 minutes > current millis() holds good it loops; once the value of current millis() is more than 3 minutes since the start time, while evaluates to false and the loop exits.  Jon's loop is no different to doing delay(180000); in your simple scenario (neither is my version.)  I can see why you might get confused though: while loops continue to loop whilst its test evaluates to TRUE.  If you used finishTime < millis() it would immediately evaluate to false and thus the while loop would exit and you would have no delay.

     

    delay() is blocking, millis() isn't as it's just getting a value, so it allows the Arduino to do other things as long as you don't put a blocking loop in the code like the above does.

     

    I don't know if there is an 'egg timer' routine, I haven't looked but I wouldn't be surprised.  I can imagine it's something that would work on an interrupt basis: that is, you ask it to start timing then go off and do something more interesting than waiting.  When the timer elapses, it calls your Interrupt Service Routing (ISR) that you told it about.  You should look up ISR if you want more info on that topic!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • Andrew J
    Andrew J over 3 years ago in reply to Andrew J

    Also, you didn't get to level 8 without helping others so what goes around comes around!!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube