

You also need to make sure your two if statements are mutually exclusive i.e. On every iteration of the main loop, only respond to the button if it's currently HIGH, and if it was LOW last time round the loop. What you need to do is store the last known state of the button. The result is that the output pin will be quickly getting turned on and off all the time. buttonState will appear HIGH every time round loop, even if the button has been held down for several seconds. The second problem is that you're not monitoring the previous state of the button. This is because the first if statement sets x to 0, which is part of the condition the second if statement looks for. Firstly, both your if statements are being triggered one after the other. These internal pull-up resistors make it easy to connect a pin to ground through a button or switch.At the moment, you've got two main problems. Pull-up resistor circuits include using the internal pull-up resistors in an Arduino. With a pull-up resistor circuit, the signal is “pulled high” (to +ve) when the button is open and low (0v) when closed.
ARDUINO BUTTON HOW TO
See how to wire and program a button for a pulldown resistor circuit.
ARDUINO BUTTON CODE
When closed (pressed), the button signal will go “high”. The following code reads the button state, then turns the onboard LED either on or off, based on if the button was pushed or not. With a pulldown resistor circuit, the signal is “pulled down” to ground (0v) when the button is open (not pressed). Ideally use the same (pulldown or pull-up) resistor circuit for all buttons. Whether your circuits use pull-up or pulldown resistors is chosen at compile time. Resistor CircuitsĪll AbleButtons (including the direct button) support pull-up and pulldown resistor circuits.

For example, the AblePulldownCallbackClickerButton and AblePullupCallbackClickerButton types combine “callback” and “clicker” capabilities if required. A ButtonList::button(id) method retrieves the button based on the id provided. In addition to that, proper debouncing should be used As a proof of concept, the interrupt function should just toggle the BUILTINLED whenever the button is pressed.

Therefore, the pin is turned to LOW whenever the button is pressed. This id allows a shared callback function to differentiate between each button. The button is connected to pin 2 and the GND. Your callback function also receives the id of the button.
