My code runs, but when the encoder reaches a value for something to happen the screen simply goes blank.
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "ClickEncoder.h"
//colors
#define BLACK 0x0000
#define GRAY 0x8410
#define WHITE 0xFFFF
#define RED 0xF800
#define ORANGE 0xFA60
#define YELLOW 0xFFE0
#define LIME 0x07FF
#define GREEN 0x07E0
#define CYAN 0x07FF
#define AQUA 0x04FF
#define BLUE 0x001F
#define MAGENTA 0xF81F
#define PINK 0xF8FF
//pins for lcd display
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_M 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST);
#define inputCLK A0
#define inputSW A2
#define inputDT A1
// LED Outputs
#define ledCW 1
#define ledCCW 1
int counter = 0;
int currentStateCLK;
int previousStateCLK;
unsigned long lastButtonPress = 0;
String encdir ="";
void setup(){
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
pinMode (inputSW,INPUT_PULLUP);
pinMode(A3, OUTPUT);
digitalWrite(A3, HIGH);
// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);
// Setup Serial Monitor
Serial.begin (9600);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
previousStateCLK = digitalRead(inputSW);
tft.begin();
tft.setRotation(3); // orientate screen
pinMode(0, OUTPUT); // sets the digital pin 0 as output
pinMode(1, OUTPUT); // sets the digital pin 1 as output
pinMode(2, OUTPUT); // sets the digital pin 2 as output
pinMode(3, OUTPUT); // sets the digital pin 3 as output
pinMode(4, OUTPUT); // sets the digital pin 4 as output
pinMode(5, OUTPUT); // sets the digital pin 5 as output
pinMode(6, OUTPUT); // sets the digital pin 6 as output
pinMode(7, OUTPUT); // sets the digital pin 7 as output
pinMode(A5, OUTPUT); // sets the digital pin A5 as output
tft.fillScreen(GRAY); //wallpaper
tft.fillRect(0, 0, 320, 25, BLUE); //Title at the top of the screen
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(20,5);
tft.print("FrameJet Control System");
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(10,30);
tft.print("Settings");
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(10,55);
tft.print("Forward Cluster");
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(10,80);
tft.print("Center Cluster");
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(10,105);
tft.print("Rear Cluster");
}
void loop()
{
// MENU STARTS HERE
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating clockwise
if (digitalRead(inputDT) != currentStateCLK) {
counter ++;
encdir ="CW";
digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, HIGH);
} else {
// Encoder is rotating counterclockwise
counter --;
encdir ="CCW";
digitalWrite(ledCW, HIGH);
digitalWrite(ledCCW, LOW);
}
Serial.print("Direction: ");
Serial.print(encdir);
Serial.print(" -- Value: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(inputSW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
if(counter == 20){
Serial.println("Settings");
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.setCursor(10,30);
tft.print("Settings");
}
}