#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// SH1107 Display Parameters for 128x128
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1107 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define button pins
#define UP_BUTTON 2
#define DOWN_BUTTON 3
#define LEFT_BUTTON 4
#define RIGHT_BUTTON 5
#define A_BUTTON 6
#define B_BUTTON 7
// Define dot properties
int dotX = (SCREEN_WIDTH / 2) - 4; // Start the dot in the center (X-axis)
int dotY = (SCREEN_HEIGHT / 2) - 4; // Start the dot in the center (Y-axis)
int dotSize = 8; // Starting size of the dot
void setup() {
// Initialize I2C and the display
Wire.begin();
display.begin(0x3C, true); // Adjust I2C address if necessary
// Initialize buttons with pull-up resistors
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);
pinMode(B_BUTTON, INPUT_PULLUP);
// Clear the display and draw the initial dot
display.clearDisplay();
drawDot();
}
void loop() {
bool updated = false;
// Check for D-pad presses to move the dot
if (digitalRead(UP_BUTTON) == LOW) {
if (dotY > 0) {
dotY -= 4;
updated = true;
}
}
if (digitalRead(DOWN_BUTTON) == LOW) {
if (dotY + dotSize < SCREEN_HEIGHT) {
dotY += 4;
updated = true;
}
}
if (digitalRead(LEFT_BUTTON) == LOW) {
if (dotX > 0) {
dotX -= 4;
updated = true;
}
}
if (digitalRead(RIGHT_BUTTON) == LOW) {
if (dotX + dotSize < SCREEN_WIDTH) {
dotX += 4;
updated = true;
}
}
// Check for A button to increase the dot size
if (digitalRead(A_BUTTON) == LOW) {
dotSize += 2;
if (dotSize + dotX > SCREEN_WIDTH || dotSize + dotY > SCREEN_HEIGHT) {
dotSize -= 2; // Prevent size overflow
} else {
updated = true;
}
}
// Check for B button to decrease the dot size
if (digitalRead(B_BUTTON) == LOW) {
if (dotSize > 2) {
dotSize -= 2;
updated = true;
}
}
// If any button was pressed and the dot moved or resized, update the display
if (updated) {
drawDot();
}
delay(100); // Debouncing delay
}
// Function to draw the dot on the OLED
void drawDot() {
display.clearDisplay(); // Clear the display
display.fillRect(dotX, dotY, dotSize, dotSize, SH110X_WHITE); // Draw the dot
display.display(); // Show the display
}Loading
grove-oled-sh1107
grove-oled-sh1107