#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin not used
#define SCREEN_ADDRESS 0x3C // I2C address for the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define UP_BUTTON 4
#define DOWN_BUTTON 5
#define LEFT_BUTTON 2
#define RIGHT_BUTTON 3
#define A_BUTTON 6
#define GRID_SIZE_X 8
#define GRID_SIZE_Y 4
#define CELL_SIZE 16 // Each cell is 16x16 pixels
int cursorX = 0;
int cursorY = 0;
bool grid[GRID_SIZE_X][GRID_SIZE_Y];
void setup() {
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
pinMode(LEFT_BUTTON, INPUT_PULLUP);
pinMode(RIGHT_BUTTON, INPUT_PULLUP);
pinMode(A_BUTTON, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Initialize grid
for (int y = 0; y < GRID_SIZE_Y; y++) {
for (int x = 0; x < GRID_SIZE_X; x++) {
grid[x][y] = false;
}
}
}
void loop() {
// Read button states and move cursor
if (!digitalRead(UP_BUTTON) && cursorY > 0) {
cursorY = cursorY - 1;
delay(250);
}
if (!digitalRead(DOWN_BUTTON) && cursorY < GRID_SIZE_Y - 1){
cursorY = cursorY + 1;
delay(250);
}
if (!digitalRead(LEFT_BUTTON) && cursorX > 0){
cursorX = cursorX - 1;
delay(250);
}
if (!digitalRead(RIGHT_BUTTON) && cursorX < GRID_SIZE_X - 1){
cursorX = cursorX +1;
delay(250);
}
// Select grid cell
if (!digitalRead(A_BUTTON)) {
grid[cursorX][cursorY] = !grid[cursorX][cursorY]; // Toggle cell state
delay(200); // Debounce delay
}
// Draw the grid
display.clearDisplay();
for (int y = 0; y < GRID_SIZE_Y; y++) {
for (int x = 0; x < GRID_SIZE_X; x++) {
int cellX = x * CELL_SIZE;
int cellY = y * CELL_SIZE;
if (grid[x][y]) {
display.fillRect(cellX, cellY, CELL_SIZE, CELL_SIZE, WHITE);
} else {
display.drawRect(cellX, cellY, CELL_SIZE, CELL_SIZE, WHITE);
}
// Highlight the cursor position
if (cursorX == x && cursorY == y) {
display.drawRect(cellX + 2, cellY + 2, CELL_SIZE - 4, CELL_SIZE - 4, WHITE);
}
}
}
display.display();
}