Pages: [1]   Go Down
Author Topic: reading SPST button  (Read 407 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have this button from radioshack: http://www.radioshack.com/product/index.jsp?productId=2909787&locale=en_US

I want the LED to always be on, am powering it through the arduino.

I have it turning on a 5V relay (https://www.sparkfun.com/products/11042) and doing PWM on a separately powered LED array through a TIP120.

So you press the button,  relay goes on, a speaker makes some noise, a for() { conditional runs x amount of times (for say, 10 minutes) to make some LEDs fade in and out.

This is working just fine, just one problem.
If the button is pressed once, the relay turns on, lights are on, etc, all for 10 minutes as the LEDs do PWM in the for conditional.
Problem is, if the button is pressed again, one time, three times, five times, during the PWM in the for conditional, the whole program will start again because the button state will be different from before (this doesn't happen if the button is pressed an even amount of times making the button state the same as before)

So my question is, is how can I make it so no matter how many times someone presses the button while the lights are doing PWM the program will stop after running once? Here is my code:

Code:
//BUTTON CONFIG:
//1 - 5v in arduino (red)
//2 - 10k pull up resistor and to PIN 2 (orange)
//3 - GND (brown)

int speaker = 13;
int relay = 12;
int led = 10;
int button = 2;

int buttonState = 0;
int lastButtonState = 0;

int brightness = 0;
int fadeAmount = 3;

int debouncer = 0;

void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT);
  pinMode(speaker, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop() {
 
buttonState = digitalRead(button);
 
if (buttonState != lastButtonState) {
  delay(50);
  debouncer = 1;
}

if (debouncer == 1) {
  tone(speaker, 500, 1000);
  tone(speaker, 500, 500);
  delay(500);
  digitalWrite(relay, HIGH);

//write a loop that fades an array of 12V LEDs on tip120s for 15 min
//and meanwhile the button is deactivated

for (int i = 0; i<33; i++){ //was 33 for 10 sec...30k is 15min
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}
//turn off relay and wait for another push
  digitalWrite(relay, LOW);
  digitalWrite(led, LOW);
  debouncer = 0;
}

else {
debouncer = 0;
}

lastButtonState = buttonState;

}
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So you press the button,

Is the button held depressed or immediately released?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

During the for() loop and the delay(500), the Arduino isn't giving any attention to your button.  It doesn't matter if you press it an odd number or even number of times.

Are you using an external pull-up (or pull-down) resistor?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Full Member
***
Karma: 1
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i click the button, press it, it goes in. it's an spst button, so it's either high or low.

not sure about the resistor...it's a 10k resistor going to ground on the same connection where the button goes into the arduino.

i changed some of the code, but one more logic type switch "int ignore=0;" and everything seems to be working great now.

Code:
//BUTTON CONFIG:
//1 - 5v in arduino (red)
//2 - 10k pull up resistor and to PIN 2 (orange)
//3 - GND (brown)

int speaker = 13;
int relay = 12;
int led = 10;
int button = 2;
int ignore = 0;

int buttonState = 0;
int lastButtonState = 0;

int brightness = 0;
int fadeAmount = 3;

int debouncer = 0;

void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT);
  pinMode(speaker, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop() {
 
buttonState = digitalRead(button);
 
if ((buttonState != lastButtonState)&&(ignore == 1)) {
  delay(50);
  debouncer = 1;
}

if (debouncer == 1) {
  tone(speaker, 500, 1000);
  tone(speaker, 500, 500);
  delay(500);
  digitalWrite(relay, HIGH);

//write a loop that fades an array of 12V LEDs on tip120s for 15 min
//and meanwhile the button is deactivated

for (int i = 0; i<30000; i++){ //was 333 for 10 sec...30k is 15min
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}
//turn off relay and wait for another push
  digitalWrite(relay, LOW);
  digitalWrite(led, LOW);
  debouncer = 0;
  ignore = 0;
}

else {
debouncer = 0;
ignore = 1;
}

lastButtonState = buttonState;

}
Logged

Pages: [1]   Go Up
Jump to: