#include <avr/dtostrf.h>
#include <DHT.h>
#define DHTPIN 11 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_CS 17
#define TFT_DC 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// global variabelen
float h,t,oh,ot;
void setup() {
// put your setup code here, to run once:
tft.begin();
dht.begin();
h = dht.readHumidity();
t = dht.readTemperature();
// nieuwe tekst schrijven
tft_txt(h,t,ILI9341_RED,ILI9341_GREEN);
// waarden opslaan
oh = h;
ot = t;
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
// vorige tekst overschrijven in zwarte kleur
tft_txt(oh,ot,ILI9341_BLACK,ILI9341_BLACK);
// nieuwe tekst schrijven
tft_txt(h,t,ILI9341_RED,ILI9341_GREEN);
// waarden opslaan
oh = h;
ot = t;
delay(1000);
}
void tft_txt(float h, float t, unsigned int ch, unsigned int ct) {
char buffer[10];
tft.setCursor(0,0);
tft.setTextColor(ch);
tft.setTextSize(2);
tft.print("Humidity: ");
dtostrf(h, 6, 2, buffer);
tft.print(buffer);
tft.setCursor(0,40);
tft.setTextColor(ct);
tft.setTextSize(2);
tft.print("Temperature: ");
tft.print(t);
}