Controlling a buzzer with a photosensitive sensor module (LDR)

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)

What have you tried and what were the results?

Paul

If you connect your sensor to pin 2 in place of the button what does the code do?

What sort of "buzzer" do you have. There are active and passive buzzers and they're driven completely differently. Have you made any attempt to make whatever it is buzz? If so post that here.

Steve

@slipstick.
Thanks for the reply

If i connect my sensor to pin 2, the buzzer stays on permanently when light is detected, instead of it to stay on for 5 seconds and then go back off when there is still light.

You know, when you press the pushbutton, you get a digital value of '0'. So, when this value is detected, the led stays on for 5 seconds before going back off, since the value would have gone back to its default state (which is a '1').

All I want, is that even if the button still stays pressed, the led should come on for just that 5 seconds

PS: I am avoiding the delay function as I am also running other things simultatenously

The actual implementation is with a buzzer, but because of the environment I am, I use an LED to test first. So, if the LED works as I want, then I will switch back to the buzzer.

@paul_KD7HB
Thanks for the reply.

What I have tried is what i have just posted as a reply to @slipstick

@slipstick: I'm not sure which buzzer is active or passive, so I have attached a picture of the buzzer.

Thanks

Halil1_chyker:
If i connect my sensor to pin 2, the buzzer stays on permanently when light is detected, instead of it to stay on for 5 seconds and then go back off when there is still light.on

You're not making things easy. Your sketch has no mention of a buzzer in it and all the comments talk about buttons and LED. Are you saying that you have tested it with the buzzer connected to to the pin that you call 'led' and the sensor connected to the pin you call 'pin'?

Then you say you have tested it with a sensor but talk about what happens when some pushbutton is pressed. Which is it? I'm confused.

Anyway you need to remember when the sensor FIRST went low and count the 5 seconds from there. Have a look at the StateChangeDetection example in the IDE for a useful technique for doing that.

Steve