Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_ST7735.h> // Hardware-specific library
- #include <SPI.h>
- #define TFT_CS 10
- #define TFT_RST 9 // you can also connect this to the Arduino reset
- // in which case, set this #define pin to 0!
- #define TFT_DC 8
- // Option 1 (recommended): must use the hardware SPI pins
- // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
- // an output. This is much faster - also required if you want
- // to use the microSD card (see the image drawing example)
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- // Option 2: use any pins but a little slower!
- #define TFT_SCLK 13 // set these to be whatever pins you like!
- #define TFT_MOSI 11 // set these to be whatever pins you like!
- //Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
- unsigned long TimerAnalog, TimerOff = 0;
- #define TIME_ANALOG 3000
- #define TIME_OFF 10000
- int sensorValue = 0;
- float PERCENTUALE, TOTALE = 0.0;
- int Rele = 4; //INSERISCO LA VARIABILE RELE' AL PIN 10
- int POWERBANK = 6; // INSERISCO LA VARIABILE POWERBANK AL PIN 6
- void setup(void) {
- Serial.begin(9600);
- Serial.print("Hello! ST7735 TFT Test");
- // Use this initializer if you're using a 1.8" TFT
- tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
- pinMode(6, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, INPUT_PULLUP);
- }
- void loop() {
- // Eseguiamo la lettura di A1 ogni TIME_ANALOG secondi
- if ((millis() - TimerAnalog > TIME_ANALOG)) {
- TimerAnalog = millis();
- sensorValue = analogRead(A1);
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
- // Questa variabile serve solo qui, quindi è dichiarata all'interno dell'if
- float voltage = sensorValue * (5.058 / 1023.0);
- // print out the value you read:
- TOTALE = voltage / (0.329); //0,329674 è IL VALORE alfa =(r2)/(r1+r2) e pure Vout = alfa * Vin
- PERCENTUALE = (100 * TOTALE) / (12.27);
- }
- // Riattivo il display alla pressione del pulsante
- if (digitalRead(8) == LOW) {
- delay(100); // debounce del pulsante
- TimerOff = millis();
- delay(200); // aspettiamo un po' per dare tempo al display di fare correttamente il resume
- }
- // visualizziamo il testo sul display per 5 secondi prima di mandarlo in poweroff
- if ((millis() - TimerOff < TIME_OFF)) {
- // Questo non serve più cosi lungo, ma per evitare di fare un refresh continuo del display
- // lasciamo comunque un piccolo delay all'interno di questo if.
- delay(100);
- // Accensione LED di stato
- digitalWrite(13, HIGH);
- //Clear display buffer from last refresh
- tft.initR(INITR_BLACKTAB); // ST7735-Chip initialisieren
- tft.fillScreen(ST7735_BLACK);
- tft.setRotation(1);
- tft.setTextColor(ST7735_BLUE);
- tft.setCursor(0, 0);
- tft.setTextSize(1);
- tft.println("V BATT:");
- tft.setCursor(0, 16);
- tft.setTextSize(2);
- tft.print(TOTALE);
- tft.println(" V");
- //PERCENTUALE DI BATTERIA RISPETTO ALLA BATTERIA CARICA A 12.27V
- tft.setCursor(0, 38);
- tft.setTextSize(1);
- tft.println("CARICA BATT:");
- tft.setCursor(0, 50);
- tft.setTextSize(2);
- tft.print(PERCENTUALE); tft.println(" %");
- ////////////////////////////////////////////////////////
- // Questa variabile serve solo qui, quindi è dichiarata all'interno dell'if
- byte stepVoltage = map(PERCENTUALE, 0, 100, 0, 11);
- // drawRect/fillRect(X, Y, Larghezza, Altezza, WHITE);
- tft.fillRect(112, 2, 6, 3);
- for (byte i = 0; i < 10; i++) {
- byte newY = i * 6 + 5;
- if ((stepVoltage >= 10 - i)) {
- // Visualizzo un rettangolo bianco "fill"
- tft.fillRect(103, newY, 24, 5, WHITE);
- }
- else {
- // Visualizzo un rettangolo bianco normale
- tft.drawRect(103, newY, 24, 5, WHITE);
- }
- }
- //////////////////////////////////////////////////////////
- }
- else {
- // i 10 secondi sono passati, spegniamo il display
- tft.clearDisplay();
- delay(100);
- }
- Serial.println(analogRead(A1));
- delay(2000); //aspetto(100 millisecondi);
- if (analogRead(A1) > 700) {
- digitalWrite(Rele, LOW); // OSSIA TOTALE STA AL DI SOPRA DEL VALORE 700 il PIN 10 ossia "Rele" è in stato LOW e quindi chiuso
- digitalWrite(POWERBANK, LOW); // OSSIA TOTALE STA AL DI SOPRA DEL VALORE 700 il PIN 6 ossia "POWERBANK" è in stato LOW e quindi chiuso
- }
- if (analogRead(A1) < 650) {
- digitalWrite(Rele, HIGH); // OSSIA TOTALE STA AL DI SOTTO DEL VALORE 600 il PIN 10 ossia "Rele" è in stato LOW
- // e quindi apre il circuito evitando la continua scarica della batteria
- digitalWrite(POWERBANK, HIGH); // OSSIA TOTALE STA AL DI SOTTO DEL VALORE 600 il PIN 6 ossia "POWERBANK" è in stato HIGH
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment