#include <Servo.h>
const float GAMMA = 1.2;
const float RL10 = 20.0;
const int LDR_THRESHOLD_FLIPFLOP = 150; // Threshold lux value for flip-flop LED behavior
const int LDR_THRESHOLD_RUNNING_LED = 250; // Threshold lux value for running LED behavior
const int LDR_THRESHOLD_BUZZER = 200; // Threshold lux value for activating buzzer
const int BUZZER_PIN = 8; // Pin for the buzzer
const int LED_PIN_12 = 12; // Pin for LED 1
const int LED_PIN_13 = 13; // Pin for LED 2
const int LED_PIN_7 = 7; // Pin for LED 3
const int SERVO_PIN = 10; // Pin for servo
bool ledState = false;
bool servoMoved = false; // Variable to track if servo has moved
Servo myservo;
void setup() {
Serial.begin(115200);
Serial.println("LDR infinity test");
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
pinMode(LED_PIN_12, OUTPUT); // Set LED 1 pin as output
pinMode(LED_PIN_13, OUTPUT); // Set LED 2 pin as output
pinMode(LED_PIN_7, OUTPUT); // Set LED 3 pin as output
myservo.attach(SERVO_PIN); // Attach servo to pin 10
}
void loop() {
// Convert the analog value into lux value:
int analogValue = analogRead(A0);
float voltage = float(analogValue) / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10.0, GAMMA) / resistance, (1.0 / GAMMA));
Serial.print("ANALOG: ");
Serial.print(analogValue);
Serial.print("\tVoltage: ");
Serial.print(voltage);
Serial.print("\tResistance: ");
Serial.print(resistance);
Serial.print("\tLUX: ");
Serial.println(lux);
// Check if lux value exceeds the flip-flop LED threshold
if (lux >= LDR_THRESHOLD_FLIPFLOP) {
// Toggle the state of LED 1 and LED 2 (flip flop)
ledState = !ledState;
digitalWrite(LED_PIN_12, ledState);
digitalWrite(LED_PIN_13, !ledState);
} else {
// Turn off LED 1 and LED 2 if lux value is below the flip-flop LED threshold
digitalWrite(LED_PIN_12, LOW);
digitalWrite(LED_PIN_13, LOW);
}
// Check if lux value exceeds the running LED threshold
if (lux >= LDR_THRESHOLD_RUNNING_LED) {
// Run LED as running light
runningLED();
digitalWrite(LED_PIN_7, HIGH); // Turn on LED on pin 7
// Check if lux value exceeds the servo threshold and servo has not moved yet
if (lux >= LDR_THRESHOLD_BUZZER && !servoMoved) {
// Move the servo to a specific position
myservo.write(90); // Example position (90 degrees)
servoMoved = true; // Set servo moved flag to true
}
} else {
// Turn off LED 7 if lux value is below the running LED threshold
digitalWrite(LED_PIN_7, LOW);
servoMoved = false; // Reset servo moved flag when lux value drops below threshold
}
// Check if lux value exceeds the buzzer threshold
if (lux >= LDR_THRESHOLD_BUZZER) {
// Turn on the buzzer for 2 seconds
tone(BUZZER_PIN, 500);
delay(2000);
noTone(BUZZER_PIN);
}
delay(750);
}
void runningLED() {
static int ledPosition = LED_PIN_12; // Start position of the running LED
digitalWrite(ledPosition, HIGH); // Turn on current LED
delay(100); // Delay for LED visibility
// Turn off current LED and move to the next one
digitalWrite(ledPosition, LOW);
ledPosition++;
// If we've reached LED 7, reset to LED 12
if (ledPosition > LED_PIN_7) {
ledPosition = LED_PIN_12;
}
}