I have this section of code. It works fine and how I want it only, it executes when the button is released not when it is pressed. My application needs to execute this code when a switch is on. The issue is that it the vairable "fsrOnMillis" is updated constantly when the button is high. I have tried a few things but can't figure out the best way to get this to function how I want.
Hardware description: Switch when turned on will provide 5V. When off 0V. 2 Led's one green and one red.
What I want to happen is when the switch is on the green led lights up and turns off. Then after a time 60,000 ms the red LED will light up to say that the process is complete.
What is happening is that when the switch is turned on nothing happens but when it is turned off the code executes. I want the process to execute once the switch is turned on.
if (digitalRead(BUTTON) == HIGH) {
// update the time when button was pushed
fsrOnMillis = currentMillis;
greenReady = true;
redReady = true;
}
// make sure this code isn't checked until after button has been let go
if (greenReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - fsrOnMillis) >= greenOnDelay ) {
// okay, enough time has passed since the button was let go.
analogWrite(greenLedPin, 153);
// setup our next "state"
greenState = true;
// save when the greenLedPin turned on
greenTurnedOnAt = currentMillis;
// wait for next button press
greenReady = false;
}
}
// see if we are watching for the time to turn off greenLedPin
if (greenState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - greenTurnedOnAt) >= greenOffDelay) {
greenState = false;
analogWrite(greenLedPin, 0);
}
}
if (redReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - fsrOnMillis) >= redOnDelay) {
// okay, enough time has passed since the button was let go.
analogWrite(redLedPin, 153);
// setup our next "state"
redState = true;
// save when the greenLedPin turned on
redTurnedOnAt = currentMillis;
// wait for next button press
redReady = false;
}
}
// see if we are watching for the time to turn off greenLedPin
if (redState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - redTurnedOnAt) >= redOffDelay) {
redState = false;
analogWrite(redLedPin, 0);
}
}
Any help is much appreciated.