Bad OLED display or something in the code

I made a toaster timer with a teensy 3.2 using this tiny display OLED Display and it has inconsistent brightness levels. It starts out with 0 minutes and 0 seconds and the brightness is consistent, but when it is incremented to 0 minutes and 30 seconds the brightness becomes inconsistent as shown in the attached image.

I think this is a problem with the display but I included the code just in case.

Is this display bad?
Do all these displays have this problem?
Am I doing something wring in the code?

// teensy 2.0
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include "EncoderStepCounter.h"

// Screen dimensions
const int SCREEN_WIDTH = 128;        // OLED display width, in pixels
const int SCREEN_HEIGHT = 32;        // OLED display height, in pixels
const int OLED_RESET = -1;

// DFPlayer pins
const int PIN_MP3_TX = PIN_B1;      // Connects to module's RX
const int PIN_MP3_RX = PIN_B2;      // Connects to module's TX

// Encoder pins
const int ENCODER_PIN1 = PIN_B6;
const int ENCODER_PIN2 = PIN_B5;

const int SWITCH_PIN = PIN_D4;
const int RELAY_PIN = PIN_D6;       // also the LED Pin
const int secIncrement = 15;

// SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 d(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
EncoderStepCounter encoder(ENCODER_PIN1, ENCODER_PIN2);
DFRobotDFPlayerMini player;
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);


int mins = 0;
int secs = 0;
char mc[3], sc[3];
String minStr, secStr;
boolean on = false, prevOn;
volatile long angleVal, cartVal;
int prevPos = 0;
boolean UP = true;
boolean DOWN = false;
boolean dir, displayChange = true;
boolean lastStat = false;
unsigned long tNow, prevT;

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

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

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  d.display();
  delay(100); // Pause for 1 seconds
  d.clearDisplay();

  pinMode(ENCODER_PIN1, INPUT_PULLUP);
  pinMode(ENCODER_PIN2, INPUT_PULLUP);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);

  encoder.begin();
  Display();
  prevT = millis();
  d.clearDisplay();
  d.setTextSize(2);                   // Normal 1:1 pixel scale
  d.setTextColor(SSD1306_WHITE);      // Draw white text
  d.setCursor(0, 0);                  // Start at top-left corner
  d.cp437(true);                      // Use full 256 char 'Code Page 437' font

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial))
  {
    Serial.println("OK");
    // Set volume to maximum (0 to 30).
    player.volume(15);
  }
  else
    Serial.println("Connecting to DFPlayer Mini failed!");
}

void loop(void)
{
  encoder.tick();
  CheckPowerButton();
  ReadEncoder();
  Display();
  Countdown();
}

void Countdown()
{
  if (secs == 0 && mins == 0 && on == true)
  {
    on = false;
    displayChange = true;
       // Play the first MP3 file on the SD card
    player.play(1);

    return;
  }

  if (on)
  {
    digitalWrite(RELAY_PIN, HIGH);
    tNow = millis();
    if (tNow - prevT > 1000)
    {
      prevT = tNow;
      Decrement(false);
      displayChange = true;
    }
    lastStat = true;
  }
  else
    digitalWrite(RELAY_PIN, LOW);
}
void ReadEncoder()
{
  if (!on)
  {
    signed char pos = encoder.getPosition();

    if (pos != prevPos)
    {
      if (pos > prevPos)
        dir = UP;
      else
        dir = DOWN;
      prevPos = pos;

      // UP Direction
      if (dir == UP)
        Increment();


      // DOWN Direction
      if (dir == DOWN)
        Decrement(true);
      displayChange = true;                   // indicate we need to change the display
    }
  }
}

void Increment()
{
  secs += secIncrement;

  if (secs >= 60)
  {
    secs = 0;
    mins += 1;
    if (mins > 29)
      mins = 29;
  }
}

void Decrement(boolean bySecIncrement)
{
  if (mins > 0 || secs > 0)
  {
    if (bySecIncrement)
      secs -= secIncrement;
    else
      secs -= 1;

    if (secs < 0)
    {
      if (bySecIncrement)
        secs = 60 - secIncrement;
      else
        secs = 59;

      mins -= 1;
      if (mins < 0)
        mins = 0;
    }
  }
}

void Display()
{
  if (displayChange == true)
  {
    displayChange = false;
    // get the mins and secs
    minStr = String(mins);
    int mLen = minStr.length();
    for (int i = 0; i < mLen; i++)
      mc[i] = minStr[i];
    mc[mLen] = 0;                                             // terminate the string

    secStr = String(secs);
    int sLen = secStr.length();
    for (int i = 0; i < sLen; i++)
      sc[i] = secStr[i];
    sc[sLen] = 0;                                             // terminate the string
    UpdateMin();
    d.setTextSize(2);
    d.setCursor(10, 0);
    d.println(mc);
    d.display();

    d.setCursor(45, 0);
    d.print("Min");
    d.display();

    UpdateSec();
    d.setCursor(10, 18);
    d.print(sc);
    d.display();

    d.setCursor(45, 18);
    d.print("Sec");
    d.display();

    if (on == true)
    {
      UpdateStatus();
      d.setCursor(92, 10);
      d.print("On");
      d.display();
    }
    else
    {
      UpdateStatus();
      d.setCursor(92, 10);
      d.print("Off");
      d.display();
    }
  }
}

void UpdateStatus()
{
  d.fillRect(92, 7, 102, 23, 0x0);
  d.setTextColor(SSD1306_WHITE);
}

void UpdateMin()
{
  d.fillRect(10, 0, 22, 14, SSD1306_BLACK);
  d.setTextColor(SSD1306_WHITE);
}

void UpdateSec()
{
  d.fillRect(10, 15, 22, 27, SSD1306_BLACK);
  d.setTextColor(SSD1306_WHITE);
}

void CheckPowerButton()
{

  if (digitalRead(SWITCH_PIN) == 0)         // if the button is pressed
  {
    while (digitalRead(SWITCH_PIN) == 0);  // wait for it to be released
    on = !on;                               // change the power state
    displayChange = true;                   // indicate we need to change the display
  }
}

Do you have some small test program that just writes a characters to all positions perhaps, and does that again with different chars a bit later and so on?

Just as simple as you can make it.

I will say the quality of these cheap devices is all over the map, I’ve also had some trouble as they age. Badly. And what seems rapidly.

a7

Have you ever dropped that teensy? I dropped one and it started doing that exact same thing.

-jim lee

jimLee:
Have you ever dropped that teensy? I dropped one and it started doing that exact same thing.

-jim lee

I'm guessing you meant dropping the display. No, it has never been dropped, nor has the Teensy. Today I am going to create a minimum sketch to see if it does that same thing on another display I have.

No I meant dropping the Teensy. Made it run slightly odd. And one of the symptoms was varying brightness on the OLED that it was running.

-jim lee

It turns out that a different display works fine, at least for now. I will see what happens after a few weeks of operating.

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