![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
Quick timer program problem |
Posted: Fri Sep 23, 2005 8:14 am |
|
|
Hi All, i am tring to write a very simple timer program, but am having some problems. It is ment to turn on a relay when i press and hold a buttion, and the delay the turnoff but some time (2 seconds in this example). This all works ok, however, if i repress the buttion within the timout priod it will not delay when i let go again. It will just stay on as long as i hold the buttion, but will turn off as soon as i let go. If i let the relay turn off before pushing the buttion, it will work OK.
My code is below
Thank you for your help
Mark
Code: |
#include <12F629.h>
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, BROWNOUT
void main() {
OUTPUT_low(pin_A0); //make sure blower is off
while(1)
{
while (input(pin_A2)); //Wait untill buttion is pressed
{
output_low(pin_a0);
}
while (!input(pin_A2)); //turn blower on if buttion is pressed
{
output_high(pin_a0);
delay_ms(2000);
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Sep 23, 2005 8:37 am |
|
|
You put ';' after the while statements. This is what your code actually looks like:
Code: | #include <12F629.h>
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, BROWNOUT
void main() {
output_low(pin_A0); //make sure blower is off
while(1)
{
while (input(pin_A2)); //Wait untill buttion is pressed
output_low(pin_a0);
while (!input(pin_A2)); //turn blower on if buttion is pressed
output_high(pin_a0);
delay_ms(2000);
}
}
|
This is what you probably want
Code: | #include <12F629.h>
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, BROWNOUT
void main() {
while(1)
{
// Blower off
output_low(pin_a0);
// Wait until button is pressed
while (input(pin_A2));
// Blower on
output_high(pin_a0);
// 2 second delay after button release
while (delaycount < 2000)
{
delay_ms(1);
// Keep delaying until button is released
if (!input(pin_A2))
delaycount = 0
else
delaycount++;
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|