Replicating button tutorial for multiple buttons

Hi guys,

Hoping to get some help as I’m struggling to get my button presses to register.
I have already followed the advice I found in this thread: https://forum.arduino.cc/index.php?topic=357607.0 but it doesn’t look like my button presses are registering.

I’m assuming I’ve made a rookie error with my wiring but I’m struggling to see it:
(Schematic attached)

Code driving it:

// arenaState:
// 0: Arena Disarmed
// 1: Standby
// 2: Fight in Progress
int arenaState = 0;
int imobilizedCounter = 0;

const int buttonStandbyPin = 2;
int buttonStandbyState = 1;

const int buttonArmedPin = 3;
int buttonArmedState = 1;

const int buttonCeasePin = 4;
int buttonCeaseState = 1;

const int buttonCounterPin = 5;
int buttonCounterState = 1;

const int ledDisarmedPin = 13;
const int ledStandbyPin = 12;
const int ledArmedPin = 11;

long currTime = 0L;

long arenaTimerStart = 0L;
long countTimerStart = 0L;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Arena ready");
  
  pinMode(ledDisarmedPin, OUTPUT);
  pinMode(ledStandbyPin, OUTPUT);
  pinMode(ledArmedPin, OUTPUT);
  pinMode(buttonStandbyPin, INPUT_PULLUP);
  pinMode(buttonArmedPin, INPUT_PULLUP);
  pinMode(buttonCeasePin, INPUT_PULLUP);
  pinMode(buttonCounterPin, INPUT_PULLUP);
}

void loop() {
  // read the state of the pushbutton values:
  // NOTE: As we are using INPUT_PULLUP button press is LOW not HIGH
  buttonStandbyState = digitalRead(buttonStandbyPin);
  buttonArmedState = digitalRead(buttonArmedPin);
  buttonCeaseState = digitalRead(buttonCeasePin);
  buttonCounterState = digitalRead(buttonCounterPin);

  if (buttonStandbyState == LOW) {
    arenaState = 1;
    Serial.println("Standby Button pressed");
  }

  if (buttonArmedState == LOW and arenaState == 1) {
    arenaState = 2;
    // start 3min countdown
    arenaTimerStart = currTime;
    Serial.println("Start Button pressed");
  }

  if (buttonCeaseState == LOW and arenaState >= 1) {
    arenaState = 0;
    Serial.println("Cease Button pressed");
  }  

  if (buttonCounterState == LOW and arenaState >= 1) {
    if (imobilizedCounter == 1) {
      imobilizedCounter = 0;
    } else {
      countTimerStart = currTime;
      imobilizedCounter = 1;
    }
    Serial.println("Imobilised Button pressed");
  }  

  switch (arenaState) {
    case 0:
      digitalWrite(ledDisarmedPin, HIGH);
      digitalWrite(ledStandbyPin, LOW);
      digitalWrite(ledArmedPin, LOW);
      break;
    case 1:
      digitalWrite(ledDisarmedPin, LOW);
      digitalWrite(ledStandbyPin, HIGH);
      digitalWrite(ledArmedPin, LOW);
      break;
    case 2:
      digitalWrite(ledDisarmedPin, LOW);
      digitalWrite(ledStandbyPin, LOW);
      digitalWrite(ledArmedPin, HIGH);
  
      // Check to see if 10sec are up
      if(imobilizedCounter == 1) {
          if(currTime >= arenaTimerStart + (10*1000L)) {
            arenaState = 0;
        }
      }
  
      // Check to see if 3min are up
      if(currTime >= arenaTimerStart + ((3*60)*1000L)) {
          arenaState = 0;
      }
      break;
    default:
      digitalWrite(ledDisarmedPin, HIGH);
      digitalWrite(ledStandbyPin, LOW);
      digitalWrite(ledArmedPin, LOW);
  }
  currTime++;
}

Note: I know my timing code is silly and needs an RTC but I don’t think it’s having an impact on my current issue.

