Hey guys!
I know the problem is straightforward and seems simple but it hasn't been that way for me. As I have searched google, so many times and also checked this platform but unfortunately haven't found one.
Please, do assist me, kind sirs and mas.
The problem I have is that I have a LDR that returns a digital value "1" when there is darkness and a "0" when there is light.
I have connected a buzzer to indicate light and a LED to indicate darkness. Now, I want the buzzer to come on for 10 seconds when there is light and then go off (even when there is still light), so as to conserve battery. Then when there is darkness, the LED simply stays on. And when there is light again, the buzzer repeats the same sequence.
I saw a code on this forum where a pushbutton was used to control an LED for 5 seconds. There, the goal was for the led to come on for 5 seconds and then go off with the buttonpress. What I want, is for the LED to go off after 5 seconds also even if the button is permanently pressed (something like that).
Please, do assist me.
Thanks as I eagerly await a response
Attached here is the code I saw online for the pushbutton and LED:
int pin = 2;
int led = 13;
void setup()
{
pinMode(pin, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
long offAt = 0;
void loop()
{
if( (digitalRead(led) == LOW ) && (digitalRead(pin) == LOW) ) //if LED is off and button is pressed [low because it has pullup resistor]
{
digitalWrite(led, HIGH);
offAt = millis() + 5000; //store var of now + 5 seconds
}
if(digitalRead(led) == HIGH) //if led is on
{
if(millis() >= offAt) //see if it's time to turn off LED
{
digitalWrite(led, LOW); //it's time. this also re-enables the button
}
}
}
sketch_mar22a.ino (864 Bytes)