Blink only 10 times

Hi Folks,

A buddy of mine wrote me this code to blink an LED with a Start and Stop push button. I was hoping to have this code so it only blinks 10 times when pressed start, rather than having it blink indefinitely, and stops after 10 blinks or when Stop is pressed. All help is appreciated!!
Here is what I have:

const int ledPin = 5;
const int buttonApin = 9;
const int buttonBpin = 8;
const unsigned long interval = 1000;
boolean Blinking = false;
boolean ledState = false;
unsigned long previousMillis = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}

void loop()
{
if (digitalRead(buttonApin) == LOW)
Blinking = true;

if (digitalRead(buttonBpin) == LOW)
Blinking = false;

if (Blinking)
{
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval)
{
  // save the last time you blinked the LED
  previousMillis = currentMillis;


  // if the LED is off turn it on and vice-versa:
  ledState = !ledState;


  // set the LED with the ledState of the variable:
  digitalWrite(ledPin, ledState);
}

} // end if (Blinking)
}

Hello wannabetechsavvyo

try and check this proposal:

const int ledPin = 5;
const int buttonApin = 9;
const int buttonBpin = 8;
const unsigned long interval = 1000;
boolean Blinking = false;
boolean ledState = false;
unsigned long previousMillis = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(buttonApin) == LOW)
    Blinking = true;
  static int blinkCount = 10;

  if (digitalRead(buttonBpin) == LOW)
    Blinking = false;

  if (Blinking)
  {
    unsigned long currentMillis = millis();


    if (currentMillis - previousMillis >= interval )
    {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      ledState = !ledState;

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);

      // decrement blink counter
      blinkCount--;
      if (blinkCount == 0) Blinking = false;
    }

  } // end if (Blinking)
}

Have a nice day and enjoy coding in C++.

Hi @paulpaulson,

I tried it and it seem to work the first time i press Start. But then if press start again the blinking is indefinite.

Hello wannabetechsavvyo

try this.

const int ledPin = 5;
const int buttonApin = 9;
const int buttonBpin = 8;
const unsigned long interval = 1000;
boolean Blinking = false;
boolean ledState = false;
unsigned long previousMillis = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(buttonApin) == LOW)
  {
    Blinking = true;
    static int blinkCount = 10;
  }

  if (digitalRead(buttonBpin) == LOW)
    Blinking = false;

  if (Blinking)
  {
    unsigned long currentMillis = millis();


    if (currentMillis - previousMillis >= interval )
    {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      ledState = !ledState;

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);

      // decrement blink counter
      blinkCount--;
      if (blinkCount == 0) Blinking = false;
    }

  } // end if (Blinking)
}
1 Like

just make Blink a counter

consider

const int ledPin = 5;
const int buttonApin = 9;
const int buttonBpin = 8;

const unsigned long interval = 1000;
int  Blinking;
boolean ledState = false;
unsigned long previousMillis = 0;

void setup ()
{
    pinMode (ledPin, OUTPUT);
    pinMode (buttonApin, INPUT_PULLUP);
    pinMode (buttonBpin, INPUT_PULLUP);
}


void loop ()
{
    if (digitalRead(buttonApin) == LOW)
        Blinking = 20;

    if (digitalRead(buttonBpin) == LOW)
        Blinking = 20;

    if (Blinking)
    {
        unsigned long currentMillis = millis ();

        if (currentMillis - previousMillis >= interval)
        {
            Blinking--;

            // save the last time you blinked the LED
            previousMillis = currentMillis;

            // if the LED is off turn it on and vice-versa:
            ledState = !ledState;

            // set the LED with the ledState of the variable:
            digitalWrite (ledPin, ledState);
        }
    } // end if (Blinking)
}
1 Like

Hi @gcjr

I tried the code you put here but the STOP button doesn't work anymore. when i press stop it again restarts the sequence.

Hi @paulpaulson
Tried the code you did too, but its functioning just as the original code i had. The only part it is missing is that it need to blink only 10 times and stop or whenever STOP is pressed.

sorry. i misunderstood what you wanted stop to do

just set Blink to 0 when the B pin is pressed

1 Like

Hi @gcjr
I did that and it works fine, just the way i thought of it to be. One quick question though, how do i make sure that after every time the LED blinks and stops after 10 blinks, it stops at a low? coz currently , when i press START the first time it blinks does stop blinking at HIGH and the once i press START again it it blinks does stop blinking at LOW. The third time it will end at a HIGH and so on

Add 1 to the count?

Post the code you are using

1 Like

you can set the starting and stopping states when the buttons are pressed

1 Like

Hi @pmagowan

if i add 1 to the count wouldn't it only change the subsequent iterations? I am little confused by that.

Here is the code I have so far with the help that I've gotten so far. Does 10 blinks but all the odd number iterations (one iteration is 10 blinks) ends in LED HIGH.

const int ledPin = 5;
const int buttonApin = 9;
const int buttonBpin = 8;

const unsigned long interval = 300;
int Blinking;
boolean ledState = false;
unsigned long previousMillis = 0;

void setup ()
{
pinMode (ledPin, OUTPUT);
pinMode (buttonApin, INPUT_PULLUP);
pinMode (buttonBpin, INPUT_PULLUP);
}

void loop ()
{
if (digitalRead(buttonApin) == LOW)
Blinking = 20;

if (digitalRead(buttonBpin) == LOW)
    Blinking = 0;

if (Blinking)
{
    unsigned long currentMillis = millis ();

    if (currentMillis - previousMillis >= interval)
    {
        Blinking--;

        // save the last time you blinked the LED
        previousMillis = currentMillis;

        // if the LED is off turn it on and vice-versa:
        ledState = !ledState;

        // set the LED with the ledState of the variable:
        digitalWrite (ledPin, ledState);
    }
} // end if (Blinking)

}

what is the pin initialized to?
think things over and figure out what needs to be done to meet your requirements

1 Like

Hi @gcjr

Think i got it how I wanted it to be. I randomly tried making Blinking = 19 instead of Blinking = 20 and it seem to work just fine.

Thanks a lot guys for all the help. Very much a beginner to programming, and I appreciate all the help and guidance that I got here.
Cheers!!

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