Button time

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

Button time

Post by pcleats »

Hello all,

I am looking for a way to measure the amount of time a button has been pressed.

1st press of the button turns on the system (working)
2nd press turns additional features (working)
3rd press turns everything off, but what I need is some type of delay that prevent accidentally turning off the system. So when I press the button a 3rd time and hold it for 3 seconds then the system will shut itself off.

What is the best way to do this.

Code: Select all

'Check to see if the start button has been pressed
'
'
Sub PowerButtonTask() 
   Do 
' Wait for a button press 
      Call WaitForInterrupt(zxPinFallingEdge, 0) 
'
' Toggle the device state
'
	Call PutPin(RelayPin, IIf(deviceIsOn, Power_off, Power_On)) 
	Call PutPin(RedLED, LEDon)
	Call Putpin(PowerLED, LEDon)
	deviceIsOn = not deviceIsOn
	'Call Delay(0.500) '500mS time delay
'
'If the power is on then go check if button has been pressed a second time
'Turn on the wireless receiver
If deviceIsOn = True and LowBatt = False then 
		Call WaitForInterrupt(zxPinrisingEdge, 0)
		Call PutPin(GreenLED, LEDon)
		Call PutPin(Wirelesspin, Power_On)
		Call LCD_DisplayStrAt("W", 1, 15)
end if
'
'If the button has been pressed a 3rd time turn everything off
'
If deviceIsOn = False then
	Call PutPin(GreenLED, LEDoff)  
	Call PutPin(RedLED, LEDoff)
	Call PutPin(PowerLED, LEDoff)
	Call Putpin(Wirelesspin, Power_off)
	Call LCD_Clear() 
end if
'
' Time delay for switch debounce
'
Call Delay(0.500) '500mS time delay
   Loop 
End Sub 


Thanks for the help
Patrick
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

I've implimented something similar in one of my apps. Here's some pseudocode (not tested)

Code: Select all

'***********Button monitor routine******************
	Dim Hour as Byte, Minute as Byte, Second as Single, LongPressTime as Single, ShortButton as Boolean, LongButton as Boolean
LongPressTime = 5.0 'time in seconds
			Do
				If GetPin(ButtonInputPin) = 0 Then 'uh oh, someone pressed the button
				'Check if 5 seconds have passed
					Call PutTime(0, 0, 0.0) 'Reset the RTC
					Do While GetPin(ButtonInputPin) = 0
						Sleep(0.0) 'just wait
					Loop
					Call GetTime(Hour, Minute, Second) 'Check the time
					If Second > LongPressTime Then 'Check if the button was pressed for more than the Long press time
						LongButton = True
					Else 
						ShortButton = True 'Short button press
					End If
				Else 
					ShortButton = False
				End If
			Loop
-Don
Post Reply