//************************************************
// Da implementare per risparmio energetico:
// 1. Spegnere periferiche:
// - power_adc_disable();
// - power_spi_disable();
// - power_twi_disable();
// - power_timer0_disable();
//
// 2. Sleep mode arudino.
// 3. Spegnere backlight
//
//
//************************************************
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include "img.h"
// Button
#define BTN 4
volatile bool wake_flag = false;
unsigned long awake_time = 0;
const unsigned long awake_duration = 2000; // per quanto resta acceso
bool prev_btn_flag = LOW;
// TFT
#define TFT_CS 10
#define TFT_DC 3
#define TFT_RST 8
#define TFT_BL 7
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
// DHT
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Sensori suolo
#define SOIL_PIN_1 A0
#define SOIL_PIN_2 A1
#define SOIL_PIN_3 A2
void setup() {
Serial.begin(9600);
pinMode(BTN, INPUT);
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, LOW);
dht.begin();
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
for (int i = 0; i < sizeof(plant); i++) {
inverted[i] = ~pgm_read_byte(&plant[i]);
}
}
void loop() {
// Lettura DHT
float H = dht.readHumidity();
float T = dht.readTemperature();
// Lettura suoli e conversione !!! DA TARARE !!!
int soilRaw_1 = analogRead(SOIL_PIN_1);
int soil_percent_1 = map(soilRaw_1, 1023, 300, 0, 100);
soil_percent_1 = constrain(soil_percent_1, 0, 100);
int soilRaw_2 = analogRead(SOIL_PIN_2);
int soil_percent_2 = map(soilRaw_2, 1023, 300, 0, 100);
soil_percent_2 = constrain(soil_percent_2, 0, 100);
int soilRaw_3 = analogRead(SOIL_PIN_3);
int soil_percent_3 = map(soilRaw_3, 1023, 300, 0, 100);
soil_percent_3 = constrain(soil_percent_3, 0, 100);
if (wake_flag) {
// Display
tft.fillScreen(ILI9341_BLACK);
// Print Temperatura Serra
tft.setCursor(T_x, T_y);
tft.print(T);
tft.print("C");
// Print Umidità Serra
tft.setCursor(H_x, H_y);
tft.print(H);
tft.print("%");
// *** Draw plants and humidities *******************
// Plant 1
tft.setCursor(plant_x0 + 10, plant_hum_y);
tft.print(soil_percent_1);
tft.print("%");
if (soil_percent_1 >= 50) tft.drawBitmap(plant_x0, plant_y, inverted, plant_w, plant_h, ILI9341_GREEN);
else if (soil_percent_1 > 25 && soil_percent_1 < 50) tft.drawBitmap(plant_x0, plant_y, inverted, plant_w, plant_h, ILI9341_YELLOW);
else tft.drawBitmap(plant_x0, plant_y, inverted, plant_w, plant_h, ILI9341_RED);
// Plant 2
tft.setCursor(plant_x1 + 10, plant_hum_y);
tft.print(soil_percent_2);
tft.print("%");
if (soil_percent_2 >= 50) tft.drawBitmap(plant_x1, plant_y, inverted, plant_w, plant_h, ILI9341_GREEN);
else if (soil_percent_2 > 25 && soil_percent_2 < 50) tft.drawBitmap(plant_x1, plant_y, inverted, plant_w, plant_h, ILI9341_YELLOW);
else tft.drawBitmap(plant_x1, plant_y, inverted, plant_w, plant_h, ILI9341_RED);
// Plant 3
tft.setCursor(plant_x2 + 10, plant_hum_y);
tft.print(soil_percent_3);
tft.print("%");
if (soil_percent_3 >= 50) tft.drawBitmap(plant_x2, plant_y, inverted, plant_w, plant_h, ILI9341_GREEN);
else if (soil_percent_3 > 25 && soil_percent_2 < 50) tft.drawBitmap(plant_x2, plant_y, inverted, plant_w, plant_h, ILI9341_YELLOW);
else tft.drawBitmap(plant_x2, plant_y, inverted, plant_w, plant_h, ILI9341_RED);
}
//**********************************************
// *** Power save mode **************************************
// Per il momento mantiene il pin BL alto per 10 secondi e poi lo spegne.
bool btn_flag = digitalRead(BTN);
if (btn_flag == LOW && prev_btn_flag == HIGH) {
digitalWrite(TFT_BL, HIGH);
awake_time = millis();
wake_flag = true;
}
prev_btn_flag = btn_flag;
if (wake_flag && (millis() - awake_time >= awake_duration)) {
digitalWrite(TFT_BL, LOW);
wake_flag = false;
}
//************************************************************
Serial.println(digitalRead(TFT_BL));
// delay(2000);
}