#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 8
#define TFT_RST 9
int xValue = 0 ;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
void setup() {
pinMode(A5, INPUT);
Serial.begin(9600);
tft.begin();
tft.setRotation(3);
}
void plotGraph() {
tft.fillScreen(ILI9341_BLACK); // Clear the screen
// Draw X-axis
tft.drawLine(0, tft.height() / 2, tft.width(), tft.height() / 2, ILI9341_WHITE);
// Draw Y-axis
tft.drawLine(tft.width() / 2, 0, tft.width() / 2, tft.height(), ILI9341_WHITE);
// Calculate the center of the screen
int centerX = tft.width() / 2;
int centerY = tft.height() / 2;
// Number of data points to read
const long numPoints = 1022;
// Read and plot data
for (int i = 0; i < numPoints; ++i) {
// Read X and Y values from analog pins
xValue = xValue + 1 ;
int yValue = analogRead(A5); // voltage value
int aValue = yValue * (5.0 / 1023.0); // current value
// Map and shift the values
int xMapped = map(xValue, 0, 1023, -centerX, centerX);
int yMapped = map(yValue, 0, 1023, centerY, -centerY);
// Shift the points to start from the center
int xPlot = centerX + xMapped;
int yPlot = centerY - yMapped;
// Draw a point at the current position
tft.drawPixel(xPlot, yPlot, ILI9341_GREEN);
// Add a delay to slow down the plot (adjust as needed)
delay(50);
}
}
void loop() {
plotGraph();
// Delay for 5 seconds before clearing the graph
}