#include <U8g2lib.h>
#include <Wire.h>
// OLED display configuration
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Button pin definitions
const int UP_BTN = 14;
const int DOWN_BTN = 26;
const int LEFT_BTN = 33;
const int RIGHT_BTN = 27;
const int SELECT_BTN = 25;
// Square properties
int squareX = 63; // Start at center X
int squareY = 31; // Start at center Y
const int squareSize = 4;
void setup() {
u8g2.begin();
// Set up button pins as inputs with pull-up resistors
pinMode(UP_BTN, INPUT_PULLUP);
pinMode(DOWN_BTN, INPUT_PULLUP);
pinMode(LEFT_BTN, INPUT_PULLUP);
pinMode(RIGHT_BTN, INPUT_PULLUP);
pinMode(SELECT_BTN, INPUT_PULLUP);
}
void loop() {
// Check button states and update square position
if (digitalRead(UP_BTN) == LOW && squareY > 0) {
squareY-=2;
}
if (digitalRead(DOWN_BTN) == LOW && squareY < 62) {
squareY+=2;
}
if (digitalRead(LEFT_BTN) == LOW && squareX > 0) {
squareX-=2;
}
if (digitalRead(RIGHT_BTN) == LOW && squareX < 126) {
squareX+=2;
}
// Draw the display
u8g2.clearBuffer();
// Draw the square if SELECT button is not pressed
if (digitalRead(SELECT_BTN) == HIGH) {
u8g2.drawBox(squareX, squareY, squareSize, squareSize);
}
u8g2.sendBuffer();
// Small delay to debounce buttons and control speed
delay(50);
}