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
  • jc2048
    jc2048 over 3 years ago

    This worked OK for me on a UNO. It sets the board LED on for 3 minutes and then extinguishes it.

     

    void setup() {
    pinMode(13, OUTPUT);    // sets the digital pin 13 as output
    }

     

    void loop() {

    unsigned long myFinishTime;

    myFinishTime = millis() + 180000UL;

    digitalWrite(13, HIGH);

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

    digitalWrite(13, LOW);

    do {}
      while (1);
    }

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

    This worked OK for me on a UNO. It sets the board LED on for 3 minutes and then extinguishes it.

     

    void setup() {
    pinMode(13, OUTPUT);    // sets the digital pin 13 as output
    }

     

    void loop() {

    unsigned long myFinishTime;

    myFinishTime = millis() + 180000UL;

    digitalWrite(13, HIGH);

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

    digitalWrite(13, LOW);

    do {}
      while (1);
    }

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

    If you want this running continuously, turning on/off for 3 minutes, you can adjust Jon's code slightly (I haven't compiled and run this but it should work ok):

     

    boolean currentState = true;
    unsigned long delayTime = 180000; // required delay in milliseconds.
    
    void setup()
         pinMode(13, OUTPUT);
    }
    
    void loop() {
         digitalWrite(13, currentState);
         unsigned long startTime = millis();
         while(millis () - startTime <= delayTime) {}; // overkill really, but subtraction avoids timer overflow issue - worth knowing
         currentState != currentState; // toggle currentState between true/flase (high/low)
    }

     

    To change the interval, just change the value of delayTime on line 02.

     

    As an aside, and this won't impact this scenario where high accuracy isn't required and the duration is pretty low, the Arduino timer isn't very accurate over extended periods of time.

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

    I wondered whether to go into the wrap-around of the millis count, but decided not to (it was late at night when I did it and just testing the 3 minute interval seemed like an eternity; I certainly didn't feel like sitting there until the millis count wrapped round).

     

    Since an unsigned long, counting at millisecond intervals, takes 49.7 days to get to the fold around, it didn't seem like it would be too much of an issue for Sean in this particular case.

     

    Your line 11 isn't quite what you wanted.

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

    Yes, I forgot to delete line 11!  I only put in the 'wrap-around' bit because Sean said he was learning and it's worth knowing - as is knowing that the millis() timer isn't very accurate over longer time spans.

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

    No, I meant you'd need to use it to initialise your startTime variable by setting it to what millis() returns at that moment.

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

    Doh!  I see, yes.  I've updated the code.

    • 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