PDA

View Full Version : Button Push within 3 second Window



Tissy
- 21st December 2005, 13:36
What is the easiest way to perform the following.

I have a routine which looks for a button push, then depending how many time the button is pushed it jumps to a routine. So for 1 button push, Routine 1, 2 Button pushes Routine 2 etc etc.

Then within each of the Routines is a confirmation LED which flashes to show how many times you pushed the button. 2 flashes = button pushed twice.

I then want a Confirmation GOSUB so that if the button is pushed say 3 times and the LED flashes 3 times, it gives you the opportunity to confirm your selection by pushing the button again.

So i would like a GOSUB which gives a 3 second window where if the button is pushed again after the flashes but at anytime during this 3 second window, the selection is confirmed and the GOSUB RETURNS, if no button is pushed within the 3 seconds window, the program starts over.

Can anyone assist with this. I'm a little stuck on how to use the timing function.

Many thanks.

Acetronics2
- 21st December 2005, 14:21
What is the easiest way to perform the following.

I then want a Confirmation GOSUB so that if the button is pushed say 3 times and the LED flashes 3 times, it gives you the opportunity to confirm your selection by pushing the button again.

So i would like a GOSUB which gives a 3 second window where if the button is pushed again after the flashes but at anytime during this 3 second window, the selection is confirmed and the GOSUB RETURNS, if no button is pushed within the 3 seconds window, the program starts over.


Many thanks.

Hi, Tissy

Something hurts me :

If you push the button 2 times, and want to confirm within 3 secs ... the device will see button pushed 3 times !!!
How do you want to understand increments from confirm pushes ???

May be you should have two timings : 1 sec window ( i.e. ) for increments and a 2 more sec window to confirm.

More than 3 sec will start over then.

Hope this is your project missing part !!!

Alain

Tissy
- 21st December 2005, 14:38
I know what you are saying, hopefully this will clarify.



Clear
DEFINE OSC 20
DEFINE LOADER_USED 1
ADCON1 = %00001111


' ---------- [ I/O Definition ] ----------
TRISA = %00000001 ' Set PORTA to all input
TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS
TRISC = %01000111 ' Set PORTC (0-2 input) rest Outputs
PORTB = 0

Switch VAR PORTC.0 ' use a pin with Schmitt Trigger input
Presses VAR BYTE
RedLED VAR PORTB.1
X VAR BYTE
LOOP VAR BYTE

Main:
if Switch = 1 THEN
WHILE Switch = 1 : WEND
COUNT Switch, 1500, Presses
BRANCHL Presses,[routine1, routine2, routine3]
GOTO MAIN
endif
GOTO Main

'--------------
routine0: ' button was not pressed again
' or pressed more than 3 times
GOTO Main

'--------------
routine1: ' pressed once
HIGH REDled
PAUSE 500
LOW REDled
GOSUB CheckButton
GOTO Main

'--------------
routine2: ' pressed twice
FOR X = 1 TO 2
HIGH REDled
PAUSE 500
LOW REDled
PAUSE 500
NEXT X
GOSUB CheckButton
GOTO Main

'--------------
routine3: ' pressed thrice
FOR X = 1 TO 3
HIGH REDled
PAUSE 500
LOW REDled
PAUSE 500
NEXT X
GOSUB CheckButton
GOTO Main

Flash:
For loop = 1 to 10
Red = 1
Pause 50
Red = 0
Pause 50
Next Loop
Return

Confirmation:

Return

Once the Switch has been pushed a number of time 1, 2 or 3, it then jumps to the routine. It is then within this routine that it looks for the confirmation Button push after the LED has flashed it corresponding number of times by going to the Confirmation subroutine.

Hope this makes sense.

Steve

dmairspotter
- 21st December 2005, 15:22
How about

Confirmation:

confirm_flag=0
x=0

WHILE (x<300) and (confirm_flag=0)

Pause 10 'check for button every 10ms
if Switch = 1 then confirm_flag=1 ' if button pushed then set the flag
x=x+1 ' increment the pause counter

WEND

Return


comfirm_flag will be 0 if no confiirm, 1 if confirmed. Routine will exit as soon as button is pushed.

Duncan

Tissy
- 21st December 2005, 15:56
Thanks Duncan, almost there is think.

Indeed it does now look at the Confirmation routine, but after three seconds, it is still in the routine and presumably waiting for the button push.

If the button is pushed, then it jumps out fine.

I just need it so that if no button is pushed after 3 seconds it jumps back to the main code.

Cheers

dmairspotter
- 21st December 2005, 20:33
You should drop out of the While...wend loop when the button is pushed or if the x variable exceeds 300. AH HA! In your code, X is a byte so never exceeds 255. Either change

(x<300) to (x<253) or change x to a word variable.

X<253 will give you a 2.5 second delay

Duncan

Tissy
- 22nd December 2005, 10:06
Spot on, it was the BYTE and not WORD variable that was causing the confliction.

All done now and working how i want.

Thanks again for taking the time to help. Merry Christmas.

Steve