I need to display some scrolling text on a 128 x 64 pixel oled, connected to an arduino Due via I2C,
The arduino may (or may not) then be reset via a button connected to the reset pin, so it can run through the if else statements again in setup and call different bits of code depending on the number set on some BCD switches.
but i've found that sometimes when resetting the arduino, the oled display will 'crash'
the arduino is still running, leds working etc, but when pressing reset it takes a lot longer than normal for the led's to change colour (indicating a correct or incorrect number code has been set on the BCD switches)
Only unplugging and replugging the usb cable gets the oled display going again.
i've isolated the code to just the stuff that does the scrolling on the oled display, and found out that it will do the same thing if using the adafruit or U8g2 libraries to run the oled, it's slightly different code for the U8G2 version... but in both resetting can lock the display up when scrolling.
it's random when the display will crash, sometimes i can press the reset button 40+ times in a row and it keeps running fine, other times i press it once and crash it.
i can even get it to do this behavior in an arduino simulator, where only restarting the simulation gets the oled going again.
Any ideas what's causing the display to do this, and how i can go about fixing it?
#include <Wire.h>//I2C stuff
#include <Adafruit_SSD1306.h>//oled oled stuff
#include <Adafruit_GFX.h>//Draw stuff on oled
Adafruit_SSD1306 oled(128, 64, &Wire, -1);//Declare oled type, size, connection and that it has no reset pin.
char message[] = "Oled roulette: Pressing the reset button may or may not crash the display";//Text to scroll
int x, minX;//Store position and length of text
void setup() {
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);//Start oled on I2C
x = oled.width();//Sets to 128 pixels width
minX = -12 * strlen(message);//12 = 6 pixels/character * text size 2
}
void loop() {
oled.clearDisplay();//Clear buffer and screen
oled.setTextColor(WHITE);//Use white pixels
oled.setTextSize(3);//Set 3 times normal text size
oled.setTextWrap(false);//Turn text wrapping off
oled.setCursor(x, 16);//Position cursor
oled.print(message);//Scrolling message
oled.display();//Send the above to the display
x = x - 3;//scroll speed, make more positive to slow down the scroll
if (x < minX) x = oled.width();//If text length less than 128 pixels, use screen size as length to base scrolling maths on
}