Blinking LED for 10 sec when pushbutton pressed

I want to blink a led for 10 sec if pushbutton was pressed once or an odd numer of times (I haven't include it in this code yet idk how to do this).

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do uploading code to the Arduino

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

A millis-timer often runs on its own in the main section of the loop() function.
You will have two pieces of code. A few code lines to start the millis-timer and a few code lines of the millis-timer itself.

What kind of solution do you like ?

  1. Using two millis-timers. One for blinking, and one for turning it off after 10 seconds. Do you want to turn it off abrupt or after a blink has finished ?
  2. Using one millis-timer. Blink the led and count the blinks. Calculate how many blinks are needed for 10 seconds and stop if that amount is reached.

The option that sounds the most simple to you, that option has code that is the easiest to understand.

The Arduino IDE has a text formatting option. It is in the menu and Arduino IDE 2.0 has it also with a right-click. Please use it and please format the text of your code. Put every indent, every space, every comma, every new line, and so on, at the right place.

consider

#define MyHW
#ifdef MyHW
int buttonPin = A1;

// -------------------------------------
#else
int LEDpin    = 9;
int buttonPin = 2;
#endif

// ---------------------------------------------------------
int buttonState = 0;

enum { Off = HIGH, On = LOW };

struct Tmr {
    byte            Pin;
    unsigned long   OnPeriod;
    unsigned long   OffPeriod;
    const int       Ncnt;
    const char *    desc;

    bool            active;
    int             cnt;
    unsigned long   msecLst;
    unsigned long   msecPeriod;
};

Tmr tmrs [] = {
    { 13, 100,   200,  33,  "Tmr0" },
};
#define Ntmr    (sizeof(tmrs)/sizeof(Tmr))

unsigned long msec;

// -------------------------------------
void
tmrOff (
    Tmr *t )
{
    Serial.print   ("tmrOff: ");
    Serial.println (t->desc);

    t->active     = true;
    digitalWrite (t->Pin, Off);
}

// -------------------------------------
void
tmrOn (
    Tmr *t )
{
    Serial.print   ("tmrOn: ");
    Serial.println (t->desc);

    t->active     = true;
    t->msecLst    = msec;
    t->msecPeriod = 0;        // force immediate update
    t->cnt        = t->Ncnt;
}

// -----------------------------------------------------------------------------
void loop ()
{
    // service tmrs
    msec = millis ();

    Tmr *t = tmrs;
    for (unsigned n = 0; n < Ntmr; n++, t++)  {
        if (! t->active)
            continue;

        if ((msec - t->msecLst) >= t->msecPeriod)  {
            if (0 == t->cnt--)
                tmrOff (t);
            else  {
                t->msecLst = msec;
                if (On == digitalRead (t->Pin))  {
                    digitalWrite (t->Pin, Off);
                    t->msecPeriod = t->OffPeriod;
                }
                else  {
                    digitalWrite (t->Pin, On);
                    t->msecPeriod = t->OnPeriod;
                }
            }
        }
    }
    
    // check for button press
    byte but  = digitalRead (buttonPin);
    if (buttonState != but)  {      // state change
        buttonState = but;
        delay (20);                 // debounce

        if (LOW == but)  {          // pressed
            tmrOn (& tmrs [0]);
        }

    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (buttonPin, INPUT_PULLUP);

    for (unsigned n = 0; n < Ntmr; n++)  {
        pinMode      (tmrs [n].Pin, OUTPUT);
        digitalWrite (tmrs [n].Pin, Off);
    }
}

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