const int LDR_PIN = A0;
const int BUTTON_PIN = 2;
volatile bool displayLDR = false;
void buttonISR() {
Serial.println("Button Pressed!"); // Debugging message
displayLDR = !displayLDR;
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT); // Use INPUT (because external pull-down resistor is used)
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISR, RISING); // Detect press
Serial.println("System Initialized. Press button to start/stop readings.");
}
void loop() {
if (displayLDR) {
int lightLevel = analogRead(LDR_PIN);
if (lightLevel < 0 || lightLevel > 1023) {
Serial.println("Reading Error");
} else {
Serial.print("Light Level: ");
Serial.println(lightLevel);
}
delay(500);
}
}