Combining Codes - LED with pushbutton and timer interrupt

Hi,

I'm really new at this, so any advice on this would be greatly appreciated. Ideally, I should be able to push the button down once, and have the LED turn on for half a second. To do this, I believe that I will need to combine both the pushbutton code and a timer interrupt (probably Timer1). I've also been looking into Metro, but it makes no sense to me.

Here's the pushbutton code:

int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}

void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON
}
}

All the timer 1 codes are available online. Here's a couple links:

http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

Thanks!

No need for a timer interrupt for such a long delay.
Have a look at the blink without delay example in the IDE.

Thanks!

Ideally, I should be able to push the button down once, and have the LED turn on for half a second.

The state change detection example also bears looking at. It shows how to do stuff when a switch BECOMES pressed (or released) rather than when the switch IS pressed or released.

Not really sure how to incorporate that part into the code. Also, I've added two more LEDs to this. This is what I've got right now:

int ledPin = 12;
int ledPin2 = 11;
int ledPin3 = 10;
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(inPin, INPUT);
}

void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED OFF
delay(50);
} else {
digitalWrite(ledPin, LOW); // turn LED ON
delay(100);
}

if (val == HIGH) {
digitalWrite(ledPin2, HIGH); // turn LED OFF
delay(50);
} else {
digitalWrite(ledPin2, LOW); // turn LED ON
delay(100);
}

if (val == HIGH) {
digitalWrite(ledPin3, HIGH); // turn LED OFF
delay(50);
} else{
digitalWrite(ledPin3, LOW); // turn LED ON
delay(100);
}
}

It works…some of the time. Any suggestions?

Any suggestions?

The usual - get rid of calls to "delay()" and look at the blink-without-delay example.

Thanks so much! I thought I would need the delay to regulate the time that the LED was lit, but I guess not. When this works, it works perfectly. However, it's not consistently working. At this point, I think it's probably just wiring and not programming. Thanks for all your help! :slight_smile:

This demo of several things at a time might give you some ideas.

...R

However, it's not consistently working

150ms is a long time to miss a short switch operation.

What is stopping your inputs from floating?