Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GOOD 14/05/2025
- #include <Adafruit_GFX.h>
- #include <Adafruit_ILI9341.h>
- #include <SPI.h>
- //For Cheap Yellow Display
- //#define TFT_MISO 12
- //#define TFT_MOSI 13 //15 // In some display driver board, it might be written as "SDA" and so on.
- //#define TFT_SCLK 14 //14
- //#define TFT_CS 15 //5 // Chip select control pin
- //#define TFT_DC 2 //27 // Data Command control pin
- //#define TFT_RST -1 //33 // Reset pin (could connect to Arduino RESET pin)
- // TFT Pins (ESP32C6 Waveshare)
- #define TFT_MOSI 19
- #define TFT_MISO 5
- #define TFT_SCK 21
- #define TFT_CS 18 //5
- #define TFT_RST 1
- #define TFT_DC 0
- //For ESP32C3 Super mini
- //#define TFT_MOSI 6 //15 // In some display driver board, it might be written as "SDA" and so on.
- //#define TFT_SCK 4 //14
- //#define TFT_CS 5 //5 // Chip select control pin
- //#define TFT_DC 7 //27 // Data Command control pin
- //#define TFT_RST 1 //33 // Reset pin (could connect to Arduino RESET pin)
- //#define TFT_MISO -1
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI,TFT_SCK,TFT_RST,TFT_MISO);
- #include "DHT.h"
- #define DHTPIN 5
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- // Gauge positions and settings
- const int gauge1X = 75, gauge1Y = 120;
- const int gauge2X = 245, gauge2Y = 120;
- const int radius = 75;
- const int needleLength = radius - 20;
- float currentValue1 = 0;
- float currentValue2 = 0;
- float previousAngle1 = -60;
- float previousAngle2 = -60;
- void setup() {
- Serial.begin(115200);
- dht.begin();
- tft.begin();
- tft.setRotation(1);
- tft.fillScreen(ILI9341_BLACK);
- fixedLabel();
- drawGaugeBackground(gauge1X, gauge1Y, -20, 80);
- drawGaugeBackground(gauge2X, gauge2Y, 0, 100);
- drawNeedle(gauge1X, gauge1Y, currentValue1, -20, 80, previousAngle1);
- drawNeedle(gauge2X, gauge2Y, currentValue2, 0, 100, previousAngle2);
- showValue(gauge1X, gauge1Y, currentValue1);
- showValue(gauge2X, gauge2Y, currentValue2);
- }
- void loop() {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- delay(250);
- float val1=t;
- float val2=h;
- updateNeedle(gauge1X, gauge1Y, val1, -20, 80, previousAngle1);
- updateNeedle(gauge2X, gauge2Y, val2, 0, 100, previousAngle2);
- }
- void drawGaugeBackground(int cx, int cy, int minVal, int maxVal) {
- for (int i = -60; i <= 240; i++) {
- float a = radians(i);
- int x = cx + cos(a) * radius;
- int y = cy - sin(a) * radius;
- tft.drawPixel(x, y, ILI9341_WHITE);
- }
- for (int i = minVal; i <= maxVal; i += 2) {
- float angle = map(i, minVal, maxVal, 240, -60); //-60,240
- float aRad = radians(angle);
- int x0 = cx + cos(aRad) * (radius - 10);
- int y0 = cy - sin(aRad) * (radius - 10);
- int x1 = cx + cos(aRad) * radius;
- int y1 = cy - sin(aRad) * radius;
- uint16_t color = (i % 10 == 0) ? ILI9341_BLUE : ILI9341_GREEN;
- tft.drawLine(x0, y0, x1, y1, color);
- if (i % 10 == 0) {
- int lx = cx + cos(aRad) * (radius - 30);
- int ly = cy - sin(aRad) * (radius - 30);
- tft.setCursor(lx - 10, ly - 7);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(1);
- tft.print(i);
- }
- }
- }
- void drawNeedle(int cx, int cy, float value, int minVal, int maxVal, float &prevAngle) {
- float angle = map(value, minVal, maxVal, 240, -60); //-60, 240
- float aRad = radians(angle);
- int x = cx + cos(aRad) * needleLength;
- int y = cy - sin(aRad) * needleLength;
- // Erase old needle
- float prevRad = radians(prevAngle);
- int px = cx + cos(prevRad) * needleLength;
- int py = cy - sin(prevRad) * needleLength;
- tft.drawLine(cx, cy, px, py, ILI9341_BLACK);
- // Draw new needle
- tft.drawLine(cx, cy, x, y, ILI9341_RED);
- prevAngle = angle;
- }
- void showValue(int cx, int cy, float value) {
- tft.fillRect(cx - 30, cy + 80, 60, 20, ILI9341_BLACK);
- tft.setCursor(cx - 25, cy + 80);
- tft.setTextColor(ILI9341_YELLOW);
- tft.setTextSize(2);
- tft.print(value, 1);
- }
- void updateNeedle(int cx, int cy, float value, int minVal, int maxVal, float &prevAngle) {
- drawNeedle(cx, cy, value, minVal, maxVal, prevAngle);
- showValue(cx, cy, value);
- }
- void fixedLabel() {
- tft.setCursor(gauge1X-18,gauge1Y+50);
- tft.setTextColor(ILI9341_GREEN);
- tft.setTextSize(1);
- tft.print("DEG C");
- tft.setCursor(gauge2X-12,gauge2Y+50);
- tft.setTextColor(ILI9341_GREEN);
- tft.setTextSize(1);
- tft.print("%RH");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement