Scrolling text on a SH1107 OLED screen

I have a 128 by 64 I2C SH1107 OLED screen. I am trying to make a diagonal scrolling animation but when the text reapers in the upper right it fills half the the screen with text. I am erasing the text by putting a black box of the previous text. I don't want to use clear display because it slows it down.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

#define i2c_Address 0x3c // I2C address
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET -1     // Not connected for QT-PY / XIAO
Adafruit_SH1107 display = Adafruit_SH1107(SCREEN_HEIGHT, SCREEN_WIDTH, &Wire, OLED_RESET);

const char* scrollText = "Diagonal Scrolling!";  // Text to scroll

void setup() {
  Serial.begin(9600);
  delay(250); // Wait for the OLED to power up

  display.begin(i2c_Address, true);
  display.clearDisplay();
  display.setRotation(1); // Set rotation if needed
  
  // Set text properties
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);

  display.display();
}

void loop() {
  static int16_t x = 0; // Starting X position (top left)
  static int16_t y = 0; // Starting Y position (top left)
  
  int16_t textWidth = strlen(scrollText) * 6;   // Calculate text width, 6px per char for size 1
  int16_t textHeight = 8;                       // Height of the text line (8px for size 1)

  // Clear display for new frame
  display.fillRect(x, y, textWidth, textHeight, SH110X_BLACK);

  // Move diagonally: increase both X and Y coordinates
  x++;
  y++;


  // Set new cursor position and print text
  display.setCursor(x, y);
  display.print(scrollText);
  display.display();  // Show text

  // Handle screen wrap-around (reset X and Y if the text goes off the screen)
  if (x > SCREEN_WIDTH) {
    x = -textWidth;  // Reset X to the left, just off the screen
  }
  if (y > SCREEN_HEIGHT) {
    y = -textHeight; // Reset Y to the top, just off the screen
  }

  delay(50); // Control scroll speed (adjust as needed)
}

One technique you may be interested in, is ‘bit blitting
Transposing an area of pixels along an axis, while extending or trimming the boundary area to create a ‘moving’ image with minimal processing.

I will look into to that. But my problem is when when the text restarts on the right side it unsynchronized with the black box that fallows behind the text to erase the previous test. I am looking for help fixing that.

Why? Isn't it easier to just print the text in the old location but with the background color (black) and then print it in the new location with the active color?

I will give that a try. but with to code the way it is now I feel like I am so close to making it work the way i want it to I have to change one little thing I just don't know what it is.

I think I have narrowed the problem. I changed the code so the text moves left to right and notice when the text moves off the screen it reapers in the row below it one letter at a time. it is treating it as text so when it moves off the screen it sees the top row as full and moves it to the next row. If I convert the words into a bitmap it should act differently.

I was having problems understanding bit maps so decided to look deeper into the adafruit libary and found a wed page that explains how to use it and it turns out all a have to do is add is this line of code in the void set up and it works now. If you are having hard time using you display use this website. https://learn.adafruit.com/adafruit-gfx-graphics-library/overview

display.setTextWrap(false);
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

#define i2c_Address 0x3c // I2C address
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET -1     // Not connected for QT-PY / XIAO
Adafruit_SH1107 display = Adafruit_SH1107(SCREEN_HEIGHT, SCREEN_WIDTH, &Wire, OLED_RESET);

const char* scrollText = "Diagonal Scrolling!";  // Text to scroll

void setup() {
  Serial.begin(9600);
  delay(250); // Wait for the OLED to power up

  display.begin(i2c_Address, true);
  display.clearDisplay();
  display.setRotation(1); // Set rotation if needed
  
  // Set text properties
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setTextWrap(false);

  display.display();
}

void loop() {
  static int16_t x = 0; // Starting X position (top left)
  static int16_t y = 0; // Starting Y position (top left)
  
  int16_t textWidth = strlen(scrollText) * 6;   // Calculate text width, 6px per char for size 1
  int16_t textHeight = 8;                       // Height of the text line (8px for size 1)

  // Clear display for new frame
  display.fillRect(x, y, textWidth, textHeight, SH110X_BLACK);
  
  // Move diagonally: increase both X and Y coordinates
  x++;
  y++;
  
  // Set new cursor position and print text
  display.setCursor(x, y);
  display.print(scrollText);
  display.display();  // Show text

  // Handle screen wrap-around (reset X and Y if the text goes off the screen)
  if (x > SCREEN_WIDTH) {
    x = -textWidth;  // Reset X to the left, just off the screen
  }
  if (y > SCREEN_HEIGHT) {
    y = -textHeight; // Reset Y to the top, just off the screen
  }

  delay(30); // Control scroll speed (adjust as needed)
}

And now, scroll up:

If you are using a SH1107.

#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SH1107 display = Adafruit_SH1107(SCREEN_HEIGHT, SCREEN_WIDTH, &Wire, OLED_RESET);

void setup () {
  if (!display.begin(SCREEN_ADDRESS, true))
    for (;;); // halt here

  display.clearDisplay();
  display.setTextSize(1);
  display.setRotation(1);

  pinMode(6, INPUT_PULLUP);
  while (digitalRead(6)){;;}

  for (int i = 63; i > 0; i -= 3) {
    display.setTextColor(SH110X_WHITE);
    display.setCursor(0, i);
    display.println(F("Twas brillig, and theslithy toves Did gyre\nand gimble in th wabe\nAll mimsy were the\nborogoves, And the\nmome raths outgrabe."));
    display.display();
    display.clearDisplay();
  }
}

void loop() { }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.