Pages: [1]   Go Down
Author Topic: start and stop intervall - please help!  (Read 560 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Shh! I am hunting wabbit. Killlll the wabbit!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:

Code:

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-
« Last Edit: August 06, 2007, 05:41:53 pm by zitron » Logged

london
Offline Offline
Full Member
***
Karma: 0
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may also want to look at

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

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

Pages: [1]   Go Up
Jump to: