#include <Servo.h>
const int ldrPin = A0; // LDR pin connected to analog pin A0
const int ledPin1 = 2; // First LED connected to digital pin 2
const int ledPin2 = 3; // Second LED connected to digital pin 3
const int servoPin = 9; // Servo motor signal pin connected to digital pin 9
const float gama = 0.7;
const float rl10 = 50;
int ldrValue; // Variable to store the LDR value
Servo servoMotor; // Create a servo object to control the servo motor
void setup() {
Serial.begin(9600);
pinMode(ldrPin, INPUT); // Set LDR pin as input
pinMode(ledPin1, OUTPUT); // Set LED pin 1 as output
pinMode(ledPin2, OUTPUT); // Set LED pin 2 as output
servoMotor.attach(servoPin); // Attach servo to servoPin
}
void loop() {
ldrValue = analogRead(ldrPin); // Read the LDR value
//ldrValue = map(ldrValue, 4095, 0, 1024, 0);
//Serial.println(ldrValue);
float voltagem = ldrValue / 1024.*5;
float resistencia = 2000 * voltagem / (1-voltagem/5);
float luz = pow(rl10*1e3*pow(10,gama)/resistencia,(1/gama));
Serial.println(luz);
// Check if the LDR intensity is low
if (luz < 500) {
digitalWrite(ledPin1, HIGH); // Turn on LED 1
digitalWrite(ledPin2, HIGH); // Turn on LED 2
servoMotor.write(0); // Set servo motor to 0 degrees
} else {
digitalWrite(ledPin1, LOW); // Turn off LED 1
digitalWrite(ledPin2, LOW); // Turn off LED 2
servoMotor.write(180); // Set servo motor to 180 degrees
}
delay(100); // Delay for stability
}