Text Shift randomly over Time on SSD1306

Hi, I am currently working on a project where I am using an Arduino Mega, a Bosch temperature/pressure sensor and a 2.23" Waveshare SSD1306 display.

At the moment I am having the problem that after a few minutes the text on the screen starts to glitch or is moving a few pixels randomly. Is my screen broken or is my code just shit?

Attached are some pictures and the code.

How it should be:

How it is after a few minutes:


//Import von Librarys
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Pixel Anzahl vom Display definiert 
#define SCREEN_WIDTH 132 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

//Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   13
#define OLED_CLK   12
#define OLED_DC    10
#define OLED_CS    11
#define OLED_RESET 9
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

//Pin vom Drucksensor
const int sensorPin = A5;

//Pin vom Temperatursensor
#define TempSensorDivider 2000   //defines the resistor value that is in series in the voltage divider
#define TempSensorPin A0         //defines the analog pin of the input voltage from the voltage divider
#define NUMSAMPLES 20                //defines the number of samples to be taken for a smooth average

//Steinhart Werte Bosch Sensor
const float steinconstA = 0.00129206139224324;        //steinhart equation constant A, determined from wikipedia equations
const float steinconstB = 0.000260792592983294;       //steinhart equation constant B, determined from wikipedia equations
const float steinconstC = 0.00000016891624679306;    //steinhart equation constant C, determined from wikipedia equations

int samples[NUMSAMPLES];

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.drawLine(66,0,66,32, SSD1306_WHITE);

}

void loop() {
  
  //Feste Werte im Display
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(5, 20.5);
  display.print("Oil T:");

  display.setCursor(70, 20.5);
  display.print("Oil P:");
  
  display.setCursor(126, 20.5);
  display.print("B");
  ZahlenLoop();
}

void ZahlenLoop() {
  while (true) {

    uint8_t i;                                          //integer for loop
    float average;                                      //decimal for average
    
    for (i=0; i<NUMSAMPLES; i++) {                      
      samples[i] = analogRead(TempSensorPin);        //takes samples at number defined with a short delay between samples
      delay(10);
    }

    average = 0;
    for (i=0; i< NUMSAMPLES; i++) {
      average += samples[i];                            //adds all number of samples together
    }
    average /= NUMSAMPLES;                              //divides by number of samples to output the average

    average = (TempSensorDivider*average)/(1023-average);        //conversion equation to read resistance from voltage divider

    float steinhart;                              //steinhart equation to estimate temperature value at any resistance from curve of thermistor sensor
    steinhart = log(average);                     //lnR
    steinhart = pow(steinhart,3);                 //(lnR)^3
    steinhart *= steinconstC;                     //C*((lnR)^3)
    steinhart += (steinconstB*(log(average)));    //B*(lnR) + C*((lnR)^3)
    steinhart += steinconstA;                     //Complete equation, 1/T=A+BlnR+C(lnR)^3
    steinhart = 1.0/steinhart;                    //Inverse to isolate for T
    steinhart -= 273.15;                          //Conversion from kelvin to celcius

    Serial.print("Temperature = ");
    Serial.print(steinhart);                      //prints final temp in celcius
    Serial.println(" *C");

    int sensorValue = analogRead(sensorPin); // Lese den Wert vom Sensor
    float voltage = sensorValue * (5.0 / 1023.0); // Wandelt den Sensorwert in Spannung um
    
    // Umrechnung der Spannung in Druck (Bar)
    float pressure = (voltage - 0.5) * 10.0 / 4.0;
    
    // Sicherstellen, dass der Druck nicht negativ wird (falls Spannung unter 0.5V fällt)
    if (pressure < 0) {
      pressure = 0;
    }
    
    // Ausgabe des Drucks im seriellen Monitor
    Serial.print("Druck: ");
    Serial.print(pressure, 1); // Ausgabe mit einer Nachkommastelle
    Serial.println(" Bar");    

    //Ausgaben im Display
    display.setTextColor(WHITE, BLACK);
    display.setCursor(41,20.5);
    int Temp = int(steinhart);
    display.print((String)Temp+"C");

    display.setCursor(106,20.5);
    display.print(pressure,1);

    display.display();

    delay(1000);

  }
}

Here is another picture.

Hi @alex53r ,

Welcome to the forum..

lose the (String) construct, print it twice, first the Temp, then the "C"..
i'm thinking memory fragmentation maybe??

curious, is serial prints clean??

good luck.. ~q

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