#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Button pin definitions
const int buttonLeftPin = 2; // Change to your button pin number
const int buttonRightPin = 3; // Change to your button pin number
const int buttonUpDownPin = 4; // Change to your button pin number
// Text to be displayed
const char* displayText = "Your text goes here!";
void setup() {
// Initialize the display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer
display.clearDisplay();
// Initialize button pins as inputs with pull-ups
pinMode(buttonLeftPin, INPUT_PULLUP);
pinMode(buttonRightPin, INPUT_PULLUP);
pinMode(buttonUpDownPin, INPUT_PULLUP);
}
void loop() {
// Set initial position for scrolling
int scrollX = 0;
int scrollY = 15; // Initial Y position
while (true) {
// Clear display before displaying text
display.clearDisplay();
// Display text on OLED
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(scrollX, scrollY);
display.println(displayText);
display.display();
// Check button presses for scrolling
if (digitalRead(buttonLeftPin) == LOW) {
scrollX++; // Scroll right
delay(50); // Adjust delay for scrolling speed
}
if (digitalRead(buttonRightPin) == LOW) {
scrollX--; // Scroll left
delay(50); // Adjust delay for scrolling speed
}
if (digitalRead(buttonUpDownPin) == LOW) {
scrollY--; // Move text up
delay(50); // Adjust delay for scrolling speed
}
// Limit scrolling bounds
if (scrollX < -128) {
scrollX = -128;
}
if (scrollX > SCREEN_WIDTH) {
scrollX = SCREEN_WIDTH;
}
if (scrollY < 0) {
scrollY = 0;
}
if (scrollY > SCREEN_HEIGHT - 10) {
scrollY = SCREEN_HEIGHT - 10;
}
}
}