Arcade Button On/Off Trouble

Hi All – I’m sure my problem will be a simple matter for most of you but I’m pretty new to Arduino. I am designing a spaceship control panel and am having some trouble with my Arcade buttons that need to be lit when pressed the first time, and then not lit when pressed again. I am trying to squeeze as many of these on to one R3 as possible and plan to have 8 total. Right now I have it wired incorrectly and when I continually press the button goes off but does not remain off. I’ve included a picture of what I’m attempting to accomplish. I have no code to this point and really don’t know where to look as the buttons I’m using don’t seem to have a lot of resources. I’ve been trying to look this up for the past few hours with little luck. Please let me know if you have any suggestions or know of a tutorial to send my way?



In the Arduino's IDE their is an example called state change detection. This is used to detect when a button becomes pressed not when it is being pressed. You can use this to count how many times a button has been pressed. Now if this is an odd number you want to light up the LED in your switch and if it is an even number you need to turn off the LED. Check if a number is even by looking at the least significant bit of the number by removing the rest of the number by masking it with a bitwise AND statement. The symbol for this is &

So you might do

if((count & 1) == 0) {
// put code here to light up the LED 
else {
// put code here to turn off the LED 

You need to debounce the button as well. This is the code I use:

#include "PushButton.h"

const uint8_t buttonPin = 2;
const uint8_t ledPin = LED_BUILTIN;

PushButton button = {buttonPin}; // a debounced button object that detects falling edges

bool ledState = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {
  if (button.isPressed()) { // on a falling edge of the button (if it's pressed)
    ledState = !ledState; // invert the state of the LED
    digitalWrite(ledPin, ledState); // write the state to the output

class PushButton
    PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
      : pin(pin) { // remember the push button pin
      pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
    bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
      bool pressed = false;
      bool state = digitalRead(pin);               // read the button's state
      int8_t stateChange = state - previousState;  // calculate the state change since last time

      if (stateChange == falling) { // If the button is pressed (went from high to low)
        if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
          pressed = true; // the button is pressed
      if (stateChange == rising) { // if the button is released or bounces
        previousBounceTime = millis(); // remember when this happened

      previousState = state; // remember the current state
      return pressed; // return true if the button was pressed and didn't bounce
    uint8_t pin;
    bool previousState = HIGH;
    unsigned long previousBounceTime = 0;

    const static unsigned int debounceTime = 25;
    const static int8_t rising = HIGH - LOW;
    const static int8_t falling = LOW - HIGH;

#endif // PUSHBUTTON_H_


Thank you all so much! This is exactly what I needed. You guys rock!