ST7735_t3 Display Library Interferes with Encoder Library?

Status
Not open for further replies.
Hello everyone, I'm working on a synthesizer and bringing up the parts one at a time right now.

Previously I got all the inputs working (pot, encoder, buttons). Not I'm adding a 1.14" LCD display by Adafruit

I'm also using the Teensy Encoder Library and the Teensy ST7735_t3 and ST7789_t3 libraries.

For the first step I wanted to print the encoder.Read value to the display. However I fond that when I added the display the encoder started outputting garbage data (numbers flipping between -1 and -2). By commenting out code I found that the problem happens when I add the display.init(135,200). I believe the .init() function is used to set the resolution of the display. (In my code the display object is tft so the actual function is tft.init(135,200).

I think there might be a conflict of multiple libraries using the same time or something like that. Can anyone help me debug it? I've tried digging into the .init() code but it's too complex for me at the moment.

Thank you!

Code:
#include <Audio.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <Bounce.h>
#include <Encoder.h>

//Engage debug messages
bool potDebug = true;
bool encoderDebug = true;
bool buttonDebug = true;
bool synthDebug = true;

//buttons, digital
const int numDPins = 12;
int dPins[numDPins] = {14,24,25,28,29,30,31,32,34,35,36,37};
int dVals[numDPins];
int dValsLag[numDPins];
Bounce dBounce[] = {
  Bounce(14,10),
  Bounce(24,10),
  Bounce(25,10),
  Bounce(28,10),
  Bounce(29,10),
  Bounce(30,10),
  Bounce(31,10),
  Bounce(32,10),
  Bounce(34,10),
  Bounce(35,10),
  Bounce(36,10),
  Bounce(37,10),
};

//hardware controls, analog
const int numAPins = 1;
int aPins[numAPins] = {A1};
int aValsRaw[numAPins];
int aVals[numAPins];
int aValsLag[numAPins];
int aFlutter = 20; //change in analog reading before we count it

//encoders
const int numEncs = 2;
int ePins[numEncs * 2] = {1,2,4,5};
Encoder myEncA(1,2);
Encoder myEncB(4,5);
long oldPositionA  = -999;
long newPositionA = 0;
long oldPositionB = -999;
long newPositionB = 0;

//LEDs
int ledPin = 13;  //store the address
int ledSet = 0;   //store the desired setting

//Display
#define TFT_MISO  39
#define TFT_MOSI  26  //a12
#define TFT_SCK   27  //a13
#define TFT_DC   40 
#define TFT_CS   38  
#define TFT_RST  41

//#include <Adafruit_GFX.h>    // Core graphics library
#include <ST7735_t3.h> // Hardware-specific library
#include <ST7789_t3.h> // Hardware-specific library
#include <SPI.h>

ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST);

//**************
//DISPLAY FUNCTIONS
//**************

void tftPrintTest() {
  tft.setTextWrap(false);
  tft.fillScreen(ST7735_BLACK);
  tft.setCursor(0, 30);
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(1);
  tft.println("Hello World!");
  tft.setTextColor(ST7735_YELLOW);
  tft.setTextSize(2);
  tft.println("Hello World!");
  tft.setTextColor(ST7735_GREEN);
  tft.setTextSize(3);
  tft.println("Hello World!");
  tft.setTextColor(ST7735_BLUE);
  tft.setTextSize(4);
  tft.print(1234.567);
}

void testtriangles() {
  tft.fillScreen(ST7735_BLACK);
  int color = 0xF800;
  int t;
  int w = tft.width()/2;
  int x = tft.height()-1;
  int y = 0;
  int z = tft.width();
  for(t = 0 ; t <= 15; t+=1) {
    tft.drawTriangle(w, y, y, x, z, x, color);
    x-=4;
    y+=4;
    z-=4;
    color+=100;
  }
}

void displayLong(long k) {
  tft.fillScreen(ST7735_BLACK);
  tft.setCursor(30, 30);
  tft.setTextColor(ST7735_BLUE);
  tft.setTextSize(4);
  tft.println(k);
}


//**************
//MAIN FUNCTIONS
//**************
void readInputs() {
  //Read Digital Pins
  for (int i = 0; i < numDPins; i++) {
    dBounce[i].update();
  }

  //Read Analog Pins
  for (int i = 0; i < numAPins; i++) {
    aValsRaw[i] = analogRead(aPins[i]);
  }
  //Reject analog fluctuations
  for (int i = 0; i < numAPins; i++) {
    aValsLag[i] = aVals[i]; //store old value for refence

    //Update aVal if the change exceeds the flutter
    if (aValsRaw[i] > aVals[i] + aFlutter || aValsRaw[i] < aVals[i] - aFlutter) {
      aVals[i] = aValsRaw[i]; //store new value
      if (potDebug) {Serial.println(aVals[i]);}
      ledSet = (aVals[0] / 100) % 2;
      Serial.print( " ledSet is ");
      Serial.println(ledSet);
      digitalWrite(ledPin,ledSet);
    }
  }
  
  //Read Encoders
  newPositionA = myEncA.read();
  newPositionB = myEncB.read();
}

//**************
//SETUP and LOOP
//**************

void setup() {
  // put your setup code here, to run once:
  // initialize the digital pin as an output.
  
  //initialize LEDs
  pinMode(ledPin, OUTPUT);

  //Initialize Digital Pins
  for (int i = 0; i < numDPins; i++) {
    pinMode(dPins[i], INPUT_PULLUP);
    dBounce[i].update();
  }

  Serial.begin(115200);

  //initialize display
  tft.init(135, 240);          
  tft.setRotation(3);
  // tft print function!
  tftPrintTest();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  readInputs();

  //Display digital pin status
  for (int i = 0; i < numDPins; i++) {
    if (dBounce[i].fallingEdge()) {
      Serial.print("Press Pin ");
      Serial.println(dPins[i]);
    }
  }

  
  if (newPositionA != oldPositionA) {
    oldPositionA = newPositionA;
    Serial.print("Enc A Position ");
    Serial.println(newPositionA);
    ledSet = (newPositionA / 4) % 2;
    digitalWrite(ledPin,ledSet);
    displayLong(newPositionA);
  }
}
 
Are you using physical pull-ups on the encoder lines? internal pullups are weak and wouldn't usually be enough
for a signal that goes off some distance in a cable run. 4k7 is a good general value for a physical pullup in a moderate
electrical environment (such as other logic signals).
 
Hello MarkT. I don't have pullup resistors, just 2x 0.1uF capacitors routed in parallel with the encoder encoder switch.

I solved it! But I still don't understand exactly why it broke.

So the trend holds true that when I commented out tft.init(135,240) the encoder function would restore. MarkT made me curious if this was a hardware issue to probed the encoder switches on my scope. I found that whenever the encoder wasn't working, pin 1 would be stuck at 0V while the other pin (pin 2) would switch as expected. When I comment out tft.init() pin 1 was start switching again as expected. So I thought maybe something is up with the internal pullup resistor.

So I added pinMode(1,INPUT_PULLUP) after tft.init(). That fixed the issue. I'm surprised at this because the other pin on the encoder (pin 2) as well as the two pins of my second encoder (pin 4 and 5) are not affected. Is there something in tft.init() that could affect the pinMode of pin 1?
 
Probably a long shot, but you might check, first where you are getting the ST7735_t3 and ...89 libraries from (in case you might have version in your arduino folders\libraries ...
Then check that the #define in ST7735_t3.cpp is commented out:
Code:
#ifdef ENABLE_ST77XX_FRAMEBUFFER
//#define DEBUG_ASYNC_UPDATE
[COLOR="#B22222"]//#define DEBUG_ASYNC_LEDS[/COLOR]
#ifdef DEBUG_ASYNC_LEDS
  #define DEBUG_PIN_1 0
  #define DEBUG_PIN_2 1
  #define DEBUG_PIN_3 2
#endif
If that line is uncommented, then it will use the three debug pins below it for debug, with pinMode and digitalWriets... I believe I verified all of the debug code is under that #ifdef...
 
Status
Not open for further replies.
Back
Top