OP fritzing

Show us a good image of your actual wiring.
Your breadboard may have split power rails.

You were right - there are splits in the side rails, however moving wiring into a contiguous section appears to have had no effect. (Based on checking the serial output)

eta: Replaced GD link with IMG as requested.

If you want help, show us your actual wiring.

Attach a good image to your post.

See:

If you want to use multiple buttons (with edge detection and debounce), the easiest approach is to use an object-oriented approach. In the following example, a PushButton class is defined that debounces the button. You can simply use PushButton::isPressed() to detect all falling edges.

Code:[hr][/td][/tr][tr][td][size=9pt][tt][color=#00979c]class[/color] [color=#000000]PushButton[/color] [color=#000000]{[/color]
  [color=#00979c]public[/color][color=#434f54]:[/color]
    [color=#000000]PushButton[/color][color=#000000]([/color][color=#00979c]uint8_t[/color] [color=#d35400]pin[/color][color=#000000])[/color] [color=#434f54]// Constructor (executes when a PushButton object is created)[/color]
      [color=#434f54]:[/color] [color=#d35400]pin[/color][color=#000000]([/color][color=#d35400]pin[/color][color=#000000])[/color] [color=#000000]{[/color] [color=#434f54]// remember the push button pin[/color]
      [color=#d35400]pinMode[/color][color=#000000]([/color][color=#d35400]pin[/color][color=#434f54],[/color] [color=#00979c]INPUT_PULLUP[/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// enable the internal pull-up resistor[/color]
    [color=#000000]}[/color][color=#000000];[/color]
    [color=#00979c]bool[/color] [color=#d35400]isPressed[/color][color=#000000]([/color][color=#000000])[/color] [color=#434f54]// read the button state check if the button has been pressed, debounce the button as well[/color]
    [color=#000000]{[/color]
      [color=#00979c]bool[/color] [color=#000000]pressed[/color] [color=#434f54]=[/color] [color=#00979c]false[/color][color=#000000];[/color]
      [color=#00979c]bool[/color] [color=#000000]state[/color] [color=#434f54]=[/color] [color=#d35400]digitalRead[/color][color=#000000]([/color][color=#d35400]pin[/color][color=#000000])[/color][color=#000000];[/color]               [color=#434f54]// read the button's state[/color]
      [color=#00979c]int8_t[/color] [color=#000000]stateChange[/color] [color=#434f54]=[/color] [color=#000000]state[/color] [color=#434f54]-[/color] [color=#000000]previousState[/color][color=#000000];[/color]  [color=#434f54]// calculate the state change since last time[/color]

      [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]stateChange[/color] [color=#434f54]==[/color] [color=#000000]falling[/color][color=#000000])[/color] [color=#000000]{[/color] [color=#434f54]// If the button is pressed (went from high to low)[/color]
        [color=#5e6d03]if[/color] [color=#000000]([/color][color=#d35400]millis[/color][color=#000000]([/color][color=#000000])[/color] [color=#434f54]-[/color] [color=#000000]previousBounceTime[/color] [color=#434f54]>[/color] [color=#000000]debounceTime[/color][color=#000000])[/color] [color=#000000]{[/color] [color=#434f54]// check if the time since the last bounce is higher than the threshold[/color]
          [color=#000000]pressed[/color] [color=#434f54]=[/color] [color=#00979c]true[/color][color=#000000];[/color] [color=#434f54]// the button is pressed[/color]
        [color=#000000]}[/color]
      [color=#000000]}[/color]
      [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]stateChange[/color] [color=#434f54]==[/color] [color=#000000]rising[/color][color=#000000])[/color] [color=#000000]{[/color] [color=#434f54]// if the button is released or bounces[/color]
        [color=#000000]previousBounceTime[/color] [color=#434f54]=[/color] [color=#d35400]millis[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// remember when this happened[/color]
      [color=#000000]}[/color]

      [color=#000000]previousState[/color] [color=#434f54]=[/color] [color=#000000]state[/color][color=#000000];[/color] [color=#434f54]// remember the current state[/color]
      [color=#5e6d03]return[/color] [color=#000000]pressed[/color][color=#000000];[/color] [color=#434f54]// return true if the button was pressed and didn't bounce[/color]
    [color=#000000]}[/color][color=#000000];[/color]
  [color=#00979c]private[/color][color=#434f54]:[/color]
    [color=#00979c]uint8_t[/color] [color=#d35400]pin[/color][color=#000000];[/color]
    [color=#00979c]bool[/color] [color=#000000]previousState[/color] [color=#434f54]=[/color] [color=#00979c]HIGH[/color][color=#000000];[/color]
    [color=#00979c]unsigned[/color] [color=#00979c]long[/color] [color=#000000]previousBounceTime[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color]

    [color=#00979c]const[/color] [color=#00979c]static[/color] [color=#00979c]unsigned[/color] [color=#00979c]long[/color] [color=#000000]debounceTime[/color] [color=#434f54]=[/color] [color=#000000]25[/color][color=#000000];[/color]
    [color=#00979c]const[/color] [color=#00979c]static[/color] [color=#00979c]int8_t[/color] [color=#000000]rising[/color] [color=#434f54]=[/color] [color=#00979c]HIGH[/color] [color=#434f54]-[/color] [color=#00979c]LOW[/color][color=#000000];[/color]
    [color=#00979c]const[/color] [color=#00979c]static[/color] [color=#00979c]int8_t[/color] [color=#000000]falling[/color] [color=#434f54]=[/color] [color=#00979c]LOW[/color] [color=#434f54]-[/color] [color=#00979c]HIGH[/color][color=#000000];[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#434f54]// -------------------------------------------------------------------------------------------------------------------------------- //[/color]

[color=#000000]PushButton[/color] [color=#000000]button[/color] [color=#434f54]=[/color] [color=#000000]{[/color] [color=#000000]2[/color] [color=#000000]}[/color][color=#000000];[/color]  [color=#434f54]// Create a new PushButton object on pin 2[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#d35400]pinMode[/color][color=#000000]([/color][color=#00979c]LED_BUILTIN[/color][color=#434f54],[/color] [color=#00979c]OUTPUT[/color][color=#000000])[/color][color=#000000];[/color]  [color=#434f54]// Set the built-in LED (pin 13) to output mode[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#00979c]static[/color] [color=#00979c]bool[/color] [color=#000000]LEDstate[/color] [color=#434f54]=[/color] [color=#00979c]LOW[/color][color=#000000];[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]button[/color][color=#434f54].[/color][color=#d35400]isPressed[/color][color=#000000]([/color][color=#000000])[/color][color=#000000])[/color] [color=#000000]{[/color]  [color=#434f54]// If the button is pressed[/color]
    [color=#000000]LEDstate[/color] [color=#434f54]=[/color] [color=#434f54]![/color][color=#000000]LEDstate[/color][color=#000000];[/color]  [color=#434f54]// Flip the state of the LED[/color]
    [color=#d35400]digitalWrite[/color][color=#000000]([/color][color=#00979c]LED_BUILTIN[/color][color=#434f54],[/color] [color=#000000]LEDstate[/color][color=#000000])[/color][color=#000000];[/color]  [color=#434f54]// Write the new state to the LED[/color]
  [color=#000000]}[/color]
[color=#000000]}[/color]

[/size]

Pieter

Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

I know how to use code tags. But it doesn't allow you to use syntax highlighting. Using the syntax highlighting of the IDE makes it a lot more readable, IMHO.

That image is washed out, cannot make things out.

Try to see if you can get the 'Debounce' sketch in the IDE examples to work.

Tried PieterP's code and that didn't work either which confirms my suspicion that there is something funky with my wiring.

I'll pull it apart later and see if I can make the debounce example work.

You may have some bad wires.