// Using this code, you can display the text of your choice on the OLED and also
// control the position of the text displayed using the push-buttons in the order
// arranged. You may also use the arrow buttons on your keyboard for the same
// functionality.
#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);
#define BUTTON_UP 23
#define BUTTON_DOWN 25
#define BUTTON_LEFT 26
#define BUTTON_RIGHT 27
int8_t displayOffsetX = 0;
int8_t displayOffsetY = 0;
const uint8_t textSize = 1;
String userInput; // This will hold the user input
void setup() {
Serial.begin(9600); // Start the serial communication with the baud rate of 9600
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
Serial.println("Enter text to be displayed:");
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_LEFT, INPUT_PULLUP);
pinMode(BUTTON_RIGHT, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(textSize);
display.setCursor(0, 0);
display.println(userInput);
display.display();
}
void loop() {
while (Serial.available()) { // Check if data is available to read
userInput = Serial.readString(); // Read the input string
display.clearDisplay();
display.setCursor(displayOffsetX * textSize, displayOffsetY * textSize);
display.println(userInput);
display.display();
}
if (digitalRead(BUTTON_UP) == LOW) {
// Scroll display up
displayOffsetY++;
display.clearDisplay();
display.setCursor(displayOffsetX * textSize, displayOffsetY * textSize);
display.println(userInput);
display.display();
delay(100);
}
if (digitalRead(BUTTON_DOWN) == LOW) {
// Scroll display down
displayOffsetY--;
display.clearDisplay();
display.setCursor(displayOffsetX * textSize, displayOffsetY * textSize);
display.println(userInput);
display.display();
delay(100);
}
if (digitalRead(BUTTON_LEFT) == LOW) {
// Scroll display left
displayOffsetX--;
display.clearDisplay();
display.setCursor(displayOffsetX * textSize, displayOffsetY * textSize);
display.println(userInput);
display.display();
delay(100);
}
if (digitalRead(BUTTON_RIGHT) == LOW) {
// Scroll display right
displayOffsetX++;
display.clearDisplay();
display.setCursor(displayOffsetX * textSize, displayOffsetY * textSize);
display.println(userInput);
display.display();
delay(100);
}
}