#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
#define DHT22_PIN PA0 // DHT sensor data pin connected to PA0
#define FAN_PIN PA2 // Relay control pin connected to PA2
#define UP_PIN PA3 // Up button connected to PA3 (can change as needed)
#define DOWN_PIN PA4 // Down button connected to PA4 (can change as needed)
#define MOTION_SENSOR_PIN PA1 // PIR sensor data pin connected to PA1
DHT dht22(DHT22_PIN, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, and 2 rows
float threshhold = 80.0; // 80 F as the threshold temperature
bool fanOn = false;
int UP_lastState = HIGH; // previous state of UP button
int UP_currentState;
int DOWN_lastState = HIGH; // previous state of DOWN button**Project Link:** https://wokwi.com/projects/408116783885922305?gh=1
Error message: /STMicroelectronics/tools/xpack-arm-none-eabi-gcc/13.2.1-1.1/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld: /sketch.ino.elf section `.rodata' will not fit in region `FLASH'
/STMicroelectronics/tools/xpack-arm-none-eabi-gcc/13.2.1-1.1/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld: region `FLASH' overflowed by 1576 bytes
collect2: error: ld returned 1 exit status
Error during build: exit status 1
int DOWN_currentState;
bool motionDetected = false; // State of motion detection
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the DHT22 sensor
dht22.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Set pin modes
pinMode(FAN_PIN, OUTPUT); // Fan relay control pin
pinMode(UP_PIN, INPUT_PULLUP); // UP button with internal pull-up
pinMode(DOWN_PIN, INPUT_PULLUP);// DOWN button with internal pull-up
pinMode(MOTION_SENSOR_PIN, INPUT); // PIR motion sensor pin
}
void loop() {
// Read temperature in Fahrenheit
float tempF = dht22.readTemperature(true);
// Read the state of the motion sensor
motionDetected = digitalRead(MOTION_SENSOR_PIN);
// Control the fan based on temperature and motion detection
if (tempF >= threshhold && motionDetected) {
if (!fanOn) {
fanOn = true;
digitalWrite(FAN_PIN, HIGH);
}
} else {
if (fanOn) {
fanOn = false;
digitalWrite(FAN_PIN, LOW);
}
}
// Read the state of the UP button to increase the threshold
UP_currentState = digitalRead(UP_PIN);
if (UP_currentState == LOW && UP_lastState == HIGH) {
threshhold += 1;
}
UP_lastState = UP_currentState;
// Read the state of the DOWN button to decrease the threshold
DOWN_currentState = digitalRead(DOWN_PIN);
if (DOWN_currentState == LOW && DOWN_lastState == HIGH) {
threshhold -= 1;
}
DOWN_lastState = DOWN_currentState;
// Check if reading the temperature failed
if (isnan(tempF)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
// Display current temperature and threshold on LCD
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Curr. Temp: ");
lcd.print(tempF);
lcd.print("F");
lcd.setCursor(0, 1); // move cursor to (0, 1)
lcd.print("Threshhold: ");
lcd.print(threshhold);
lcd.print("F");
}
delay(1000); // Delay for readability
}