reading SPST button

Hello,

I have this button from radioshack: RadioShack.com Official Site - America's Technology Store

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

I have it turning on a 5V relay (SparkFun Beefcake Relay Control Kit (Ver. 2.0) - KIT-13815 - SparkFun Electronics) 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:

//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;

}

So you press the button,

Is the button held depressed or immediately released?

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?

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.

//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;

}