i2c Oled display sometime printing old data instead of new one with Teensy Lc

charnjit

Well-known member
i have well connected i2c oled display(128x32) with teensy LC(pin18,19) . using this code
C++:
#include <EEPROM.h>
#include <Keypad.h>
#include <MIDI.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels.
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)  -1
#define SCREEN_ADDRESS 0x3C   //  See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const byte ROWS = 14; //
const byte COLS = 6; //
char keys[ROWS][COLS] = {
{ 1, 2, 3, 4, 5, 6},
{ 7, 8, 9,10,11,12},
{13,14,15,16,17,18},
{19,20,21,22,23,24},
{25,26,27,28,29,30},
{31,32,33,34,35,36},
{37,38,39,40,41,42},
{43,44,45,46,47,48},
{49,50,51,52,53,54},
{55,56,57,58,59,60},
{61,62,63,64,65,66},
{67,68,69,70,71,72},
{73,74,75,76,77,78},
{79,80,81,82,83,84}
};
byte rowPins[ROWS] = {2,3,4,5,6,7,8,9,10,11,12,14,15,16 };
byte colPins[COLS] = {21,22,23,24,25,26};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

        #define PATCH_UP    52         
         byte Patch=0;     
        
           void setup() {
                       Serial.begin(9600);
                       display.begin();
                   if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
                                                           Serial.println(F("SSD1306 allocation failed"));
                                                           for(;;); // Don't proceed, loop forever
                                                            }
                           Serial.println("SETUP DONE"); 
}


void loop() {
 
  if (kpd.getKeys())
        {
        for (int i=0; i<LIST_MAX; i++)   
        {      int mykey = kpd.key[i].kchar;
          
             if (( kpd.key[i].stateChanged ) && (mykey==PATCH_UP))   //
            {
                switch (kpd.key[i].kstate) {   
                    case PRESSED:
                   Patch++;   
                   SCN_PRINT_NUMBER(Patch);
                   delay(500);
                                }
            }   //-------------------------------------------------------
        } 
    }
                   Patch++;   
                   SCN_PRINT_NUMBER(Patch);
                  Serial.println(Patch);
                   delay(500);             
           }
               void SCN_PRINT_NUMBER(int n) {
                                             display.begin();
                    display.clearDisplay(); // Clear display buffer
                    display.setTextColor(WHITE);
                    display.setCursor(0, 0);
                    display.setTextSize(2);
                    display.print(n);
                    display.display();
                                             }
oled is being update every half second . but value of "Patch" some time missing on display .not showing in right increament . looking like
1...2...3...3...5...5...7 when printing last value again , it also print with another cursor position of text automatic.
value of "Patch" printing in serial monitor with right increament like 1...2...3...4...5...6...7...8...9. i have tested three new displays. but getting same result..
please reply . if this kind of problem is fixed
..........thank you..............
 
I'd suggest you try removing the first "display.begin();" in setup() and the one in SCN_PRINT_NUMBER().
I'm pretty sure you only need to call it once in setup().

Pete
 
This can run on a T_4.0 here with OLED - it is 132*64 - but 32 maps.
The problem is having to use Wire1 has a pin #16 conflict with the keyboard, so it has to not be queried.
If it is then the repeat .begin helps it work - just some weird flashes on the lower extra pixel on 64 instead of 32.

Some MODS to SCR_PRINT_NUMBER track prior value and make it clear if it is ever called out of turn.
Key test disabled - try it like that perhaps then undo the ZERO in " if (0&&kpd.getKeys())"

