Hello, I want to display sensor value (load cell) in 2.8" TFT touchscreen mcufriend 0x4747, but I get bad result, the value is flickering like in video. I use mcufriend_kbv and HX711 library, here is my code
#include "HX711.h"
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
//-----------------hx711------------------------
HX711 scale(22, 24);
float calibration_factor = -200.91; // this calibration factor is adjusted according to my load cell
int units;
float ounces;
//-----------------lcd------------------------------
uint8_t YP = A1; // must be an analog pin, use "An" notation!
uint8_t XM = A2; // must be an analog pin, use "An" notation!
uint8_t YM = 7; // can be a digital pin
uint8_t XP = 6; // can be a digital pin
#define TS_MINX 180
#define TS_MINY 182
#define TS_MAXX 922
#define TS_MAXY 920
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#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 10
#define MAXPRESSURE 1000
void setup() {
Serial.begin(9600);
//-----------------------------------
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average();
//---------------------------------
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
Serial.print(identifier);
}
void loop() {
Serial.println(scale.get_units());
tft.fillScreen(WHITE);
tft.setCursor(130,100);
tft.setTextColor(BLACK);
tft.setRotation(1);
tft.setTextSize(4);
tft.print(scale.get_units());
}
What should I do?
Am I do some mistake in my code?
i'm sorry it looks I'm typo, I mean tft.fillScreen(WHITE). If I put tft.fillScreen(White) in void setup, previous value is left behind, like in attachment. I understand setup() and loop(), the reason I put tft.fillScreen(WHITE) is to refresh the display.
The Library Manager could not find an "official" HX711 library. So I was not able to reproduce your program exactly.
However, this example illustrates two ways of printing a varying number. I use a YELLOW background to highlight the printed characters.
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
void setup()
{
Serial.begin(9600);
uint16_t identifier = tft.readID();
tft.begin(identifier);
Serial.print(identifier);
tft.setRotation(1);
tft.fillScreen(WHITE);
tft.setTextSize(4);
tft.setTextColor(BLACK, YELLOW); //set back colour for rubout
}
void loop()
{
float result = 0.01 * random(-10000, 30000); //-100.0 to +300.00
Serial.println(result, 2);
tft.setCursor(130, 100);
tft.print(result, 2); //2 decimals
tft.print(" "); //rubs out old digit
tft.setCursor(130, 140);
char buf[10];
dtostrf(result, 6, 2, buf); //width=6, prec=2
tft.print(buf); //-99.99 to 299.99
delay(1000); //wait between readings
}
Note that the "trailing space" method works with similar numbers but goes wrong when the result width varies.
Using dtostrf() gives you nice formatted output. You can even use it for integers if you use prec=0.
thank you so much David, this works. sorry it looks like I have made a mistake. in my previous sketch written setTextColor (BLACK). I must check my sketch more carefully