Advertisement
Tywais

2_METERS_ILI9431_ESP32C6

May 19th, 2025 (edited)
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | Source Code | 0 0
  1. //GOOD 14/05/2025
  2.  
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_ILI9341.h>
  5. #include <SPI.h>
  6.  
  7. //For Cheap Yellow Display
  8. //#define TFT_MISO 12
  9. //#define TFT_MOSI 13 //15 // In some display driver board, it might be written as "SDA" and so on.
  10. //#define TFT_SCLK 14 //14
  11. //#define TFT_CS   15 //5  // Chip select control pin
  12. //#define TFT_DC   2 //27  // Data Command control pin
  13. //#define TFT_RST  -1 //33  // Reset pin (could connect to Arduino RESET pin)
  14.  
  15. // TFT Pins (ESP32C6 Waveshare)
  16. #define TFT_MOSI  19
  17. #define TFT_MISO  5
  18. #define TFT_SCK   21
  19. #define TFT_CS    18 //5
  20. #define TFT_RST   1
  21. #define TFT_DC    0
  22.  
  23. //For ESP32C3 Super mini
  24. //#define TFT_MOSI 6 //15 // In some display driver board, it might be written as "SDA" and so on.
  25. //#define TFT_SCK  4 //14
  26. //#define TFT_CS   5 //5  // Chip select control pin
  27. //#define TFT_DC   7 //27  // Data Command control pin
  28. //#define TFT_RST  1 //33  // Reset pin (could connect to Arduino RESET pin)
  29. //#define TFT_MISO  -1
  30.  
  31. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI,TFT_SCK,TFT_RST,TFT_MISO);
  32.  
  33. #include "DHT.h"
  34. #define DHTPIN 5
  35. #define DHTTYPE DHT22
  36. DHT dht(DHTPIN, DHTTYPE);
  37. // Gauge positions and settings
  38. const int gauge1X = 75, gauge1Y = 120;
  39. const int gauge2X = 245, gauge2Y = 120;
  40. const int radius = 75;
  41. const int needleLength = radius - 20;
  42.  
  43. float currentValue1 = 0;
  44. float currentValue2 = 0;
  45. float previousAngle1 = -60;
  46. float previousAngle2 = -60;
  47.  
  48. void setup() {
  49.   Serial.begin(115200);
  50.   dht.begin();
  51.   tft.begin();
  52.   tft.setRotation(1);
  53.   tft.fillScreen(ILI9341_BLACK);
  54.   fixedLabel();
  55.  
  56.   drawGaugeBackground(gauge1X, gauge1Y, -20, 80);
  57.   drawGaugeBackground(gauge2X, gauge2Y, 0, 100);
  58.  
  59.   drawNeedle(gauge1X, gauge1Y, currentValue1, -20, 80, previousAngle1);
  60.   drawNeedle(gauge2X, gauge2Y, currentValue2, 0, 100, previousAngle2);
  61.   showValue(gauge1X, gauge1Y, currentValue1);
  62.   showValue(gauge2X, gauge2Y, currentValue2);
  63. }
  64.  
  65. void loop() {
  66.   float h = dht.readHumidity();
  67.   float t = dht.readTemperature();
  68.   delay(250);
  69.  
  70.   float val1=t;
  71.   float val2=h;
  72.   updateNeedle(gauge1X, gauge1Y, val1, -20, 80, previousAngle1);
  73.   updateNeedle(gauge2X, gauge2Y, val2, 0, 100, previousAngle2);
  74.  
  75. }
  76.  
  77. void drawGaugeBackground(int cx, int cy, int minVal, int maxVal) {
  78.   for (int i = -60; i <= 240; i++) {
  79.     float a = radians(i);
  80.     int x = cx + cos(a) * radius;
  81.     int y = cy - sin(a) * radius;
  82.     tft.drawPixel(x, y, ILI9341_WHITE);
  83.   }
  84.  
  85.   for (int i = minVal; i <= maxVal; i += 2) {
  86.     float angle = map(i, minVal, maxVal, 240, -60); //-60,240
  87.     float aRad = radians(angle);
  88.     int x0 = cx + cos(aRad) * (radius - 10);
  89.     int y0 = cy - sin(aRad) * (radius - 10);
  90.     int x1 = cx + cos(aRad) * radius;
  91.     int y1 = cy - sin(aRad) * radius;
  92.  
  93.     uint16_t color = (i % 10 == 0) ? ILI9341_BLUE : ILI9341_GREEN;
  94.     tft.drawLine(x0, y0, x1, y1, color);
  95.  
  96.     if (i % 10 == 0) {
  97.       int lx = cx + cos(aRad) * (radius - 30);
  98.       int ly = cy - sin(aRad) * (radius - 30);
  99.       tft.setCursor(lx - 10, ly - 7);
  100.       tft.setTextColor(ILI9341_WHITE);
  101.       tft.setTextSize(1);
  102.       tft.print(i);
  103.     }
  104.   }
  105. }
  106.  
  107. void drawNeedle(int cx, int cy, float value, int minVal, int maxVal, float &prevAngle) {
  108.   float angle = map(value, minVal, maxVal, 240, -60);  //-60, 240
  109.   float aRad = radians(angle);
  110.   int x = cx + cos(aRad) * needleLength;
  111.   int y = cy - sin(aRad) * needleLength;
  112.  
  113.   // Erase old needle
  114.   float prevRad = radians(prevAngle);
  115.   int px = cx + cos(prevRad) * needleLength;
  116.   int py = cy - sin(prevRad) * needleLength;
  117.   tft.drawLine(cx, cy, px, py, ILI9341_BLACK);
  118.  
  119.   // Draw new needle
  120.   tft.drawLine(cx, cy, x, y, ILI9341_RED);
  121.   prevAngle = angle;
  122. }
  123.  
  124. void showValue(int cx, int cy, float value) {
  125.   tft.fillRect(cx - 30, cy + 80, 60, 20, ILI9341_BLACK);
  126.   tft.setCursor(cx - 25, cy + 80);
  127.   tft.setTextColor(ILI9341_YELLOW);
  128.   tft.setTextSize(2);
  129.   tft.print(value, 1);
  130. }
  131.  
  132. void updateNeedle(int cx, int cy, float value, int minVal, int maxVal, float &prevAngle) {
  133.   drawNeedle(cx, cy, value, minVal, maxVal, prevAngle);
  134.   showValue(cx, cy, value);
  135. }
  136.  
  137. void fixedLabel() {
  138.   tft.setCursor(gauge1X-18,gauge1Y+50);
  139.   tft.setTextColor(ILI9341_GREEN);
  140.   tft.setTextSize(1);
  141.   tft.print("DEG C");
  142.  
  143.   tft.setCursor(gauge2X-12,gauge2Y+50);
  144.   tft.setTextColor(ILI9341_GREEN);
  145.   tft.setTextSize(1);
  146.   tft.print("%RH");
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement