#include <Servo.h>
// Pin configuration
int servoPin = 9;
const int button1_pin = 4;
const int button2_pin = 3;
// Constants for the photoresistor
const float GAMMA = 0.7;
const float RL10 = 50;
// Servo and state variables
Servo myservo;
int button1_value; //0 for low value, 1 for high value
int button2_value; //0 for low value, 1 for high value
int state = 0;
int prevstate = 0;
int newState;
int dirc = 0; //continuous servo direction
int time = 0; //time to spin that direction
void setup() {
Serial.begin(9600);
Serial.println("Start Up");
myservo.attach(servoPin);
pinMode(button1_pin, INPUT);
pinMode(button2_pin, INPUT);
}
void loop() {
button1_value = digitalRead(button1_pin);
button2_value = digitalRead(button2_pin);
// Convert analog value to lux
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));
// Define modes based on brightness and button presses
if (button1_value == HIGH) {
dirc = 180; // direction for servo to run
time = 3600; //time for servo to run in, change this variable if you need your servo to rotate longer or shorter
newState = 1;
Serial.println("Button 1 was pushed. Blind up.");
} else if (button2_value == HIGH) {
dirc = 0; // direction for servo to run
time = 3000; //time for servo to run in, change this variable if you need your servo to rotate longer or shorter
Serial.println("Button 2 was pushed. Blind down.");
newState = 2;
} else if (lux > 500 && state != 2) {
dirc = 180; // direction for servo to run
time = 3600; //time for servo to run in, , change this variable if you need your servo to rotate longer or shorter
Serial.println("Auto DAY routine. Blind up.");
newState = 3;
} else if (lux <= 500 && state != 1) {
dirc = 0; // direction for servo to run
time = 3000; //time for servo to run in, , change this variable if you need your servo to rotate longer or shorter
Serial.println("Auto NIGHT routine. Blind down.");
newState = 4;
}
// Map state and prevstate to their corresponding meanings
String stateString, prevstateString;
switch(state) {
case 1:
stateString = "Opened manually";
break;
case 2:
stateString = "Closed manually";
break;
case 3:
stateString = "Opened Automatically";
break;
case 4:
stateString = "Closed Automatically";
break;
default:
stateString = "Unknown";
break;
}
switch(prevstate) {
case 1:
prevstateString = "Opened manually";
break;
case 2:
prevstateString = "Closed manually";
break;
case 3:
prevstateString = "Opened Automatically";
break;
case 4:
prevstateString = "Closed Automatically";
break;
default:
prevstateString = "Unknown";
break;
}
// If the newState is different from the state, execute servo controls
if (newState != state) {
prevstate = state; // Update prevstate to current state
state = newState; // Update state to newState
ldr_press();
}
// Print values
Serial.print("Button1_value: ");
Serial.print(button1_value);
Serial.print("| Button2_value: ");
Serial.print(button2_value);
Serial.print("| Light Reading lux: ");
Serial.print(lux);
Serial.print(" | State: ");
Serial.print(stateString);
Serial.print("| Prevstate: ");
Serial.println(prevstateString);
// Delay for 3 seconds
delay(3000);
}
void ldr_press() {
Serial.println("State Change. Rotating Servo");
myservo.attach(servoPin);
myservo.write(dirc);
delay(time);
myservo.detach();
//delay(5000); // Change this to 900000 if you want 15 minutes between cycles
}