Go Down

Topic: "Go" Button (Read 829 times) previous topic - next topic

kersny

I need to put a button in my arduino that will pause all code until the button is pressed.
I need to not let the pin in the loop turn on, until a switch is pressed. After that, the switch doesn't matter at all.
Here is my code:
Code: [Select]

int pin = 9;
int ledPin = 13;
volatile int state = LOW;
volatile int Count = 0;
volatile int Dists = 65;

void setup()
{
 pinMode(pin, OUTPUT);
 pinMode(ledPin, OUTPUT);      // sets the digital pin as output
 attachInterrupt(1, Dist, FALLING);
}

void loop()
{
 digitalWrite(pin, HIGH);
}

void Dist()
{
 Count++;
 if (Count >= Dists)
 {
   digitalWrite(pin, LOW);
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(100000);    
 }
}

mem

#1
Mar 16, 2008, 05:22 pm Last Edit: Mar 16, 2008, 05:28 pm by mem Reason: 1
The following code added to setup should do what you want.

Code: [Select]

void setup()
{
 pinMode(pin, OUTPUT);
 pinMode(ledPin, OUTPUT);      // sets the digital pin as output

int swPin = 8;                          // set this to the switch pin  
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);  // turn on pull-up resistor
while(digitalRead(swPin) == HIGH)
    ;  // do nothing while the switch is not pressed
// code will get here when swPin is switched to ground

 attachInterrupt(1, Dist, FALLING);
}


Also, the delay of 100000 in the interrupt handler is not a good idea. Better to set a new variable to true when the interrupt occurs and do a digital read in loop() and delay if that variable is set to true.

kersny

#2
Mar 16, 2008, 07:13 pm Last Edit: Mar 16, 2008, 07:14 pm by kersny Reason: 1
This doesn't seem to be working for me. I apply power, wait ~ 10 sec. (I have an NG rev.c) and press the button, and nothing happens? All I added was a switch between pin 8 and ground. I didn't think I needed to pull it up since you used the internal pullups.

PS. I know the delay isn't ideal, but I will be turning the system off shortly after it stops, so I don't really worry about it.

mem

It works for me, but perhaps I don't quite understand what you want to do.  Try adding some code so you can see the LED responds to the switch press.

Code: [Select]
void setup()
{
 pinMode(pin, OUTPUT);
 pinMode(ledPin, OUTPUT);      // sets the digital pin as output

int swPin = 8;
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);  // turn on pull-up resistor
digitalWrite(ledPin,HIGH);  // turn on the LED so you know the code is waiting for the switch
while(digitalRead(swPin) == HIGH)
    ;  // do nothing while the switch is not pressed
// code will get will get here when swPin is switched to ground, so the LED will turn off
digitalWrite(ledPin, LOW);  

}



kersny

Nevermind, I fixed it. I did put an LED blink in there, but you forgot the brackets after the while statement. Its all good, and it works now. Thank you.

mem

#5
Mar 16, 2008, 08:14 pm Last Edit: Mar 16, 2008, 08:15 pm by mem Reason: 1
Quote
Nevermind, I fixed it. I did put an LED blink in there, but you forgot the brackets after the while statement. Its all good, and it works now. Thank you.

Good to hear its working.

BTW, the brackets were not forgotten, they are not needed in the code as posted. did you forget the semicolon after the while statement?  ;)

Go Up