Pages: [1]   Go Down
Author Topic: "Go" Button  (Read 690 times)
0 Members and 1 Guest are viewing this topic.
Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 20
I built a Segway...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following code added to setup should do what you want.

Code:

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.
« Last Edit: March 16, 2008, 11:28:16 am by mem » Logged

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 20
I built a Segway...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 16, 2008, 01:14:58 pm by kersny » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}

Logged

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 20
I built a Segway...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  smiley-wink
« Last Edit: March 16, 2008, 02:15:46 pm by mem » Logged

Pages: [1]   Go Up
Jump to: