1

I have a problem I can't see to solve. I'm trying to program my Arduino, which I just got. I have read a lot of programming scenarios, but can't seem to find one that fits my problem.

I can get the LED to turn off and on like I want.

Assume a switch is pressed or even two wires to the LED are put together. Once the switch is pressed I'd like the LED to flash briefly, even though the LED has power to it. Is there a way to do this with Arduino code?

(1) Press switch (2) Flash LED briefly (3) Switch is still being pressed (4) LED remains off until switch is pressed again (5) loop through routine again.

  • Check for a state change instead of just the state. – Ignacio Vazquez-Abrams Jul 31 '16 at 16:32
  • 1
    "I'd like the LED to flash briefly, even though the LED has power to it." I'm guessing this is not what you really need. If you DO keep power to it, the LED will stay on. What I think you mean (correct me if I'm wrong) is that you want to power the LED briefly (the blink period) when the button is pressed and for however long it is pressed, until the button is released again. With an LED, power => light (as long as the power we're talking about is logic HIGH or logic LOW; not just any old source of power). – JRobert Aug 1 '16 at 23:54
1

This is a modified Example program that i have tested and seems to do what you expect. I have used a 2 position switch instead of a button. using a pointer "ledState" to indicate that the LED has flashed and have then combined 2 conditions with the "&&" in the IF statement.

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int ledState = 0;
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);        
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if ((buttonState == HIGH)&&(ledState == 0)) {
    // Flash LED:
    ledState = 1;
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
  } else if ((buttonState == LOW) && (ledState == 1)){
    ledState = 0;
    delay(50);
    }
}
  • You might want to add a bit of delay after ledState = 0; to prevent switch bounce from re-triggering. – Gerben Aug 1 '16 at 15:29
  • 1
    Yes, You are correct, I have edited my answer post to reflect that change...thanks. no more bounce. – Doug Aug 1 '16 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.