Momentary output from a latching input.

Hi,

I’m switching a LED with a latching on/off switch. What I’m trying to achieve is for the LED to blink for 1/2 a second each time the switch is switched to HIGH and then for the LED to switch off and remain off even while the switch remains HIGH. I thought I could achieve this with the delay() in the sketch below but obviously as I’ve discovered the LED remains HIGH while the switch is HIGH and the delay doesn’t apply until the switch is switched to LOW. I was wondering how ( if at all possible ) you would go about changing the output without changing the switches state. I realize this is probably a very simple problem but I’ve searched the web a can’t seem to find an answer. Any help would be greatly appreciated.

int led = 9;
int switchPin = 2;
int val;

void setup () {

pinMode (led, OUTPUT);
pinMode (switchPin, INPUT);
}

void loop () {

val = digitalRead (switchPin);

if ( val == HIGH) {
digitalWrite (led, HIGH);
delay (500);
digitalWrite (led, LOW);

} else {
digitalWrite (led, LOW); }}

sketch_dec29b.ino (309 Bytes)

Howdy,

I've modified the "Button" example built into Arduino. Also, in future, use code tags for posting code.

I've added buttonCount as a way to keep track of the state change. I nested an additional If/else statement to handle the led and buttonCount. The original else statement resets buttonCount once the switch no longer equals HIGH.

The delay is inserted in the nested else statement. Otherwise, if you release the switch before the delay finishes, the led will not turn off. This is because in order to reach the (ledPin, LOW), (buttonState = HIGH) must be true.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonCount = 0;         // variable for state change
  
void setup() {

  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     

}

void loop(){

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    
      if (buttonCount == 0) {      //added section to insure led turns off after 500ms, even if button is held. 
        buttonCount++;             //adds +1 to buttonCount so the if statement will be false on the next pass.
        digitalWrite(ledPin, HIGH);
      }
      else {
        delay(500);
        digitalWrite(ledPin, LOW); //turns off led
      }
  }
  else {
    buttonCount = 0;              //resets buttonCount once the switch is no longer HIGH
  }
}

Here is your modified code that should work.

int led = 9;
int switchPin = 2;
int val;
int buttonCount = 0;

void setup () {

pinMode (led, OUTPUT);
pinMode (switchPin, INPUT);
}

void loop () {

val = digitalRead (switchPin);

if ( val == HIGH) {
   if (buttonCount == 0){
     buttonCount++;
     digitalWrite (led, HIGH);
   }
   else {
     delay(500);
     digitalWrite(led, LOW);
   }
}
else {
   buttonCount = 0; 
}
}