Advertisement
pleasedontcode

**Gauge Display** rev_01

Dec 28th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Gauge Display**
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-12-28 18:45:41
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* convert code for arduino */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <TFT_eSPI.h>   //https://github.com/Bodmer/TFT_eSPI
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  33. TFT_eSPI tft = TFT_eSPI();
  34. TFT_eSprite img = TFT_eSprite(&tft);
  35. TFT_eSprite ln = TFT_eSprite(&tft);
  36.  
  37. double rad = 0.01745;
  38. int angle;
  39.  
  40. int sx = 120;
  41. int sy = 120;
  42. int r = 76;
  43.  
  44. float x[360];
  45. float y[360];
  46. float x2[360];
  47. float y2[360];
  48.  
  49. int chosenOne = 0;
  50. int minValue[6] = {0, 20, 0, 0, 0, 80};
  51. int maxValue[6] = {40, 100, 60, 80, 70, 160};
  52. int dbounce = 0;
  53.  
  54. void setup() {
  55.     // put your setup code here, to run once:
  56.    
  57.     pinMode(12, INPUT_PULLUP);
  58.    
  59.     tft.init();
  60.     tft.setRotation(0);
  61.     tft.setSwapBytes(true);
  62.     img.setSwapBytes(true);
  63.     tft.fillScreen(TFT_ORANGE);
  64.     img.createSprite(240, 240);
  65.    
  66.     tft.setPivot(60, 60);
  67.     img.setTextDatum(4);
  68.     img.setTextColor(TFT_BLACK, 0xAD55);
  69.     img.setFreeFont(&Orbitron_Medium_28);
  70.  
  71.     int i = 0;
  72.     int a = 136;
  73.  
  74.     while (a != 44) {
  75.         x[i] = r * cos(rad * a) + sx;
  76.         y[i] = r * sin(rad * a) + sy;
  77.         x2[i] = (r - 20) * cos(rad * a) + sx;
  78.         y2[i] = (r - 20) * sin(rad * a) + sy;
  79.         i++;
  80.         a++;
  81.         if (a == 360)
  82.             a = 0;
  83.     }
  84. }
  85.  
  86. //min angle 136 or 137
  87. //max angle 43
  88.  
  89. int a1, a2;
  90. int result = 0;
  91.  
  92. void loop() {
  93.     if (digitalRead(12) == 0) {
  94.         if (dbounce == 0) {
  95.             dbounce = 1;
  96.             chosenOne++;
  97.             if (chosenOne >= 6)
  98.                 chosenOne = 0;
  99.         }
  100.     } else dbounce = 0;
  101.  
  102.     result = map(analogRead(14), 0, 4095, minValue[chosenOne], maxValue[chosenOne]);
  103.     angle = map(result, minValue[chosenOne], maxValue[chosenOne], 0, 267);
  104.  
  105.     if (chosenOne == 0)
  106.         img.pushImage(0, 0, 240, 240, gauge1);
  107.     if (chosenOne == 1)
  108.         img.pushImage(0, 0, 240, 240, gauge2);
  109.     if (chosenOne == 2)
  110.         img.pushImage(0, 0, 240, 240, gauge3);
  111.     if (chosenOne == 3)
  112.         img.pushImage(0, 0, 240, 240, gauge4);
  113.     if (chosenOne == 4)
  114.         img.pushImage(0, 0, 240, 240, gauge5);
  115.     if (chosenOne == 5)
  116.         img.pushImage(0, 0, 240, 240, gauge6);
  117.  
  118.     if (chosenOne == 5)
  119.         img.drawFloat(result / 10.00, 2, 120, 114);
  120.     else if (chosenOne == 4)
  121.         img.drawString(String(result * 100), 120, 114);
  122.     else
  123.         img.drawString(String(result), 120, 114);
  124.  
  125.     a1 = angle - 4;
  126.     a2 = angle + 4;
  127.  
  128.     if (a1 < 0)
  129.         a1 = angle - 4 + 359;
  130.     if (a2 >= 359)
  131.         a2 = angle + 4 - 359;
  132.  
  133.     if (result <= minValue[chosenOne] + 4)
  134.         img.fillTriangle(x[angle], y[angle], x2[angle], y2[angle], x2[a2 + 2], y2[a2 + 2], TFT_RED);
  135.     else if (result >= maxValue[chosenOne] - 4)
  136.         img.fillTriangle(x[angle], y[angle], x2[a1 - 2], y2[a1 - 2], x2[angle], y2[angle], TFT_RED);
  137.     else
  138.         img.fillTriangle(x[angle], y[angle], x2[a1], y2[a1], x2[a2], y2[a2], TFT_RED);
  139.  
  140.     img.pushSprite(0, 0);
  141. }
  142.  
  143. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement