TigerBalm2
Member
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!
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);
}
}