View previous topic :: View next topic |
Author |
Message |
A_L_E_X
Joined: 21 Feb 2010 Posts: 11
|
Timers to keep lights on |
Posted: Fri May 17, 2013 5:47 am |
|
|
I have a system with two light bulbs and two buttons. When a button is pressed the corresponding light bulb should be on. But after the button is released the light must stay on for a specific time. I can make the system for only one light but for two lights i need to use timers. Does anyone have an example of how to use timers to just light a led for a specific time. I've read the example from the site but i don't understood how timers are working. Is there other way to do what i what or timers is the only solution. It doesn't matter what PIC i use. I was thinking at a pic12f615 because it have less pins. The thing i want to do can be done with two 555 or with just a 556 but i want to do it with a microcontroller. The main problem is how to separate the processes. I haven't used timers since now so a basic hello world application that blink a led using a timer would help me. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri May 17, 2013 6:44 am |
|
|
Probably easiest and simplest to use interrupts for both buttons and timers.
You could use small delay_ms(xx)'s multiple times, for the timings.
Create counters to set the times.
Poll the buttons.
Mike |
|
|
A_L_E_X
Joined: 21 Feb 2010 Posts: 11
|
|
Posted: Fri May 17, 2013 7:31 am |
|
|
The maximum time is 12 seconds. Do you know any example similar to my case ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Fri May 17, 2013 8:38 am |
|
|
All you need is to have a 'tick' interrupt at some frequency that suits, then whenever the button is seen as 'on', load a counter with the number of counts of this interrupt you want it to stay on for. So (pseudo code):
Code: |
int16 count1=0,count2=0;
#define TicksToStayOn (NoOfSecondsYouWant*InterruptsPerSecond)
............
//In your main loop
if (button1IsOn)
{
count1=TicksToStayOn;
}
if (button2IsOn)
{
count2=TicksToStayOn;
}
If (count1>0)
output1_on;
else
output1_off;
If (count2>0)
output2_on;
else
output2_off;
.............
void your_tick_interrupt(void)
{
if (count1>0)
--count1;
if (count2>0)
--count2;
}
|
So whenever the button is seen as 'pressed', the counter is loaded (and reloaded so long as the button is pressed). When the button is released the counter starts counting down at the interrupt rate. When it gets to zero, the output is turned off.
Best Wishes |
|
|
|