/*
=== **Scrolling Text Demo ===
Part of API menu review
8/13/25 Sketch reviewed with Claude AI
*/
#include "U8g2lib.h"
// Constructor for U8g2lib
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
u8g2_uint_t offset; // Tracks the current horizontal position for scrolling animation
u8g2_uint_t width; // Stores the total pixel width of the text string
// this example text is 82 characters long
const char *text = "We could never learn to be brave and patient if there were only joy in the world. ";
void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_inb16_mr);
// width is a combination of 'text' length, which is 82
// and the pixel width of the font, which is 14.
width = u8g2.getUTF8Width(text); // width: 82 x 14 = 1148
u8g2.setFontMode(0); // enable transparent mode, which is faster
}
void loop(void) {
u8g2_uint_t x; // declare a local variable x for horizontal positioning
u8g2.firstPage();
do {
// draw the scrolling text at current offset
x = offset;
u8g2.setFont(u8g2_font_inb16_mr); // set the target font
do { // repeated drawing of the scrolling text...
// x = horizontal, y = vertical, text = *string
u8g2.drawUTF8(x, 30, text); // draw the scolling text
x += width; // add the pixel width of the scrolling text
// draw again until the complete display is filled
// while x < 128
} while (x < u8g2.getDisplayWidth()); // getDisplayWidth() = 128
/*
Lines 35-41: Inner loop that draws the text multiple times across the screen:
Draws the text at position (x, 30) - 30 pixels from top
Increases x by the text width to position the next copy
Continues until the entire 128-pixel width is filled
This creates seamless scrolling by having multiple copies of the text
*/
u8g2.setFont(u8g2_font_inb16_mr); // draw the current pixel width
u8g2.setCursor(0, 58);
u8g2.print(width); // this value must be lesser than 128 unless U8G2_16BIT is set
// Complete the page rendering system -
// this actually sends the drawn content to the display.
} while (u8g2.nextPage());
// Decreases offset by 10 pixels (moves text left)
// When offset becomes more negative than the text width, reset to 0
// This creates continuous left-scrolling motion
offset -= 10; // speed adjust, lower number slower, higher number faster
if ((u8g2_uint_t) offset < (u8g2_uint_t) - width)
offset = 0; // start over again
delay(50); // > value, can be used to slow down scrolling
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1