#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
// Define pin connections
#define TFT_CS 26
#define TFT_RST 25
#define TFT_DC 2
#define DHT_PIN 14
#define DHT_TYPE DHT22
// Initialize display and DHT sensor
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
DHT dht(DHT_PIN, DHT_TYPE);
bool isCrying = false;
void setup() {
Serial.begin(115200);
// Initialize TFT display
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
// Initialize DHT sensor
dht.begin();
// Draw initial smiley emoji
drawSmiley();
}
void loop() {
// Read temperature
float temperature = dht.readTemperature();
// Check if reading failed
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display temperature on the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
// Check if the temperature exceeds the threshold
if (temperature > 35.0) {
if (!isCrying) { // Avoid unnecessary redrawing
isCrying = true;
drawCrying();
}
} else {
if (isCrying) { // Avoid unnecessary redrawing
isCrying = false;
drawSmiley();
}
}
delay(2000); // Delay between readings
}
void drawSmiley() {
tft.fillScreen(ILI9341_BLACK); // Clear screen
tft.fillCircle(120, 160, 80, ILI9341_YELLOW); // Face
tft.fillCircle(90, 140, 10, ILI9341_BLACK); // Left eye
tft.fillCircle(150, 140, 10, ILI9341_BLACK); // Right eye
tft.fillRect(90, 180, 60, 10, ILI9341_BLACK); // Mouth base
tft.fillTriangle(90, 190, 120, 200, 150, 190, ILI9341_BLACK); // Smiling curve
}
void drawCrying() {
tft.fillScreen(ILI9341_BLACK); // Clear screen
tft.fillCircle(120, 160, 80, ILI9341_YELLOW); // Face
tft.fillCircle(90, 140, 10, ILI9341_BLACK); // Left eye
tft.fillCircle(150, 140, 10, ILI9341_BLACK); // Right eye
tft.fillRect(87, 150, 5, 15, ILI9341_BLUE); // Left tear
tft.fillRect(147, 150, 5, 15, ILI9341_BLUE); // Right tear
tft.fillRect(90, 190, 60, 10, ILI9341_BLACK); // Mouth base
tft.fillTriangle(90, 190, 120, 180, 150, 190, ILI9341_BLACK); // Frowning curve
}