Single button function


Closed Thread
Results 1 to 40 of 41

Hybrid View

  1. #1
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Will this work?

    Hi DynamoBen,

    I wrote a short bit of code here that compiles to 74 words. I'm not sure if it's what you were looking for but I thought I'd post it anyway.

    The first press and release of the button after a hearing a single beep of the buzzer will INCREMENT your variable. Any further (single quick press's) that sounds a single beep will continue to increment the variable and when your finished incrementing then holding the button down for 4.5 seconds will ENTER the variable and exit the loop.

    If you wanted to increment and enter another variable using the same push button then you could place another IF-THEN in the loop and when it beeps a set number of times then the code could jump out of that loop into another loop and the same basic code could be repeated for a different variable (with a little modification to the original code below).

    Also an Led could be used instead of the buzzer. I'm not sure but I think this is what you wanted?

    IF Settings_Push_Button = Is_Pressed THEN
    For i = 0 to 100'this ensures that we return to mainloop
    WHILE Settings_Push_Button = Is_Pressed
    PWM Buzzer,250,1 : PAUSE 1500 : Y = Y + 1
    IF Y = 3 THEN
    'Do the ENTER
    Y = 0
    IF Y = 0 THEN EXIT
    ENDIF
    WEND

    IF Y = 2 THEN
    Y = 0
    'Do the Increment
    ENDIF

    PAUSE 100
    NEXT i
    ENDIF

    EXIT:

    jessey

  2. #2
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Alright I'm back at this again. I can’t figure out an efficient way to do this. My original solution was:

    ButtonHeld con 30

    Switch_Routine:
    ButtonCnt=0
    While Switch=0:Wend
    Pause 250
    Return

    Main:
    ' display lcd info here

    IF Switch=0 Then
    GoSub Switch_Routine

    IF ButtonCnt>ButtonHeld Then
    ' change menu option
    Else
    ' increment value
    EndIF
    EndIF

    Goto Main


    Timer1 counts how long (100 ms interval) the switch is held. Once the user releases the switch then it would reenter the Main to execute the code. The problem is there is no feedback to the user if the button has been held long enough. The program stays at the While:Wend until the user releases the button. If I jump out of the switch prior to the release of the switch the loop keeps executing the same switch command until the user releases the switch. Remember: the switch is still being held down.

    I’m looking for ideas. If the user presses and releases the switch the program would increment value. If the user presses and holds the switch for 3 seconds or more it would change menu options.

    Basically I want two buttons in one.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Code:
    	MyButton var PortB.0		' Your Button can be anywhere
    					' Connect between PIC pin and Vss
    					' Use Weak Pull-Up or Resistor to Vdd
    
    	ButtonPress var BYTE		' Button Counter Variable
    
    	LongPress con 20		' Change this value for desired SET 
    					' function trip-point in 50mS steps
    					' Currently set for 1 Second
    
    	
    MainLoop:
    	LCDOut $FE,1,"Go Press..."
    ButtonLoop:
    	Gosub GetButton
    	If ButtonPress>0 then
    		If ButtonPress=1
    			LCDOut $FE,1,"Short Press"
    			else	
    			LCDOut $FE,1,"Long Press"
    			endif
    		Pause 1000
    		Goto MainLoop
    		endif
    	Goto ButtonLoop
    
    	'
    	'	Subroutine weighs-up users finger
    	'	in multiples of 50mS
    	'	Constant LONGPRESS determines boredom level
    	'	-------------------------------------------
    	'	on Exit...
    	'	ButtonPress=0 - No Press
    	'	ButtonPress=1 - Short Press
    	'	ButtonPress=2 - Long Press
    GetButton:
    	ButtonPress=0
    	While MyButton=0
    		If ButtonPress<255 then ButtonPress=ButtonPress+1
    		Pause 50 ' This is also our Debounce value
    		Wend
    	If ButtonPress>0 then
    		If ButtonPress=>LongPress then 
    			ButtonPress=2
    			else
    			ButtonPress=1
    			endif
    		endif
    	Return
    If you have an LCD for example, you can always clear the display (or light a LED if you have one) to indicate that a SET has been performed. Here's a variation..
    Code:
    	MyButton var PortB.0		' Your Button can be anywhere
    					' Connect between PIC pin and Vss
    					' Use Weak Pull-Up or Resistor to Vdd
    
    	ButtonPress var BYTE		' Button Counter Variable
    
    	LongPress con 20		' Change this value for desired SET 
    					' function trip-point in 50mS steps
    					' Currently set for 1 Second
    
    	
    MainLoop:
    	LCDOut $FE,1,"Go Press..."
    ButtonLoop:
    	Gosub GetButton
    	If ButtonPress>0 then
    		If ButtonPress=1
    			LCDOut $FE,1,"Short Press"
    			else	
    			LCDOut $FE,1,"Long Press"
    			endif
    		Pause 1000
    		Goto MainLoop
    		endif
    	Goto ButtonLoop
    
    	'
    	'	Subroutine weighs-up users finger
    	'	in multiples of 50mS
    	'	Constant LONGPRESS determines boredom level
    	'	-------------------------------------------
    	'	on Exit...
    	'	ButtonPress=0 - No Press
    	'	ButtonPress=1 - Short Press
    	'	ButtonPress=2 - Long Press
    GetButton:
    	ButtonPress=0
    	While MyButton=0
    		If ButtonPress<255 then ButtonPress=ButtonPress+1
    		Pause 50 ' This is also our Debounce value
    		If ButtonPress=LongPress then LCDOut $FE,1
    		Wend
    	If ButtonPress>0 then
    		If ButtonPress=>LongPress then 
    			ButtonPress=2
    			else
    			ButtonPress=1
    			endif
    		endif
    	Return
    Another variation which I tend to use, is if you have a Piezo, beep it at every Button Press start (ie ButtonCount=1) as a confidence indicator for the user (they like things like that), and then Beep Constantly once the LONGPRESS value has been reached. example...

    Code:
    	'
    	'	Subroutine weighs-up users finger
    	'	in multiples of 50mS
    	'	Constant LONGPRESS determines boredom level
    	'	-------------------------------------------
    	'	on Exit...
    	'	ButtonPress=0 - No Press
    	'	ButtonPress=1 - Short Press
    	'	ButtonPress=2 - Long Press
    GetButton:
    	ButtonPress=0
    	While MyButton=0
    		If ButtonPress<255 then ButtonPress=ButtonPress+1
    		Pause 50 ' This is also our Debounce value
    		If ButtonPress=1 then Gosub Beep
    		If ButtonPress=>LongPress then Gosub Beep
    		Wend
    	If ButtonPress>0 then
    		If ButtonPress=>LongPress then 
    			ButtonPress=2
    			else
    			ButtonPress=1
    			endif
    		endif
    	Return
    You can go on with variations for ever....

    Code:
    	'
    	'	Subroutine weighs-up users finger
    	'	in multiples of 50mS
    	'	Constant LONGPRESS determines boredom level
    	'	-------------------------------------------
    	'	on Exit...
    	'	ButtonPress=0 - No Press
    	'	ButtonPress=1 - Short Press
    	'	ButtonPress=2 - Long Press
    GetButton:
    	ButtonPress=0
    	While MyButton=0
    		If ButtonPress<255 then ButtonPress=ButtonPress+1
    		Pause 50 ' This is also our Debounce value
    		If ButtonPress=1 then Gosub Beep
    		If ButtonPress=>LongPress then Gosub Beep
    		If ButtonPress=255 then Gosub Klaxon ' Users fallen asleep
    		Wend
    	If ButtonPress>0 then
    		If ButtonPress=>LongPress then 
    			ButtonPress=2
    			else
    			ButtonPress=1
    			endif
    		endif
    	Return
    I use a WORD as the Counter in many cases, and if the Button is held for a really long time (eg 30 Seconds or even a Minute), it lets me jump into a Secret Set-Up Menu or just display an unexpected message for the User if I'm feeling devilish... or just as a 'Granny Button' to erase Passwords, Contrast and LCD Backlight levels that have been messed up. Users are a peculiar bunch... they put a password into the Menu (which they promptly forget) or save a near Invisible LCD Contrast setting and then they call Tech-Support to pull them out of the sh*t. It's nice to tell them to Press and Hold the Button for 30 minutes (or until their finger goes numb - whichever comes first) and everything will reset back to the factory default settings... (it'll reset after a minute, but if you tell them 30 they'll not do it a second time).

  4. #4
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Melanie as always thank you for your examples and advice.

    What I'm seeing in your examples is what I'm already doing. A single short press functions normally and the user sees a value increment when they release the switch. However in the case of a long press (ENTER) they have no idea, short of adding an led/buzzer/clear screen, that they have held the switch long enough to change menus. Basically they have to count in their head.

    Is this just something I need to live with? Do you run into any problems with this type of interface?

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Wherever possible I use a 3-button interface (Up/Down/Set), but if I'm stuck with one button, I try to provide some kind of user feedback so they know where they stand...

    Example 1... www.k3planet.com/Datasheet-9910-1.pdf

    ...and people thought I was kidding when I said I put "Yankee Doodle" into a commercial product!

  6. #6
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Nice!

    Alright I give; I either need to use another pin for feedback or add another switch. I guess I need to move from a minimalist single switch project to a two switch project.

    Thanks for the assistance.

  7. #7
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by DynamoBen
    However in the case of a long press (ENTER) they have no idea, short of adding an led/buzzer/clear screen, that they have held the switch long enough to change menus. Basically they have to count in their head.
    Is this just something I need to live with? Do you run into any problems with this type of interface?
    What about having the menu change happen automatically after a button HOLD of say 1.5 seconds.
    The user sees the menu change, so they know they've held the button long enough.
    After the menu changes, your code then waits for the button to be released before continuing - avoiding spurious inputs at the new menu.
    If the user is goofy enough to continue holding the button, the program just sits waiting until they release it (or rig up a shocker on the button so they get zapped after 20 seconds :-)

    Only quicly looked over Melanies samples - maybe she already suggested something like that?

    Arch
    "Data sheets? I ain't got no data sheets. I don't need no data sheets. I don't have to read any stinking data sheets!"

  8. #8
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Archilochus
    What about having the menu change happen automatically after a button HOLD of say 1.5 seconds.
    The user sees the menu change, so they know they've held the button long enough.
    After the menu changes, your code then waits for the button to be released before continuing - avoiding spurious inputs at the new menu.
    If the user is goofy enough to continue holding the button, the program just sits waiting until they release it (or rig up a shocker on the button so they get zapped after 20 seconds :-)

    Only quicly looked over Melanies samples - maybe she already suggested something like that?

    Arch
    This is exactly what I wanted to do. However it’s a programming nightmare. I have been working on this for days and have come up with nothing. It is simple to describe but difficult to implement.

  9. #9
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Do you not have anything that can be used as feedback to the user? Describe your User Interface (OK, so we know it's got ONE button) - but what else has your device got in the way of LEDs LCDs or Beepers? And what do you want the Button to Do?

Similar Threads

  1. Sony SIRC IR Issue
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th August 2015, 08:10
  2. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  3. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43
  4. Pushbutton code routine suggestions?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd September 2005, 01:02
  5. Button subfunction 16F628
    By Jųan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th August 2005, 16:44

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts