Single button function


Closed Thread
Results 1 to 40 of 41

Hybrid View

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


    Did you find this post helpful? Yes | No

    Lightbulb Finally got it!

    After working on this for some time I have come up with a routine that does what I need it to do. I may use Timer1 to keep track of how long the button is held in place of PAUSE, but it works as is.

    There are three button states 0, 1, 2:
    0=no action
    1=short press/release
    2=Held


    Switch_Routine:
    ButtonState=0
    IF Switch=1 Then ButtonCnt=0

    IF Switch=0 Then
    While Switch=0
    Pause 250
    ButtonCnt=ButtonCnt+1

    IF ButtonCnt=ButtonHeld Then
    ButtonState=2
    Return
    EndIF
    Wend

    IF ButtonCnt<ButtonHeld Then ButtonState=1
    EndIF
    Return
    Last edited by DynamoBen; - 12th August 2006 at 18:44.

  2. #2
    Join Date
    Jun 2005
    Location
    Germany
    Posts
    6


    Did you find this post helpful? Yes | No

    Question Button code doesn't work

    Say hello to everyone,

    where is the fault in this example:


    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

    I have this Sample code compiled and get this error message:

    ERROR Line 18: Bad expression or missing THEN, (Button_01.php)

    See attachment

    I am here new and am pleased about every help.

    Konrad
    Attached Images Attached Images  

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Post

    Hi, Konrad

    I used such a "multi role" button simply by using the BUTTON Command in a loop...

    the example is here,

    Code:
    '*****************************************************************************
    'Servotest 84 D'après Elektor
    '*****************************************************************************
    '
    '16F84A à 20 Mhz
    
    ' Barregraph 7 leds sur PortB
    ' résolution variable 2-6-20 µS
    ' Mémorisation réglage " moyen"
    
    @  __config _HS_OSC & _WDT_ON & _CP_ON
    
    DEFINE OSC 20 ' 
    DEFINE BUTTON_PAUSE 18
    
    Signal	var PortA.0
    Writeok	var PortA.1 ' Indicateur mode : 0 = manuel 1 = Cycle
    Up 		var PortA.2	' Rotation à Droite
    Down 	var PortA.3 ' Rotation à gauche
    Prog 	var PortA.4 ' Bouton mémorisation
    
    pos 	var Word
    posn 	var Word
    
    compte	var Byte
    dir 	var Byte
    delay	var Byte
    N		var Byte
    
    PORTA 	= 0
    PORTB 	= %00001000 'PortB.7 libre, Led centrale allumée.
    
    TRISA 	= %00011100
    TRISB 	= %00000000
    
    
    READ 1, Posn.HighByte
    READ 2, Posn.LowByte
    
    IF posn = $FFFF OR posn < 750 OR posn > 2250 THEN posn = 1500
    pos = posn
    
    '*****************************************************************************
    mainloop:
    
    Writeok = 0
    
    delay = 0
    
    button Up, 		0, 255, 0, delay, 1, upbutton
    button Down, 	0, 255, 0, delay, 1, dwnbutton
    button Prog, 	0, 255, 0, delay, 1, Program
    
    Gosub Outsig
    PAUSE 18
    compte = 0
    
    Goto Mainloop
    
    
    '*****************************************************************************
    upbutton:
    
    IF Down = 0 THEN 
    
    	Pos = posn
    	Goto Jump1
    	
    Endif
    
    	Compte = compte + 1
    	
    	Select Case Compte
    	
    		Case 255
    			Compte = Compte-1
    			N = 10
    			
    		Case is > 50
    			N = 10
    				
    		Case is > 15 
    		 	N = 3
    		 	
    		Case else
    			N = 1
    			
    	End Select
    	
       	pos = pos + 2*N
       
       	if pos > 2250 then  pos = 2250
      
    Jump1:
    
    For delay = 1 to 9	
    
    	Gosub Outsig
    	Pause 18
    
    Next delay
    
    	Gosub Outsig
    
    Goto mainloop
    
    
    '*****************************************************************************
    dwnbutton:
    
    IF Up = 0 THEN 
    
    	Pos = posn
    	Goto Jump2
    	
    Endif
    
    	Compte = compte + 1
    	
    	Select Case Compte
    	
    		Case 255
    			Compte = Compte - 1
    			N = 10
    			
    		Case is > 50
    			N = 10
    			
    		Case is > 10 
    		 	N = 3
    		 			
    		Case else
    			N = 1
    			
    	End Select
    	
       	pos = pos - 2*N
       
       	if pos < 750 then pos = 750
       	 
    Jump2:
    	
    For delay = 1 to 9	
    
    	Gosub Outsig
    	Pause 18
    
    Next delay
    
    	Gosub Outsig
    
     Goto mainloop
     
    
    '*****************************************************************************
    Program:
    
    Compte = 0
    
    Test: 
    
    IF Prog = 0 Then 
    	
    	compte = compte + 1
    	Gosub Outsig
    	Pause 18
    	
    	IF Compte > 250 THEN Compte = 250: Goto Test
    	IF Compte >= 80 THEN WriteoK = Compte .4 : Goto Test
    	IF Compte >= 20 THEN WriteoK = Compte .2
    	
    	Goto Test		'Attendre relâchement bouton ...
    	
    Endif	
    	
    	IF Compte > 250 OR Compte < 20 Then mainloop
    	
    	IF Compte >= 80 Then
    	 
    		pos = 1500 
    		Goto Save	'remise au Neutre
    
    	Endif	
    
    					'Position "moyenne"
    
    
    Save:
    
    WRITE 1, Pos.HighByte			' 10ms
    WRITE 2, Pos.LowByte			' 10ms
    
    	Posn = pos
    	Gosub Outsig
    
    For delay = 1 to 25
    	
    	WriteoK = 1
    	Gosub Outsig
    	Pause 18
    	
    Next Delay
    	
    	Goto mainloop
    
    
    	
    '*****************************************************************************
    Outsig:
    
    Select Case pos			'Allumage Bargraph
    
    	Case 750
    	
    		PortB = %01100001
    
    	Case is <= posn - 635
    	
    		PortB = %01000000
    		
    	Case is <= posn - 519
    	
    		PortB = %01100000
    		
    	Case is <= posn - 404
    	
    		PortB = %00100000
    		
    	Case is <= posn - 288
    	
    		PortB = %00110000
    		
    	Case is <= Posn - 173	
    	
    		PortB = %00010000
    		
    	Case is <= Posn - 58
    	
    		PortB = %00011000
    		
    	Case is <= Posn
    	
    		PortB = %01001000
    		
    '*****************************************************************************		
    	Case posn					' Position centrée
    	
    		PortB = %01001001
    '*****************************************************************************
    			
    	Case is <= posn + 58
    	
    		PortB = %00001001
    		
    	Case is <= posn + 173
    	
    		PortB = %00001100
    		
    	Case is <= posn + 288
    	
    		PortB = %00000100
    	
    	Case is <= posn + 404
    	
    		PortB = %00000110
    			
    	Case is <= posn + 519
    	
    		PortB = %00000010
    		
    	Case is <= posn + 635
    	
    		PortB = %00000011
    		
    	Case is < posn + 750
    	
    		PortB = %00000001
    		
    	Case 2250
    	
    		PortB = %01000011
    		
    END Select
    		
    Low Signal
    Pulsout Signal, pos /2		'Envoi Signal servo
    
    RETURN
    
    END

    The "important" sections have been Highlighted ...

    Here, the more the pressing UP and DOWN buttons speed up the 'count speed' ... pressing "prog" permits to choose beetween options ...
    Note WriteOk is a led that shows the delay increasing value when pushing the button ...

    Alain
    Last edited by Acetronics2; - 15th January 2008 at 12:58.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Jun 2005
    Location
    Germany
    Posts
    6


    Did you find this post helpful? Yes | No

    Post

    Hi, Alain,

    thanks for the fast reaction and the Code sample.

    I would like to build a "One Button Dimmer"

    1. Short Press Button T1: Toggle LED1 on/off or off/on.

    2. Long Press Button T1: Ramp the Light von LED1 up or Ramp the Light down depending on the one last "Long Press", Ramp direction toggle every "Long Press".

    3. Before power off store the last Ramp value

    See the schematic in the attachment

    Every help is welcome,

    Konrad
    Attached Images Attached Images  

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Talking Slb 0587

    Hi, Vtt

    A look Here, may be ...

    http://www.electronique-diffusion.fr...lb0587&x=2&y=8

    ...

    some chips still on the market !!! ... and in my drawers ... LOL !

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    Join Date
    Feb 2013
    Posts
    1,137


    Did you find this post helpful? Yes | No

    Default Re: Slb 0587

    Due to my lack of knowledge, when facing same problem, I did the following - while user keeps button pressed, value of needed variable is being increased and value of waiting variable reset. As user releases the button, waiting variable is being increased, and after it reaches some value (say for 2 seconds), next chunk of code is required. I'd like to do the same with long press, but still can't figure how.

    Any ideas to add long press detection to this code?

    Code:
    setuploop:
    if but2=0 then
    dlycnt=dlycnt+1 'increase debounce variable while button is pressed
    
    pause 1
    endif
    
    if BUT2=1 and dlycnt>100 then 'if button pressed long enough
    dlycnt=0
    high buz 'enable buzzer pin
    menuitem=menuitem+1
    lcdout $fe,1, #menuitem, " pressed "
    pause 5
    low buz 'disable buzzer
    endif
    goto setuploop

  7. #7
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Single button function

    IF Button = Pressed THEN
    WHILE Button = Pressed
    LOOP
    ENDIF

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 : 1

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