Using a WaveShare RP2040 Zero, two buttons, and an SSD1306 128x64 OLED.
I'm "wiping" over "const char Characters", it has uppercase, lowercase, and numbers.
if I scroll left, I see the first four character from Characters are repeated. But if I scroll all the way right to the point it wraps around or all the way left, the duplicated characters do not appear.
I'm super new to C++ and simply cannot see the issue.
#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);
// Pin definitions for buttons
const int button1Pin = 28;
const int button2Pin = 27;
int button1state = 0;
int button2state = 0;
// Characters set
int slider = 5;
int leftleftchar_index = slider-2;
int leftchar_index = slider-1;
int mid_index = slider;
int rightchar_index = slider+1;
int rightrightchar_index = slider+2;
const char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
// Function to update the display
void updateDisplay() {
display.clearDisplay();
display.fillRect(49, 0, 24, 100, SSD1306_WHITE); // Fill rectangle with solid white
display.setTextSize(4);
display.setCursor(1, 30);
display.setTextColor(SSD1306_WHITE);
display.print(characters[leftleftchar_index % strlen(characters)]);
display.setTextSize(4);
display.setCursor(26, 20);
display.setTextColor(SSD1306_WHITE);
display.print(characters[leftchar_index % strlen(characters)]);
display.setTextSize(4);
display.setCursor(51, 15);
display.setTextColor(SSD1306_BLACK); // Set text color to black with white background
display.print(characters[mid_index % strlen(characters)]); ;
display.setTextSize(4);
display.setCursor(76, 20);
display.setTextColor(SSD1306_WHITE);
display.print(characters[rightchar_index % strlen(characters)]);
display.setTextSize(4);
display.setCursor(101, 30);
display.setTextColor(SSD1306_WHITE);
display.print(characters[rightrightchar_index % strlen(characters)]);
display.display(); // Update the display with the new data
}
void setup() {
// Initialize I2C and the display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// if display doesn't initialize
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
// Set button pins as input
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
// Initial character display
updateDisplay();
}
void loop() {
// Check button states
if (digitalRead(button1Pin) == LOW) { // Button 1 pressed
if (button1state == 0) {
leftleftchar_index--;
leftchar_index--;
mid_index--;
rightchar_index--;
rightrightchar_index--;
updateDisplay();
button1state = 1;
}
} else {
button1state = 0; // Reset state when button is released
}
if (digitalRead(button2Pin) == LOW) { // Button 2 pressed
if (button2state == 0) {
leftleftchar_index++;
leftchar_index++;
mid_index++;
rightchar_index++;
rightrightchar_index++;
updateDisplay();
button2state = 1;
}
} else {
button2state = 0; // Reset state when button is released
}
delay(10); // Debounce delay
}