Guest User

ST7735 per lettura tensione

a guest
Jan 13th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1.  
  2. #include <Adafruit_GFX.h> // Core graphics library
  3. #include <Adafruit_ST7735.h> // Hardware-specific library
  4. #include <SPI.h>
  5.  
  6.  
  7. #define TFT_CS 10
  8. #define TFT_RST 9 // you can also connect this to the Arduino reset
  9. // in which case, set this #define pin to 0!
  10. #define TFT_DC 8
  11.  
  12. // Option 1 (recommended): must use the hardware SPI pins
  13. // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
  14. // an output. This is much faster - also required if you want
  15. // to use the microSD card (see the image drawing example)
  16. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  17.  
  18. // Option 2: use any pins but a little slower!
  19. #define TFT_SCLK 13 // set these to be whatever pins you like!
  20. #define TFT_MOSI 11 // set these to be whatever pins you like!
  21. //Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  22. unsigned long TimerAnalog, TimerOff = 0;
  23. #define TIME_ANALOG 3000
  24. #define TIME_OFF 10000
  25.  
  26. int sensorValue = 0;
  27. float PERCENTUALE, TOTALE = 0.0;
  28. int Rele = 4; //INSERISCO LA VARIABILE RELE' AL PIN 10
  29. int POWERBANK = 6; // INSERISCO LA VARIABILE POWERBANK AL PIN 6
  30.  
  31. void setup(void) {
  32. Serial.begin(9600);
  33. Serial.print("Hello! ST7735 TFT Test");
  34.  
  35. // Use this initializer if you're using a 1.8" TFT
  36. tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
  37. pinMode(6, OUTPUT);
  38. pinMode(4, OUTPUT);
  39. pinMode(5, INPUT_PULLUP);
  40.  
  41. }
  42.  
  43. void loop() {
  44.  
  45. // Eseguiamo la lettura di A1 ogni TIME_ANALOG secondi
  46. if ((millis() - TimerAnalog > TIME_ANALOG)) {
  47. TimerAnalog = millis();
  48. sensorValue = analogRead(A1);
  49. // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  50.  
  51. // Questa variabile serve solo qui, quindi è dichiarata all'interno dell'if
  52. float voltage = sensorValue * (5.058 / 1023.0);
  53. // print out the value you read:
  54. TOTALE = voltage / (0.329); //0,329674 è IL VALORE alfa =(r2)/(r1+r2) e pure Vout = alfa * Vin
  55. PERCENTUALE = (100 * TOTALE) / (12.27);
  56. }
  57.  
  58. // Riattivo il display alla pressione del pulsante
  59. if (digitalRead(8) == LOW) {
  60. delay(100); // debounce del pulsante
  61. TimerOff = millis();
  62. delay(200); // aspettiamo un po' per dare tempo al display di fare correttamente il resume
  63. }
  64.  
  65. // visualizziamo il testo sul display per 5 secondi prima di mandarlo in poweroff
  66. if ((millis() - TimerOff < TIME_OFF)) {
  67.  
  68. // Questo non serve più cosi lungo, ma per evitare di fare un refresh continuo del display
  69. // lasciamo comunque un piccolo delay all'interno di questo if.
  70. delay(100);
  71.  
  72. // Accensione LED di stato
  73. digitalWrite(13, HIGH);
  74.  
  75. //Clear display buffer from last refresh
  76.  
  77. tft.initR(INITR_BLACKTAB); // ST7735-Chip initialisieren
  78.  
  79. tft.fillScreen(ST7735_BLACK);
  80. tft.setRotation(1);
  81. tft.setTextColor(ST7735_BLUE);
  82.  
  83. tft.setCursor(0, 0);
  84. tft.setTextSize(1);
  85. tft.println("V BATT:");
  86. tft.setCursor(0, 16);
  87. tft.setTextSize(2);
  88. tft.print(TOTALE);
  89. tft.println(" V");
  90.  
  91. //PERCENTUALE DI BATTERIA RISPETTO ALLA BATTERIA CARICA A 12.27V
  92. tft.setCursor(0, 38);
  93. tft.setTextSize(1);
  94. tft.println("CARICA BATT:");
  95. tft.setCursor(0, 50);
  96. tft.setTextSize(2);
  97. tft.print(PERCENTUALE); tft.println(" %");
  98.  
  99.  
  100. ////////////////////////////////////////////////////////
  101.  
  102. // Questa variabile serve solo qui, quindi è dichiarata all'interno dell'if
  103. byte stepVoltage = map(PERCENTUALE, 0, 100, 0, 11);
  104.  
  105. // drawRect/fillRect(X, Y, Larghezza, Altezza, WHITE);
  106. tft.fillRect(112, 2, 6, 3);
  107. for (byte i = 0; i < 10; i++) {
  108. byte newY = i * 6 + 5;
  109.  
  110. if ((stepVoltage >= 10 - i)) {
  111. // Visualizzo un rettangolo bianco "fill"
  112. tft.fillRect(103, newY, 24, 5, WHITE);
  113. }
  114. else {
  115. // Visualizzo un rettangolo bianco normale
  116. tft.drawRect(103, newY, 24, 5, WHITE);
  117. }
  118. }
  119.  
  120. //////////////////////////////////////////////////////////
  121.  
  122.  
  123.  
  124. }
  125. else {
  126. // i 10 secondi sono passati, spegniamo il display
  127. tft.clearDisplay();
  128. delay(100);
  129. }
  130. Serial.println(analogRead(A1));
  131. delay(2000); //aspetto(100 millisecondi);
  132. if (analogRead(A1) > 700) {
  133. digitalWrite(Rele, LOW); // OSSIA TOTALE STA AL DI SOPRA DEL VALORE 700 il PIN 10 ossia "Rele" è in stato LOW e quindi chiuso
  134. digitalWrite(POWERBANK, LOW); // OSSIA TOTALE STA AL DI SOPRA DEL VALORE 700 il PIN 6 ossia "POWERBANK" è in stato LOW e quindi chiuso
  135. }
  136. if (analogRead(A1) < 650) {
  137. digitalWrite(Rele, HIGH); // OSSIA TOTALE STA AL DI SOTTO DEL VALORE 600 il PIN 10 ossia "Rele" è in stato LOW
  138. // e quindi apre il circuito evitando la continua scarica della batteria
  139. digitalWrite(POWERBANK, HIGH); // OSSIA TOTALE STA AL DI SOTTO DEL VALORE 600 il PIN 6 ossia "POWERBANK" è in stato HIGH
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment