#include <Servo.h>
#include <LiquidCrystal.h>
#include <Stepper.h>
#include <TM1637Display.h> // Include library for 7-segment display (for common TM1637 display)
#include <DHT.H> // Library for DHT11 or DHT22 sensor
#define DHTPIN A0 // Pin where the DHT sensor is connected
#define DHTTYPE DHT22 // Define the type of DHT sensor
LiquidCrystal lcd(7, 6, 5, 2, A4, A5);
const int StepsPerRevolution = 200;
Steeper myStepper(StepsPerRevolution,8, 9,10, 11);
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
Servo myServo; // Create a Servo object
int pos = 0;
#define CLK 3 // Define 7-segment display pins (CLK for TM1637)
#define DIO // Define 7-segment display pins (DIO for TM1637)
TM1637Display display(CLK, DIO); // Initialize display object
void setup() {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
myServo.attach(9); // Attach servo motor to pin 9
display.setBrightness(0x0f); // Set maximum brightness for display
dht.begin(); // Start DHT sensor
Serial.begin(9600); // Start serial communication for debugging
myStepper.setSpeed(60); // Set the speed at 60rpm
}
void loop() {
float temperature =
dht.readTemperature(); // Read temperature in Celcios
float humidity = dht.readHumidity(); // Read humidity
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Temp is: ");
lcd.print(temperature);
lcd.setCursor(0, 1);
lcd.print("Humid is: ");
lcd.print(humidity);
// Check if the reading is valid
if (isnan(temperature)) {
//Serial.println("Failed to read from the DHT sensor! ");
return;
}
// Display temperature on 7-segment display
int tempToDisplay = (int)temperature; // Convert temperature to integer
display.showNumberDec(tempToDisplay); // Display temperature
// Display on LCD
// Control servo motor based on temperature
if(temperature < 20) {
myServo.write(90);// Servo at 90 degrees
digitalWrite(A2, HIGH);
digitalWrite(A3, LOW);
}
else if(temperature >= 40){
myServo.write(180); // Servo at 180 degrees
myStepper.step(StepsPerRevolution);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
}
else {
myServo.write(180); // Servo at 180 degrees
}
delay(1); // Wait 1 second before updating again
}