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.


