Just learning and need a little help

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 button

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

The first bit works OK but I am having problems reliably turning it off again.

Sure, because once it starts blinking, you only check the state of the off switch every 2 seconds. You would need to hold the switch down longer than that to be certain that it was recognized.

Now that you have gotten this far, it's time to look at the blink without delay example, and NEVER use delay() again. Get over this hurdle, and you are home free.

Thanks PaulS

Great, I will go through that example and have a play. I will let you know how I get on. I'm sure I will have more questions.

It is little overwhelming starting something like this having very little electronics or programming knowledge

Thanks again

skarekrow