View previous topic :: View next topic |
Author |
Message |
bauche
Joined: 06 Nov 2010 Posts: 22 Location: montreal qc
|
question about status of pin |
Posted: Fri Oct 12, 2012 4:34 am |
|
|
Hi I'm newbie I'm trying to do something like...if an input (ex:RA4)
is high or low.
If my input is high nothing happened,
but if my input RA4 is low more than 3 second then call a function.
If is less then 3 seconds nothing happened.
Ex of my code:
void prob (void)
{
if(input(PIN_A4))
{
delay_ms(3000);
output_high(LED2);
output_low(LED1);
zap:
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"PROBLEM");
delay_ms(5000);
goto zap;
}
Thanks for help!!! |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri Oct 12, 2012 5:31 am |
|
|
Using a delay for three seconds will not really do what you want. Consider what might happen if the input goes low for 1 seconds, then goes high for 1.5 seconds then goes low for anything between just over 0.5 seconds and just less than 3 seconds. Your code would incorrectly think it was low for the full three seconds, which is NOT what you state you want.
What you have to do is detect the pin going low, then start some sort of timer and wait for it to time out. If at any time BEFORE it times out the pin goes high then you should ignore it, and go back to waiting for it to go low. If the timer (however it is implemented) times out without the pin having gone high then you declare it a valid detection and do whatever you need to do.
The problem is that if you are polling the pin while you wait, then there will always be a finite non-zero time depending on the polling interval, during which the pin could have changed state without you knowing about it. To get round this you might be tempted to use an interrupt on change pin, but this suffers from the same problem to a limited extent as the code will be blind to changes while the ISR is running.
However as I see you are using a goto (!) I doubt any of this will matter to you and you'll just use a delay anyway.
RF Developer |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Oct 12, 2012 5:46 am |
|
|
Read the guidelines on posts.
Use the code button to preserve indentation etc.
Avoid goto like the plague.
I suggest you poll the button every say 1 to 10ms with an interrupt. That way you deal with the issues associated with switch bounce, those raised by RF_Developer, and allow your code to do something else at the same time.
Mike
EDIT. A search on this forum will yield loads of code to achieve your objective. |
|
|
|