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 2852 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
  • jc2048
    jc2048 over 3 years ago in reply to colporteur

    This was going to be my reply, but I went off and rescued some food that was cooking and when I came back Andrew had beaten me to it.

     

    Behind the scenes, there will be an interrupt running from a timer. It may be once a millisecond or it may be faster (with an additional prescaler count). Whichever, there will be a hidden variable (an unsigned long, so a 32-bit binary number) that gets incremented once a millisecond. The millis() function simply returns that value to you when you call it.

     

    All it's doing, really, is saving you from having to put together the interrupt routine for yourself, which is what an embedded-code software engineer would do.

     

    The line

     

    myFinishTime = millis() + 180000UL;

     

    is saying read the current time, add 3 minutes' worth of milliseconds to it, and save the result as the value millis() will return once the time period we want is up.

     

    The do-while loop

     

    do {}
      while(myFinishTime > millis());

     

    is then saying, keep doing what's between the curly brackets while the finish time is greater than the current time. Eventually, millis() catches up with myFinishTime, and then the do loop finishes and control passes on.

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

    Awe ouch! The variable is outside the conditional test brackets so it is constant.

     

    Was that a tree in the forest? I couldn't see. My bad. I like the simplicity. I may go back and modify my delay code to test this out.

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

    Awe ouch! The variable is outside the conditional test brackets so it is constant.

     

    Was that a tree in the forest? I couldn't see. My bad. I like the simplicity. I may go back and modify my delay code to test this out.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
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