// Demonstrate graph widget functions with two independent trace instances
// Multiple traces can be drawn at a time with multiple trace instances
// Note: Traces are automatically clipped at graph boundaries by widget library
// Requires widget library here:
// https://github.com/Bodmer/TFT_eWidget
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
#include <TFT_eWidget.h> // Widget library
GraphWidget gr = GraphWidget(&tft); // Graph widget
const int potpin1 = 35;
int value1 = 0;
//int vin1Value; // do not change
//float voltage = 0; // do not change
// Traces are drawn on tft using graph instance
TraceWidget tr1 = TraceWidget(&gr); // Graph trace 1
TraceWidget tr2 = TraceWidget(&gr); // Graph trace 2
TraceWidget tr3 = TraceWidget(&gr); // Graph trace 3
TraceWidget tr4 = TraceWidget(&gr); // Graph trace 4
void setup() {
Serial.begin(115200);
pinMode(potpin1, INPUT);
delay(1000);
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// Graph area is 300 pixels wide, 150 high, dark grey background
gr.createGraph(300, 150, tft.color565(5, 5, 5));
// x scale units is from 0 to 100, y scale units is -50 to 50
gr.setGraphScale(0.0, 100.0, 0.0, 5.0);
// X grid starts at 0 with lines every 10 x-scale units
// Y grid starts at -50 with lines every 25 y-scale units
// blue grid
gr.setGraphGrid(0.0, 10.0, 0.0, 1.0, TFT_BLUE);
// Draw empty graph, top left corner at 0,0 on TFT
gr.drawGraph(0, 0);
// Start a trace with using red and another with green
tr1.startTrace(TFT_RED);
// tr2.startTrace(TFT_GREEN);
// tr3.startTrace(TFT_YELLOW);
//tr4.startTrace(TFT_BLUE);
// Add points on graph to trace 1 using graph scale factors
tr1.addPoint(0.0, 0.0);
tr1.addPoint(100.0, 0.0);
/*
// Add points on graph to trace 2 using graph scale factors
// Points are off graph so the plotted line is clipped to graph area
tr2.addPoint(0.0, -100.0);
tr2.addPoint(100.0, 100.0);
// Add points on graph to trace 3 using graph scale factors
tr3.addPoint(0.0, 50.0);
tr3.addPoint(50.0, 0.0);
// Add points on graph to trace 4 using graph scale factors
// Points are off graph so the plotted line is clipped to graph area
tr4.addPoint(0.0, -75.0);
tr4.addPoint(75.0, 75.0);
*/
// Get x,y pixel coordinates of any scaled point on graph
// and ring that point.
// tft.drawCircle(gr.getPointX(50.0), gr.getPointY(0.0), 5, TFT_MAGENTA);
// Draw the x axis scale
/* tft.setTextDatum(TC_DATUM); // Top centre text datum
tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(-50.0) + 3);
tft.drawNumber(50, gr.getPointX(50.0), gr.getPointY(-50.0) + 3);
tft.drawNumber(100, gr.getPointX(100.0), gr.getPointY(-50.0) + 3);
*/
// Draw the y axis scale
tft.setTextDatum(MR_DATUM); // Middle right text datum
tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(-50.0));
tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(0.0));
tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(50.0));
// Draw channel labels
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(1);
tft.drawString("Ch1 = ", 50, 175);
/* tft.drawString("Ch2 = ", 120, 175);
tft.drawString("Ch3 = ", 190, 175);
tft.drawString("Ch4 = ", 260, 175);
*/
// Restart traces with new colours
//tr1.startTrace(TFT_WHITE);
//tr2.startTrace(TFT_YELLOW);
}
void loop() {
static uint32_t plotTime = millis();
static float gx = 0.0, //value1 = 0.0;
// static float delta = 7.0;
value1 = analogRead(potpin1);
Serial.println(value1);
// Sample periodically
if (millis() - plotTime >= 100) {
plotTime = millis();
// Add a new point on each trace
tr1.addPoint(gx, value1);
/* tr2.addPoint(gx, gy / 2.0); // half y amplitude
tr3.addPoint(gx, gy / 4);
tr4.addPoint(gx, gy / 3.0); // half y amplitude
*/
// display channel values
//float f = gy
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(1);
tft.fillRect(50, 170, 25, 10, TFT_BLACK);
tft.drawNumber(value1, 70, 175);
/* tft.fillRect(120, 170, 25, 10, TFT_BLACK);
tft.drawNumber(gy / 2, 140, 175);
tft.fillRect(180, 170, 25, 10, TFT_BLACK);
tft.drawNumber(gy, 200, 175);
tft.fillRect(250, 170, 25, 10, TFT_BLACK);
tft.drawNumber(gy / 2, 270, 175);
*/
// Create next plot point
gx += 1.0;
/* gy += delta;
if (gy > 70.0) {
delta = -7.0;
gy = 70.0;
}
if (gy < -70.0) {
delta = 7.0;
gy = -70.0;
}
*/
// If the end of the graph is reached start 2 new traces
if (gx > 100.0) {
gx = 0.0;
// gy = 0.0;
// Draw empty graph at 40,10 on display
gr.drawGraph(0, 0);
// Start new trace
tr1.startTrace(TFT_RED);
/* tr2.startTrace(TFT_GREEN);
tr3.startTrace(TFT_YELLOW);
tr4.startTrace(TFT_BLUE);
*/
}
}
}Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1
Loading
ili9341-cap-touch
ili9341-cap-touch