0

So basically I'm trying to create a device where when I push a button it starts a loop. When I push this button again, it stops the loop. Right now I have it logged to serial. It starts up correctly with no output. When I press the button the loop begins and it spits out "toggled on" continuously. When I press the button again... nothing happens. What should happen ideally is that it should stop spitting out "toggled on" and just go back to doing nothing.

#include <Bounce2.h>

Bounce bouncer = Bounce();
const int W = 10;
bool wOn = false;

void setup() {
  Serial.begin(9600);
  pinMode(W, INPUT_PULLUP);
  bouncer.attach(W);
  bouncer.interval(5);
}

void loop() {
  bouncer.update();
  if (bouncer.read() == LOW) {
    wOn = !wOn;
  }

  if (wOn > 0) {
    Serial.println("toggled on");
  }
}
1

If you change your sketch to this one, you'll see the issue:

const int W = 10;
bool wOn = false;

void setup() {
  Serial.begin(9600);
  pinMode(W, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(W) == LOW) {
    wOn = !wOn;
  }

  if (wOn) {
    //Serial.println("toggled on");
  }
  Serial.println(wOn);
}

The button's contacts "bounce" (Google "contact bounce"), sending multiple low to high signals. There are several software solutions that will eliminate this issue. Have a look at: arduino.cc/en/Tutorial/Debounce. There are a couple of libraries that simplify de-bouncing buttons: playground.arduino.cc/Code/Debounce and playground.arduino.cc/Code/Bounce

  • I've added Bounce2 (I believe correctly) and it still has the same result. I added that bit to see the wOn value and it changes correctly between 0 and 1. Still the same end result. I've updated my code snippet to reflect my changes. – C Steph Jun 29 '18 at 19:36
  • It seems that it only changes wOn back to false when I click it then instantly goes back to 1. The problem seems to be that it's infinitely changing the wOn back. Maybe I need to account for state changes? – C Steph Jun 29 '18 at 19:41
-1

Fixed. The solution I used was to add state change checking using .fell() which you'll see reflected below. I added state change verification using Bounce2.fell(), like so:

  if (bouncer.read() == LOW && bouncer.fell()) {
  • 1
    Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review – VE7JRO Jun 29 '18 at 20:26
  • 1
    What I'm suggesting @C Steph is to edit your question and put the original, non-functioning code back. Then you could edit your answer and use your complete, functional sketch with the Bounce2 library. Your current answer is only 7 words long, so it's incomplete. In approximately 24 hours, you will be able to accept your own answer and I encourage you to do so. This will give you addition reputation on the Arduino.SE network. – VE7JRO Jun 29 '18 at 20:50
  • If you edit your question with the fix, then the answers (and your question) will not make any sense. @VE7JRO - I've have done it for the OP. – Greenonline Jun 29 '18 at 21:08
  • I was looking for, Question = broken code, Answer = completely fixed and working code. There were 2 edits by the OP to their original Question. I was hoping the OP would have changed the Question back to the sketch they first posted (it did not use the Bounce2 library and was identical to the sketch in my answer, minus the code comment and extra Serial.println I added). Is there any way to go back through the question editing process to restore the first post by the OP? – VE7JRO Jun 29 '18 at 23:04
  • I just tried to edit the question and was not able to go back to the OP's first post. More than likely, my reputation level is not high enough to access the information or only a site moderator can make these types of changes. – VE7JRO Jun 29 '18 at 23:30

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.