Code:
#include <EEPROM.h>
#include <Keypad.h>
#include <MIDI.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels.
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)  -1
#define SCREEN_ADDRESS 0x3C   //  See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const byte ROWS = 14; //
const byte COLS = 6; //
char keys[ROWS][COLS] = {
  { 1, 2, 3, 4, 5, 6},
  { 7, 8, 9, 10, 11, 12},
  {13, 14, 15, 16, 17, 18},
  {19, 20, 21, 22, 23, 24},
  {25, 26, 27, 28, 29, 30},
  {31, 32, 33, 34, 35, 36},
  {37, 38, 39, 40, 41, 42},
  {43, 44, 45, 46, 47, 48},
  {49, 50, 51, 52, 53, 54},
  {55, 56, 57, 58, 59, 60},
  {61, 62, 63, 64, 65, 66},
  {67, 68, 69, 70, 71, 72},
  {73, 74, 75, 76, 77, 78},
  {79, 80, 81, 82, 83, 84}
};
byte rowPins[ROWS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16 };
byte colPins[COLS] = {21, 22, 23, 24, 25, 26};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define PATCH_UP    52
byte Patch = 0;

void setup() {
  Serial.begin(9600);
  display.begin();
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  Serial.println("SETUP DONE");
}


void loop() {

  if (0&&kpd.getKeys())
  {
    for (int i = 1000; i < LIST_MAX; i++)
    { int mykey = kpd.key[i].kchar;

      if (( kpd.key[i].stateChanged ) && (mykey == PATCH_UP)) //
      {
        switch (kpd.key[i].kstate) {
          case PRESSED:
            Patch++;
            SCN_PRINT_NUMBER(Patch);
            delay(500);
        }
      }   //-------------------------------------------------------
    }
  }
  Patch++;
  SCN_PRINT_NUMBER(Patch);
  Serial.println(Patch);
  delay(500);
}
void SCN_PRINT_NUMBER(int n) {
  static int nLast=0;
  //display.begin();
  display.clearDisplay(); // Clear display buffer
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print(n);
  display.print(" ");
  display.print(nLast);
  display.print(" ");
  display.print(n-nLast);
  if ( n-nLast > 1 ) Serial.print( "  MISSED -----" );
  nLast = n;
  display.display();
}
 
removing the first "display.begin();" in setup() and the one in SCN_PRINT_NUMBER().
Leaving only the one in setup().
2nd .being helped here with Wire1 conflict - but for @charnjit it may be causing the trouble.
It helped here, but not when using 64 height on this display instead of 32.
 
Thank for Reply.....
i removed display.begin(); from setup and loop. got skipping data and cursor position auto increases one by one pixel.see result here
video1
i also tryied code from @defragster . got result below not stable position and skipping
video2
in Serial monitor result is accurate increament.
i want to use My oled display with my Teensy lc board only . it was my starting code in post#1 to test result on display.
what to try next???
 
code from @defragster . got result below not stable position and skipping
That is Odd! Was that code used as posted where the " if (0&&kpd.getKeys())" was still preventing the keyboard scan?
Running on a T_4.0 here and never saw that pixel position shift!
Question did this line ever print to SerMon:
if ( n-nLast > 1 ) Serial.print( " MISSED -----" );
Clearly it is skipping as shown in video2 - but never showing a skip difference other that 1 when it updates the display.
If that 'MISSED' is not showing on SerMon then the display updates are being called in order, but the display is just not 'working properly'

Perhaps the LC is getting behind - which should not be the case with a fixed "delay(500);" For a test change that loop() delay(500); to delay(1000);

The code running on that T_4.0 before was using "Wire.setClock(8000000);", perhaps add that to setup with a value of 1,000,000. If it fails worse then, the wires may be the trouble if they are too long or poor in connection quality?

Also: Not sure if this is the LATEST version - but this 2.5.7 is what the IDE shows for the SSD1306 drive in use here:
Using library Adafruit_SSD1306 at version 2.5.7 in folder: T:\T_Drive\tCode\libraries\Adafruit_SSD1306
 
" MISSED -----" );
not being print in Serial monitor
. i test these display on Arduino leonardo board they are working right.i seemed my Teensy Lc board got fault something
thank you all of you.
 
That is ODD. It is being called each time or that would print, the display is just not getting/taking an update.
 
Back
Top