#include <Stepper.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define switchPin 2
#define ldrPin A2
#define tempPin 7
#define relayPin 4
#define ledPin 5
#define servoPin 6
const int stepsPerRevolution = 200;
const int dirPin = 9;
const int stepPin = 3;
const int enablePin = 8;
const int potPin = A0;
int currentStep = 0;
const int deadZone = 100; // Increase the dead zone to prevent continuous movement
OneWire oneWire(tempPin);
DallasTemperature sensors(&oneWire);
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the address to 0x27 (or your specific I2C address)
const int threshold = 500; // Adjust this value based on your LDR readings
const int hysteresis = 50; // Adjust this value to prevent rapid toggling
bool isLightOn = false;
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
sensors.begin();
lcd.begin(16, 2);
lcd.backlight();
}
void loop() {
int switchState = digitalRead(switchPin);
int ldrValue = analogRead(ldrPin);
sensors.requestTemperatures();
float tempValue = sensors.getTempCByIndex(0);
if (switchState == HIGH) {
if (ldrValue < threshold && !isLightOn) {
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Lights: ON ");
isLightOn = true;
} else if (ldrValue >= threshold && isLightOn) {
digitalWrite(ledPin, LOW);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Lights: OFF ");
isLightOn = false;
}
}
if (tempValue > 27) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
int potValue = analogRead(potPin);
int targetStep = map(potValue, 0, 1023, -1000, 1000);
int stepsToMove = targetStep - currentStep;
Serial.print("Target Step: ");
Serial.println(targetStep);
Serial.print("Current Step: ");
Serial.println(currentStep);
Serial.print("Steps to Move: ");
Serial.println(stepsToMove);
if (abs(stepsToMove) > deadZone) {
if (stepsToMove > 0) {
digitalWrite(dirPin, HIGH);
} else {
digitalWrite(dirPin, LOW);
stepsToMove = -stepsToMove;
}
for (int i = 0; i < stepsToMove; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
currentStep = targetStep;
}
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempValue);
lcd.print(" C");
Serial.print("Temp: ");
Serial.print(tempValue);
Serial.print("°C, LDR: ");
Serial.print(ldrValue);
if (currentStep < -750) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Curtain Down");
} else if (currentStep > 800) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Curtain Up");
}
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Control room
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|