it works perfectly except when i upload or restart the UNO…the button LED blinks Really fast…like almost on solid…once i press the button, it triggers and fades fast, and then slow after the time is up…any thoughts??
blh64:
You need to do your timing and detect button state changes
const int Blink_Button = 2;
const int Smash_Trigger_Button = 3;
const int Button_LED = 9;
const int Smash_Trigger = 13;
int Button_LED_State;
int Previous_Button_LED_State;
unsigned long Time, startTime;
const unsigned long fastDuration = 5000;
int Count;
const int Fade_Amount = 5;
int Brightness = 0;
void setup() {
pinMode (Blink_Button, INPUT_PULLUP);
pinMode (Smash_Trigger_Button, INPUT_PULLUP);
pinMode (Smash_Trigger, OUTPUT);
pinMode (Button_LED, OUTPUT);
Previous_Button_LED_State = digitalRead(Blink_Button);
startTime = 0;
}
void loop() {
Button_LED_State = digitalRead(Blink_Button);
if ( Button_LED_State != Previous_Button_LED_State ) {
// we have a state change
if ( Button_LED_State == HIGH ) {
// button was just pressed, increase fade speed and trigger sound board
Time = 5;
Trigger();
startTime = millis();
}
else {
// button was released, do nothing
}
delay(50); // debounce
Previous_Button_LED_State = Button_LED_State;
}
Fade();
// check to see if we are doing a fast fade and if so,
// turn it off if enough time has elapsed
// startTime == 0 means we are not doing fast fade
if ( startTime && millis() - startTime >= fastDuration ) {
startTime = 0; // turn it off
Time = 30; // slow fade
}
}
void Fade() {
static unsigned long Previous_Millis = 0;
unsigned long Current_Millis = millis();
if (Current_Millis - Previous_Millis >= Time) {
Previous_Millis = Current_Millis;
analogWrite(Button_LED, Brightness);
// change the brightness for next time through the loop:
Brightness += Fade_Amount;
// reverse the direction of the fading at the ends of the fade:
if (Brightness <= 0 || Brightness >= 255) {
Fade_Amount = - Fade_Amount;
}
}
}
void Trigger() {
digitalWrite (Smash_Trigger, HIGH);
delay (100);
digitalWrite (Smash_Trigger, LOW);
}