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