Hello! I have Arduino for two years, but a bit forgotten because the university and other stuff.
First of all, sorry for my english
We use arduino to control the temperature of our "beer-fridge" where we have the beer while it's fermenting.
Now, in the university I started a project and I have to use Arduino, but I'm a bit rusty...
To practice, I'm making a simple alarm with a countdown.
Basically consist of 3 buttons, various leds, a buzzer, a laser...
It will be better to show you what I did with Fritzing:
I don't know so much about electronics, so I hope you understand it.
My idea is, if you cross the laser, the LDR sensor don't receive light and it will start a countdown.
The basic code (button sequence, leds... I know how to do it, or I hope XD ) But I don't know how to do the countdown.
Any idea?
All what I did in my university was in Python, so the only C I learnt is with Arduino, and it's not much :~
the LDR needs a pullup to Vcc. as you have it it is trying to put a voltage into the A0 port but with no source. Put a resister between the A0 and Vcc and then when the LDR starts to conduct it will pull the voltage DOWN !!
I assume also that you are using the internal pull ups on input pins 2,3,4 otherwise you will need pullup resisters on those ports too. Also advisable it to pus a resister in line with the switch to GND so that if the port is set to output and HIGH then pressing the button will not sink the current directly to GND with the potential to fry the port.
Another option is to use the timer interrupts to give you a given time interval.
When you service the interrupt you can increment a counter and then set a variable to indicate that the counter has gone past a set number. If you know that the ISR is running at say 10 Htz then it will be called 10 times a second . If you have a counter within the ISR then as it get to 10 you know that 1 second has elapsed. Setting several counters and flags a single ISR from the timer can be used to trigger many timed events without any blocking code.
One caveat is that the code serviced in the ISR does NOT take any longer to run that the period between interrupts unless you disable interrupts within the ISR, but doing this you will lose the precision of the timing..
Don't forget to declare any variables used in an ISR as volatile also.