Buttons' clicks are not registred

Hello. I am trying to make a simple Keyboard & Mouse HID device, so i started wiring and seting up buttons breadboard.

here is my setup:


I hope circuit is visible from the photo

Here is my code:

#include <Bounce2.h>

#define btn_count 8
#define encoderSw 13
#define encoderDt 12
#define encoderClk 11
Bounce btns[btn_count] = {};

// is dir clockwise
bool encoderDir;
bool moved = false;

void handleEncoder() {
  encoderDir = digitalRead(encoderClk)  == digitalRead(encoderDt);
  moved = true;
}

void setup() {
  Serial.begin(115200);

  pinMode(encoderDt, INPUT);
  pinMode(encoderClk, INPUT);

  attachInterrupt(digitalPinToInterrupt(encoderClk), handleEncoder, CHANGE);

  // put your setup code here, to run once:
  btns[0].attach(10, INPUT_PULLUP);
  btns[1].attach(A2, INPUT_PULLUP);
  btns[2].attach(A3, INPUT_PULLUP);
  btns[3].attach(A4, INPUT_PULLUP);
  btns[4].attach(3, INPUT_PULLUP); 
  btns[5].attach(5, INPUT_PULLUP);
  btns[6].attach(A5, INPUT_PULLUP);
  btns[7].attach(encoderSw, INPUT_PULLUP);

  for (int i = 0; i < btn_count; ++i) {
    btns[i].interval(5);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < btn_count; ++ i) {
    btns[i].update();

    if (btns[i].fell()) {
      Serial.print("Button ");
      Serial.print(i);
      Serial.println(" pressed!");
    }

    if (btns[i].rose()) {
      Serial.print("Button ");
      Serial.print(i);
      Serial.println(" released!");
    }
  }

  if (btns[7].fell()) {
    Serial.print("VRX: ");
    Serial.print(analogRead(A0));
    Serial.print(" VRy: ");
    Serial.println(analogRead(A1));
  }

  if (moved) {
    moved = false;
    Serial.print("encoder changed: ");
    Serial.println(encoderDir);
  }
}

(I temporarily removed encoder and joystick from photo)

I am able to register 4 buttons with no problem, but:

i am unable to register clicks of these buttons.
Well, to be honest occasionally their clicks are registred, but not always and their behaviour is unpredictable.

However when i plotted my circuit in wokwi it worked, every click was registres, so it should be a hardware problem:

I also tried reading raw values, without using library and i got HIGH value constantly, with no drops to LOW.

Could anyone help me out and point me to the problem in the circuit? Or is it just buttons not working, perhaps?

P.S.: I tried replacing those buttons with working ones and tested the one that i was unable to receive click and they worked, so every button is working.

https://europe1.discourse-cdn.com/arduino/original/4X/2/6/c/26c1cf29abfea7ce2c139550fe53840bb597f59a.png - What Software is this? As this could be very useful!

I believe it is Wokwi:

I also tried reading raw values, without using library and i got HIGH value constantly, with no drops to LOW.

Are your connections diagonally opposide across the button.

It seems like the problem is with the hardware and how it is connected to the ground. The thing is, it works properly in Wokwi but when you try it in person it just reads as HIGH all the time. This means the pins are not really connecting to the ground like they should be.

The likely causes are:

  • Split Rails: When you use mini-breadboards just putting them together is not enough to make them work. You need to use a jumper wire to connect the ground rail from one board to the other.

  • Button Orientation: Those little switches are shaped like rectangles. If you turn them sideways the pins might not be lining up right. That can cause problems.

  • Breadboard Tolerance: The legs on those buttons are really short so they do not always make a good connection in the middle of the board.

Try taking a jumper wire from a place where you know the ground is good and touch it to one of the pins that is not working right. If you see a "" message in the Serial Monitor then you know the code is fine and the problem is definitely, with how the breadboard is set up.

Welcome to the forum

It isn't

for (int i = 0; i < btn_count; ++i) {
    btns[i].interval(5);
  }

What will the value of i be on the first iteration of this for loop and others like it ?

It turns out that some buttons didn't sit well on breadboard, after putting everything properly it worked!

Guess that solves it then!

You have not answered my question

As a clue, what does ++i do to the value of i ?

dude
in loop:

for (int i = 0; i < btn_count; ++i) {
    btns[i].interval(5);
  }

++ i will be executed after body of a loop is executed and it will be executed until loop condition is true.

so basically

for (int i = 0; i < btn_count; ++i) {
...
}
// == it is the same as:
int i = 0;
while (i < btn_count) {
  ...
  ++ i
}

and there will be no difference if i use ++ i or i ++.

the only difference is when you use it in some formula or expression:

x = ++ i != x = i ++

and if using ++ i instead of i ++ in loop would be different, i would have never even used ++ i in my coding project, which i have a lot.

My apologies. I had misunderstood quite what you were doing