#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#define TFT_CS 44
#define TFT_RST -1
#define TFT_DC 43
#define TFT_MOSI 47
#define TFT_SCLK 21
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
const int potPin = 13;
int lastAdcValue = -1;
const int threshold = 10;
void setup() {
Serial.begin(115200);
tft.init(240, 240);
tft.setRotation(2);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
analogReadResolution(12);
tft.setTextColor(ST77XX_CYAN);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("ADC Pin: GPIO13");
}
void clearLine(int yPos, int length) {
tft.setCursor(10, yPos);
for (int i = 0; i < length; i++) {
tft.print(" ");
}
}
void loop() {
int adcValue = analogRead(potPin);
if (abs(adcValue - lastAdcValue) > threshold) {
float voltage = adcValue * (3.3 / 4095.0);
float angle = (adcValue * 270.0) / 4095.0;
tft.fillRect(0, 40, 240, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_YELLOW);
tft.setCursor(10, 40);
tft.print("ADC Range: 0 - 4095");
tft.fillRect(0, 60, 240, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(10, 60);
tft.print("ADC Value: ");
tft.println(adcValue);
tft.fillRect(0, 90, 240, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_GREEN);
tft.setCursor(10, 90);
tft.print("Volt Range: 0-3.3V");
tft.fillRect(0, 110, 240, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(10, 110);
tft.print("Voltage: ");
tft.print(voltage, 2);
tft.println(" V");
tft.fillRect(0, 140, 240, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_MAGENTA);
tft.setCursor(10, 140);
tft.print("Angle Range: 0-270");
tft.fillRect(0, 160, 240, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(10, 160);
tft.print("Angle: ");
tft.print(angle, 1);
tft.println(" deg");
lastAdcValue = adcValue;
}
delay(200);
}