/* Hello Wokwi! */
#include <LiquidCrystal_I2C.h>
#include <DHT.h>;
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
float hum; //Stores humidity value
float temp; //Stores temperature value
int Hum; //Stores humidity value
int Temp; //Stores temperature value
int MotorPosition;
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
Servo servo3; // create servo object to control a servo
Servo servo4; // create servo object to control a servo
int servo1pos;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("1.");
lcd.setCursor(9, 0);
lcd.print("2.");
lcd.setCursor(0, 1);
lcd.print("3.");
lcd.setCursor(9, 1);
lcd.print("4.");
dht.begin();
Serial.begin(9600);
servo1.attach(2); // attaches the servo1 on pin 9 to the servo object
servo2.attach(10); // attaches the servo2 on pin 9 to the servo object
servo3.attach(11); // attaches the servo3 on pin 9 to the servo object
servo4.attach(12); // attaches the servo4 on pin 9 to the servo object
}
void loop() {
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if (lux >= 50){
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();
Hum = hum;
Temp = temp;
switch (Temp) {
case 0 ... 24:
MotorPosition = 100;
servo1pos = map(MotorPosition, 0, 100, 0, 180); // scale it to use it with the servo (value between 0 and 180)
servo1.write(servo1pos); // sets the servo position according to the scaled value
Serial.println(servo1pos);
delay(15);
break;
case 25 ... 35:
MotorPosition = 60;
servo1pos = map(MotorPosition, 0, 100, 0, 180); // scale it to use it with the servo (value between 0 and 180)
servo1.write(servo1pos); // sets the servo position according to the scaled value
Serial.println(servo1pos);
delay(15);
break;
case 36 ... 40:
MotorPosition = 40;
servo1pos = map(MotorPosition, 0, 100, 0, 180); // scale it to use it with the servo (value between 0 and 180)
servo1.write(servo1pos); // sets the servo position according to the scaled value
Serial.println(servo1pos);
delay(15);
break;
case 41 ... 100:
MotorPosition = 0;
servo1pos = map(MotorPosition, 0, 100, 0, 180); // scale it to use it with the servo (value between 0 and 180)
servo1.write(servo1pos); // sets the servo position according to the scaled value
Serial.println(servo1pos);
delay(15);
break;
}
}
else {
}
lcd.setCursor(3, 0);
lcd.print(String(MotorPosition) + String("%"));
lcd.setCursor(12, 0);
lcd.print(String(MotorPosition) + String("%"));
lcd.setCursor(3, 1);
lcd.print(String(MotorPosition) + String("%"));
lcd.setCursor(12, 1);
lcd.print(String(MotorPosition) + String("%"));
delay(10);
}