// LightController class definition
class LightController {
private:
int ledPin; // Pin connected to the LED
int ldrPin; // Pin connected to the LDR
int threshold; // Threshold value to control LED
public:
// Constructor to initialize the LED and LDR pins and threshold
LightController(int led, int ldr, int thresh) : ledPin(led), ldrPin(ldr), threshold(thresh) {}
// Initialize the LED pin as output
void begin() {
pinMode(ledPin, OUTPUT);
}
// Read the LDR value
int readLDR() {
return analogRead(ldrPin);
}
// Control LED based on the LDR reading
void controlLED() {
int value = readLDR();
Serial.print("LDR Value: ");
Serial.println(value);
if (value < threshold) {
digitalWrite(ledPin, HIGH); // Turn LED on if below threshold
} else {
digitalWrite(ledPin, LOW); // Turn LED off if above threshold
}
}
};
// Create an instance of the LightController class with LED on pin 5, LDR on pin 27, and a threshold of 200
LightController lightControl(5, 27, 200);
void setup() {
Serial.begin(9600);
lightControl.begin(); // Initialize the LED pin
}
void loop() {
lightControl.controlLED(); // Control LED based on light intensity
delay(500); // Add a delay for stability
}