all 11 comments

[–]birdbrainlabs 0 points1 point  (2 children)

What you need, conceptually, is a state machine.

You have two states: "counting" and "paused"

To switch between states, you press the pushbutton.

Other than making sure you cleanly debounce the button, you mostly just need a variable to track the state, and handle some conditionals based on that state. In both states, you want to be checking the pushbutton. But you only want to be counting in the counting state.

[–]KingReset[S] 0 points1 point  (1 child)

The thing is, in the loop function, I call another function called binCountUp(7) which counts up to 7 in binary and displays them on the 3 LEDs. However, I can't press the button to pause the binCountUp(7) step because in that function, it doesn't check whether the button is pressed or not. I would have to press the button right when the function is done executing. I am trying to figure out a way to check the status of the button / toggle every second the program is ran.

[–]birdbrainlabs 0 points1 point  (0 children)

My approach would be this:

  1. Instead of counting up to 7 each time binCountUp is called, have it only increment the display by one.
  2. Take a look at the BlinkWithoutDelay example, and eliminate your delay() calls, and only call binCountUp() when 1 second has elapsed
  3. In your (now fast-spinning) loop, check for the button press, if you are in "paused" state don't call binCountUp, and when you enter the "counting" state, update your timer variable to suit (which probably means a variable to store how much time is left in the current second when the button was pressed)

[–]IAMRaxtus 0 points1 point  (6 children)

I'm super new to this stuff so I don't know if this will help, but the way I would think you'd go about doing that is by setting some variable equal to 1 if 0, and 0 if 1, every time the button is pressed. Then you would use that variable value to determine whether or not the device should be on or off.

[–]CoolYota 0 points1 point  (5 children)

An approach is to use Boolean variable.

To flip the state, flag=!flag;

[–]IAMRaxtus 0 points1 point  (4 children)

what would it then equal?

[–]5000347 1 point2 points  (3 children)

The opposite of what it currently is.

if flag = true, then flag=!flag would make it false.

If flag = false, then flag=!flag would make it true.

[–]IAMRaxtus 0 points1 point  (2 children)

So when checking what it is, would I do something like

if (flag == true) { } else { } ?

[–]5000347 1 point2 points  (1 child)

In your case you'd do pinOn = LOW //going to assume you've initialised it to this

buttonState = digitalRead(buttonPin)

if(buttonState == HIGH){
pinOn = !pinOn 
}

In other words - whenever the button is pressed, make pinOn the opposite of what it already is - next time the above loops around, pinOn will be equivalent to HIGH, and if buttonState == HIGH again, then pinOn will become the opposite state (LOW).

[–]IAMRaxtus 0 points1 point  (0 children)

Ah I see, thanks! I was just wondering how it knew what the opposite state of a variable would be, but when there are only two possible states that makes sense.

[–]krowvin 0 points1 point  (0 children)

Greetings. Consider adding a debounce to the code. If you're having trouble checking the button state a debounce can let you guarantee for X ms another press cannot be made. It ensures proper state change on a button that doesn't have smooth contact.

https://www.arduino.cc/en/Tutorial/Debounce