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 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);
}
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)
}
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)
}
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)
}
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.
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
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;
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)