#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);
const String loremipsum = "hello i am Arvind Patil, from india "
" this project is example of scrolling text, on ssd1306 oled "
"this is a simulated project no hardware is used, "
"the fastest way to learn electronics, "
"thanks to wokwi simulator "
"HAVE A NICE DAY, "
"JAI HIND NAMASTE.";
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setTextWrap(false);
display.setCursor(0, 0);
display.display();
delay(3000);
display.clearDisplay();
}
void loop() {
static uint16_t start_at = 0;
display.clearDisplay();
int lineHeight = 8; // Adjust this value according to the font size you're using
int lineCount = SCREEN_HEIGHT / lineHeight;
int x = 0;
int y = 0;
for (int i = 0; i < lineCount; i++) {
int lineStart = start_at;
String line = "";
int lineWidth = 0;
while (lineWidth < SCREEN_WIDTH && lineStart < loremipsum.length()) {
char c = loremipsum.charAt(lineStart++);
if (c == ' ' || c == '\n') {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(line, x, y, &x1, &y1, &w, &h);
if (x + w > SCREEN_WIDTH) {
break;
}
line += c;
} else {
line += c;
}
}
display.setCursor(x, y);
display.print(line);
y += lineHeight;
start_at = lineStart;
if (start_at >= loremipsum.length()) {
start_at = 0;
break;
}
}
display.display();
delay(1000);
}