Hi All
I am using the Getting started with arduino book and I am really enjoying the learning process but I am a little stuck.
I learned how to make a LED blink and then when on to using a push button to turn a LED on and off but then I decided to go off piste a little and combine the two. I want to push the button and the LED starts blinking then press the button again and turn it off.
The first bit works OK but I am having problems reliably turning it off again.
Any help would be appreciated
//Turning on a flashing LED
#define LED 13 //LED in pin 13
#define BUTTON 7 // input pin connected to buttonint val =0; // val will be used to store the state of the input pin
int old_val = 0; //this variable stores the previous value of "val"
int state = 0; //0 = LED off, 1 = LED on
void setup(){
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT); // Tell arduino BUTTON is an INPUT
}void loop(){
val = digitalRead(BUTTON); // Read input value and store it//Check if there was a transition
if ((val == HIGH) && (old_val == LOW)) {
state = 1 - state;
delay(200);
}old_val = val; //val is now old, lat's store it
if (state == 1) {
digitalWrite (LED, HIGH);
delay(1000);
digitalWrite (LED, LOW);
delay(1000);
} else {
digitalWrite(LED, LOW);
}
}