A question of Arduino timing

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.

Does Your delay-code do what You want?
If not, please post it here.

If all you're doing and all you'll ever do with this code is turn on an LED for 3-minutes there's nothing technically wrong with using a long blocking function like delay(). But you could also do this with, say, a 555 timer; you don't need all the MHz of processing cycles and peripherals and memory of an MCU for this task.

As soon as you might want to add other functions or features that you'd like to do while the LED is being timed you need to migrate to a non-blocking

Here's an example using a non-blocking technique:

 * Sketch:  sketch_oct26g
 * Target:  Uno, 2650 etc
const uint8_t pinButton = 2;
const uint8_t pinLED = LED_BUILTIN;

void setup() 
    pinMode( pinButton, INPUT_PULLUP ); //when pressed, button to ground pin
    pinMode( pinLED, OUTPUT );


void loop() 
    //handle the LED

    //...do other non-blocking stuff

void DoLED( void )
    //static variables stick around and retain their
    //value between calls
    static uint32_t
    static bool
        bLEDState = false;

    if( bLEDState == false )
        //LED is off now; has button been pressed?
        if( digitalRead( pinButton ) == LOW )
            //yes; get and save the current millis count
            timeStart = millis();
            //turn on the LED
            digitalWrite( pinLED, HIGH );
            //and indicate the LED is on
            bLEDState = true;
        //LED is on now; has 3-minutes elapsed?
        if( (millis() - timeStart) >= 180000ul )
            //yes; turn off the LED
            digitalWrite( pinLED, LOW );
            //and indicate it's off
            bLEDState = false;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.