Working on a project where i need to display live sensor values on a 3.5in TFT Touchscreen LCD. The current method im using does the job, but if you blink you'll missing the displayed value. On top of that theres a long period of no displayed value on the screen. It simply makes a blacked out rectangle over the old value and the new value prints over the rectangle. Is there a way i can have an easy to read value displayed without it flickering?
Heres the current code im using:
// INCLUDE
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <pin_magic.h>
#include <registers.h>
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <Wire.h>
//DEFINE
#if defined(SAM3X8E)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin
#define TS_MINX 130
#define TS_MAXX 905
#define TS_MINY 75
#define TS_MAXY 930
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define MINPRESSURE 5
#define MAXPRESSURE 1000
#define PENRADIUS 2
//INT
int speed1;
int change;
int percentage;
int lsdtreading;
int in1 = 22;
int in2 = 24;
int ENA = 13;
int distance;
int length1 = 76.2; //3 in = 76.2 mm
//OTHER
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
String inString = ""; // string to hold input
void Motor_Setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ENA, OUTPUT);
digitalWrite(24, LOW);
digitalWrite(22, HIGH);
}
void mainUI() {
tft.setRotation(1);
tft.fillScreen(BLACK);
//HEADER
//tft.setRotation(3);
tft.setCursor(25, 20);
tft.setTextSize(3);
tft.println("3451L FLYBALL TACHOMETER");
tft.fillRect(15, 60, 450, 2, RED);
tft.fillRect(15, 95, 450, 2, RED);
tft.fillRect(15, 130, 450, 2, RED);
tft.fillRect(15, 165, 450, 2, RED);
tft.fillRect(15, 200, 450, 2, RED);
tft.fillRect(15, 235, 450, 2, RED);
tft.fillRect(15, 270, 450, 2, RED);
tft.fillRect(15, 305, 450, 2, RED);
tft.fillRect(15, 60, 2, 247, RED);
tft.fillRect(245, 60, 2, 247, RED);
tft.fillRect(465, 60, 2, 247, RED);
tft.setTextSize(3);
tft.setCursor(25, 70);
tft.println("Motor Output");
tft.setCursor(25, 105);
tft.println("Motor (R)");
tft.setCursor(25, 140);
tft.println("Shaft (R)");
tft.setCursor(25, 175);
tft.println("Height (E)");
tft.setCursor(25, 210);
tft.println("Angle (E)");
tft.setCursor(25, 245);
tft.println("Shaft (E)");
tft.setCursor(25, 280);
tft.println("Error");
tft.setCursor(441, 70);
tft.println("%");
tft.setCursor(405, 105);
tft.println("RPM");
tft.setCursor(405, 140);
tft.println("RPM");
tft.setCursor(422, 175);
tft.println("mm");
tft.setCursor(405, 210);
tft.println("deg");
tft.setCursor(405, 245);
tft.println("RPM");
tft.setCursor(441, 280);
tft.println("%");
}
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
while (!Serial); // wait for
tft.reset();
tft.begin(0x9486);
Motor_Setup();
mainUI();
}
void MotorA() {
speed1 = map(analogRead(A15), 1024, 0, 0, 255);
/*
speed1 = 1024 - analogRead(A15);
speed1 = speed1 * 0.2492668622;
*/
analogWrite(ENA, speed1);
//Serial.println(speed1);
//Motor Display
tft.setTextSize(3);
percentage = (speed1 * 100) / 255;
//Serial.println(percentage);
if (percentage < 10) {
tft.setCursor(370, 70);
tft.println(percentage);
}
else if (percentage < 100) {
tft.setCursor(355, 70);
tft.println(percentage);
}
else {
tft.setCursor(335, 70);
tft.println(percentage);
}
}
void RPM() {
char myRPM[5];
while (Serial1.available() > 0) {
byte m = Serial1.readBytesUntil('\n', myRPM, 5);
myRPM[m] = '\0';
float rpm = atof(myRPM);
float rpm2 = rpm * 0.6;
Serial.println(rpm, 2);
tft.setTextSize(3);
if (rpm > 300) {
//Motor Display
tft.setCursor(300, 105);
tft.println(rpm, 0);
//Shaft Display
tft.setCursor(300, 140);
tft.println(rpm2, 0);
delay(50);
}
}
}
void Height() {
char distance1[5];
while (Serial2.available() > 0) {
byte m = Serial2.readBytesUntil('\n', distance1, 5);
distance1[m] = '\0';
int height0 = atoi(distance1)-35;
//float height1 = height0 * 0.0393701 - 0.9;//Inches
Serial.println(height0);
tft.setTextSize(3);
//Height Display
tft.setCursor(320, 175);
tft.println(height0);
//Angle Display
int angle = acos(height0 / (2 * length1));
tft.setCursor(340, 210);
tft.println(angle);
//Vertical Velocity
//Experimental RPM
//E.RPM = (v.VELOCITY) / (-2 * lenght1 * sin(acos(distance / (2 * length1))));
}
}
void refresh() {
delay(1);
tft.fillRect(247, 62, 155, 32, BLACK);
tft.fillRect(247, 97, 155, 32, BLACK);
tft.fillRect(247, 132, 155, 32, BLACK);
tft.fillRect(247, 167, 175, 32, BLACK);
tft.fillRect(247, 202, 155, 32, BLACK);
tft.fillRect(247, 237, 155, 32, BLACK);
tft.fillRect(247, 272, 155, 32, BLACK);
}
void loop() {
MotorA();
RPM();
Height();
delay(100);
refresh();
}