// https://wokwi.com/projects/353032053211476993
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's pin
const int LED_PIN = 6; // Arduino pin connected to LED's pin
const int ANALOG_THRESHOLD = 777;
// const int BUZZER_PIN = 4;
// int startMode = 1; // set to 1 by default
void setup() {
Serial.begin(9600);
Serial.println("Hello Beep World!\n");
pinMode(LED_PIN, OUTPUT);
}
bool needABeep; // state change on analog "button" sets this
byte buttonState; // sorry, borrowed from state change detection example!
byte lastButtonState;
unsigned long startTime;
const unsigned long desiredinterval = 1222;
void loop() {
buttonState = analogRead(LIGHT_SENSOR_PIN) > ANALOG_THRESHOLD;
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
needABeep = true;
}
}
lastButtonState = buttonState;
// start a beep?
if (needABeep) {
digitalWrite (LED_PIN, HIGH);
startTime = millis();
needABeep = false; // we started the beep, OK?
}
// turn off beep?
if (millis() - startTime > desiredinterval) {
digitalWrite(LED_PIN, LOW);
}
delay(50); // global poor man's debouncing
}