View previous topic :: View next topic |
Author |
Message |
Bibi Guest
|
sensor ... |
Posted: Fri Jul 01, 2005 3:00 am |
|
|
Hello,
i have a sensor that is 1 when (!input(sensor)) and 0 when (input(sensor)).
What i want to do is to count if it is pressed 6 times in an interrupt.
It should be something pretty much like this :
Code: | counter = 0;
button_pressed_6_times = 0;
if (!input(sensor)){ // Wait for Enter Button to be pressed
while (!input(sensor)); // Wait for Enter Button to be released
counter +=1; // counts how many times it is pressed
}
if(counter == 5) button_pressed_6_times == 1; |
Will that work or does someone has an idea ?
The thing that is difficult is that it should be in an interrupt ...
Thank you very much. |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
Re: sensor ... |
Posted: Fri Jul 01, 2005 5:13 am |
|
|
Bibi wrote: | Hello,
i have a sensor that is 1 when (!input(sensor)) and 0 when (input(sensor)).
What i want to do is to count if it is pressed 6 times in an interrupt.
[/code]
Wait. You want to stay this long in an interrrupt service routine? Don't do that. Your interrupt should get in and out within microseconds. Defer this counting six times for your main loop.
Code: | counter = 0;
button_pressed_6_times = 0;
if (!input(sensor)){ // Wait for Enter Button to be pressed
while (!input(sensor)); // Wait for Enter Button to be released
counter +=1; // counts how many times it is pressed
}
if(counter == 5) button_pressed_6_times == 1; |
This should be:
if (counter == 5)
{
button_pressed_6_times = 1;
}
Will that work or does someone has an idea ?
The thing that is difficult is that it should be in an interrupt ...
Thank you very much. |
There is something clumsy about staying long in an interrupt service routine. But if you do insist on staying in the ISR, the above code, with the "==" correction should work. Make sure those variables are Global though; i don't see how or where they are initialized. |
|
![](templates/subSilver/images/spacer.gif) |
Bibi Guest
|
|
Posted: Fri Jul 01, 2005 5:25 am |
|
|
If it is not in an isr how could i do ?
The variables are global exact ! |
|
![](templates/subSilver/images/spacer.gif) |
Bibi Guest
|
|
Posted: Fri Jul 01, 2005 6:30 am |
|
|
I forgot to say that it should be pressed 6 times for a period of 30 seconds.
So if the button is pressed 6 times during 30 seconds button_pressed_6_times == 1; |
|
![](templates/subSilver/images/spacer.gif) |
sseidman
Joined: 14 Mar 2005 Posts: 159
|
|
Posted: Fri Jul 01, 2005 7:11 am |
|
|
Bibi wrote: | I forgot to say that it should be pressed 6 times for a period of 30 seconds.
So if the button is pressed 6 times during 30 seconds button_pressed_6_times == 1; |
That's a MUCH different problem, and you need to define it a little better. Do you mean 30 seconds from the first button press, or any 6 presses within 30 seconds? With the first, you would start a 30 second timer on the first button press, and reset things if the timer reaches 30 seconds and six presses have not occured. The second cas is much more complex.
Be careful about button bounce, as well.
Scott |
|
![](templates/subSilver/images/spacer.gif) |
Bibi Guest
|
|
Posted: Fri Jul 01, 2005 7:18 am |
|
|
It should be 6 presses in 30 seconds, the first press starts the process and then ther are 30 secondes left for the button to be pressed 5 more times to the process to be ok if not nothing happens. |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Fri Jul 01, 2005 8:48 am |
|
|
Don't forget to do some debouncing of the switch _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
![](templates/subSilver/images/spacer.gif) |
Bibi Guest
|
|
Posted: Mon Jul 04, 2005 1:50 am |
|
|
Do have any other idea ? |
|
![](templates/subSilver/images/spacer.gif) |
Bibi Guest
|
|
Posted: Mon Jul 04, 2005 6:09 am |
|
|
PLEASE HELP !!!! ![Crying or Very sad](images/smiles/icon_cry.gif) |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
Posted: Mon Jul 04, 2005 7:10 am |
|
|
Do you want code?
ex_stwt1.c
or
ex_stwt.c
or
ex_stwt2.c
in the CCS examples directory that comes with every version of the compiler
Read it and you'll find you can set up your isr. Then look the main. It counts the timing between two events (you press a key two consecutive times and it tells you how many seconds elapsed between the two keypresses).
With a few changes, you can have this example work for you. |
|
![](templates/subSilver/images/spacer.gif) |
|