/*
Wokwi | questions
Button components floating.
HexedRevii - Friday, March 27, 2026 6:04 PM
My two button components are stuck floating,
even though they should be wired correctly according to Arduino.
*/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
//#include "Joystick.h"
const int NUM_BTNS = 3;
const int BTN_PINS[] = {13, 12, 11};
const int RGB_PINS[] = {6, 5, 3};
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
//Joystick* joystick = NULL;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
btnState[i] = digitalRead(BTN_PINS[i]); // check each button
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == 0) { // was just pressed
btnPressed = i + 1;
}
delay(20); // debounce
}
}
return btnPressed;
}
void ledButtonCallback() {
}
void setup() {
Serial.begin(9600);
display.setRotation(2);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Failed to initialise display.");
for (;;) {}
}
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
for (int i = 0; i < 3; i++) {
pinMode(RGB_PINS[i], OUTPUT);
}
// init
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
display.setCursor(0, 20);
display.print("Hello, guys");
display.display();
Serial.println("Ready!");
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber) {
//Serial.println(btnNumber);
for (int i = 0; i < 3; i++) {
analogWrite(RGB_PINS[i], 0);
}
if (btnNumber == 1) {
analogWrite(RGB_PINS[0], 255);
} else if (btnNumber == 2) {
analogWrite(RGB_PINS[1], 255);
} else if (btnNumber == 3) {
analogWrite(RGB_PINS[2], 255);
}
}
}