start and stop intervall - please help!

the led-interval should start with the startbutton and stop with the stopbutton.
i know its nothing spectacular, but it doesn't work and i don't know why...

int ledPin = 13;
int startbuttonPin = 7;
int stopbuttonPin = 8;
int val = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(startbuttonPin, INPUT);
pinMode(stopbuttonPin, INPUT);

}

void loop()
{

val = digitalRead(startbuttonPin);
if (val == HIGH) {

interval();

}

else{

stopit();

}

val = digitalRead(stopbuttonPin);
if (val == HIGH) {

stopit();

}

else{

interval();

)

}

void stopit() {
digitalWrite(ledPin, LOW);
}

void interval() {

digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(5000);
digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(ledPin, LOW);
delay(5000);
digitalWrite(ledPin, HIGH);
delay(4000);
digitalWrite(ledPin, LOW);
delay(5000);
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
delay(5000);

}

If you are in the interval function you don't read the stop button. So, you can never stop the interval function. You can try to use interrupts, read about in the reference.

The interval function does not loop--it will exit. It just takes about 29 seconds.

Given that length of time monitoring buttons are problematic.

There are at least two solutions

  1. Use interrupts to toggle a state variable
  2. Rewrite the code to have a tighter loop around monitoring the key pins. To do this you could use the elapsed time functions to adjust the LED interval.

i'm really a newbie, so i have to ask again...

i want to use the interruption function from the reference:

attachInterrupt(interrupt, function, mode)

would it work when i insert the interruption between every blink of the led in the interval();?

and i don't really know what i should insert in the brackets of the interrupt function...

i'm really a newbie, so i have to ask again...

i want to use the interruption function from the reference:

attachInterrupt(interrupt, function, mode)

would it work when i insert the interruption between every blink of the led in the interval();?

and i don't really know what i should insert in the brackets of the interrupt function...

I'm no expert either, but I think the general idea is that you want to put whatever you want to be executed when the interrupt happens in the interrupt function. When the interrupt happens, the processor stops doing what it was doing, say in the middle of a delay(), and jumps over to do the stuff in interrupt() function.

So for example, you can setup a loop like this:

int flag = 1;

void loop() {

  while(flag == 1) { 
    turnonLED(); 
  }
}

interrupt() {
  flag = 0;
  turnoffLED();
}

When this runs, the LED will be kept on until the interrupt occurs, setting flag to 0 thus quitting the while loop and turning off the LED.

Not the best code example, but like I said, I'm no expert!

-Z-

You may also want to look at

which flashes an led without using delay - this can be adapted to do what you want