1

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.

3
  • your problem makes no sense ... if you can detect the button in the pressed state, then you can detect the button in the released state just as easily .... is there something that you forgot to tell us? – jsotola Dec 5 '19 at 20:46
  • You said in the question exactly what the problem is. Take that line out. Move it to where it only happens when the switch is off. I don’t understand what the hold up is. – Delta_G May 2 '20 at 21:18
  • Oh I see, you want to update that time once when the button first BECOMES pressed, not constantly as long as it IS pressed. Go have a look at the State Change Example in the IDE. It shows you how to use a variable to keep up with the last state so you can react only when the state changes. – Delta_G May 2 '20 at 21:28
0

You should provide a description of your hardware setup as part of your question, as well as a clearer statement of your problem.

Are you saying that your code is backwards, and is acting like the button is pressed when its not, and like it's been released when you press it?

You probably have your button set up as INPUT_PULLUP, and wired to connect the pin to ground. That's a pretty typical setup. In that case the button pin reads as LOW when it's pressed and HIGH when it's not pressed. Just changed your if statement to:

//                         vvv
if (digitalRead(BUTTON) == LOW)
//                         ^^^
1
  • Hi Duncan, thanks for the reply. No, I have a switch when the switch is on there is 5V signal going to the a Digital input on the arduino. What I would like to happen is the code waits for the switch to be turned on, light the green LED, turn off the green led after a delay. Then the Red LED will turn on and off again at a later time. What is happening is nothing when the switch is off, when it is turned on nothing happens but once it is off again the code executes. – Daytona675 Dec 4 '19 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.