i was trying to make this thing that sounds a buzzer when light is detected(through a photoresistor), and for some reason, the photoresistor is staying at the same light level no matter what i do. this means the buzzer is always ringing, and it won't stop unless i take out the photoresistor. even if i cover the photoresistor, the light level stays at 1023
here is my code:
`const int photoResistorPin = A0; // Analog pin for the photoresistor
const int buzzerPin = 9; // Digital pin for the piezo buzzer
const int lightThreshold = 1000;
bool isLightDetected = false;
void setup() {
Serial.begin(9600);
// Set the buzzer pin as output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Read the value from the photoresistor
int lightValue = analogRead(photoResistorPin);
// Determine if light is detected
bool currentLightDetected = lightValue > lightThreshold;
if (currentLightDetected != isLightDetected) {
isLightDetected = currentLightDetected;
if (isLightDetected) {
Serial.println("Light detected");
tone(buzzerPin, 1000);
} else {
Serial.println("Light not detected");
noTone(buzzerPin); // Stop the buzzer
}
i was trying to make this thing that sounds a buzzer when light is detected(through a photoresistor), and for some reason, the photoresistor is staying at the same light level no matter what i do. this means the buzzer is always ringing, and it won't stop unless i take out the photoresistor. even if i cover the photoresistor, the light level stays at 1023
here is my code:
`const int photoResistorPin = A0; // Analog pin for the photoresistor const int buzzerPin = 9; // Digital pin for the piezo buzzer
const int lightThreshold = 1000;
bool isLightDetected = false;
void setup() { Serial.begin(9600);
// Set the buzzer pin as output pinMode(buzzerPin, OUTPUT); }
void loop() { // Read the value from the photoresistor int lightValue = analogRead(photoResistorPin);
// Determine if light is detected bool currentLightDetected = lightValue > lightThreshold;
if (currentLightDetected != isLightDetected) { isLightDetected = currentLightDetected;
}
delay(100); } `