#include <Wire.h> // Include the I2C library
#include <Adafruit_GFX.h> // Include the GFX library
#include <Adafruit_SSD1306.h> // Include the SSD1306 OLED library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Create an OLED object
const char* message = "Real-time Scrolling Message on OLED Display"; // Message to display
int messageLength; // Store the length of the message
void setup() {
// Initialize serial communication for debugging (optional)
Serial.begin(9600);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{ // Initialize with I2C address 0x3C
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Infinite loop if the display isn't found
}
display.clearDisplay(); // Clear the display buffer
display.setTextSize(1); // Set text size to normal (1)
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 0); // Set the cursor to top-left corner
messageLength = strlen(message); // Get the length of the message
}
void loop()
{
// Scroll the message from right to left, character by character
for (int i = 0; i < messageLength + SCREEN_WIDTH; i++) {
display.clearDisplay(); // Clear the display
// Display the message starting from different positions based on the scroll index
display.setCursor(SCREEN_WIDTH - (i % (messageLength + SCREEN_WIDTH)), 0); // Calculate the x position
display.print(message); // Print the message
display.display(); // Refresh the display with the new content
delay(150); // Delay to control the scroll speed
}
}