How to measure how long I pressed a button ?


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    in order to measure a buttons hold time you need to either note time button was pressed then the time button was released
    the duration is release time - when pressed time, or when button is pressed start a timer and then stop it when button released
    you are not doing either of these things
    you never start timer1, you never note when the button is released
    Warning I'm not a teacher

  2. #2
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    Thanks for reply !
    T1CON = %00000110 ; this command does not start timer1?
    Or do I have to use the command: T1CON.0 = 1 'start Timer1 ?
    How to see if button is released ? IF DataW.... it's not enough ?
    Last edited by fratello; - 28th March 2024 at 09:19.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    No it just sets it to use external clock

    Name:  TIMER1.jpg
Views: 73103
Size:  155.9 KB
    Warning I'm not a teacher

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    you have set timer1 to overflow every 16.38 mS , you never count the overflows
    also adding 65535 to a word var seems very odd do you actually intend to subtract 1


    i can't see how any of this can work, i makes no sense to me

    Code:
    while durata < longpress
        	if T1CON.0 == 1 then
            	overflow_counter_low = overflow_counter_low + 1
            	if overflow_counter_low == 65535 then
                		overflow_counter_low = 0
                		durata = durata + 65535
            	endif
        	endif    
        	durata = durata + (TMR1H * 256 + TMR1L)
    wend
    Warning I'm not a teacher

  5. #5
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    Quote Originally Posted by richard View Post
    you have set timer1 to overflow every 16.38 mS , you never count the overflows
    also adding 65535 to a word var seems very odd do you actually intend to subtract 1
    ....
    i can't see how any of this can work, i makes no sense to me
    Gemini code !

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    What happens if more than one button is pressed at the same time?

    Anyway, this might serve as starting point. It compiles but it is not tested.
    Code:
    Long_Press CON 5000
    Short_Press con 100
    
    cnt VAR WORD
    b_level VAR BYTE
    DataW VAR WORD
    
    Main:
        GOSUB ReadButton
        
        IF b_level > 0 THEN
        	cnt = 0
            WHILE b_level > 0  
        	   cnt = cnt + 10  
        	   if cnt > Long_Press THEN EXIT
        	   PAUSE 10
        	   GOSUB ReadButton
        	WEND
        	
        	SEROUT GPIO.2, 2, ["You pressed button ", #b_level, " for ", #cnt, "ms", 13, 10]
        endif
    Goto Main	
    
    
    ReadButton:
        ADCON0.1 = 1
    	While ADCON0.1 = 1 : Wend 
    	
        DataW.HighByte = ADRESH 
        DataW.LowByte = ADRESL
    
    	b_level = 0
    	if DataW < 805 then
            	if DataW > 50  AND DataW < 160 THEN b_level = 1
            	if DataW > 180 AND DataW < 270 THEN b_level = 2
            	if DataW > 290 AND DataW < 380 THEN b_level = 3
            	if DataW > 400 AND DataW < 500 THEN b_level = 4
            	if DataW > 540 AND DataW < 650 THEN b_level = 5
            	if DataW > 690 AND DataW < 800 THEN b_level = 6
    	ENDIF
    RETURN

  7. #7
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    Thanks !
    But nothing on serial ...(in Proteus 8)
    Last edited by fratello; - 28th March 2024 at 18:33.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    forget Proteus.

    Also I think Henrik forgot to add the label Exit: just before the Serout command.

    Ioannis
    Last edited by Ioannis; - 28th March 2024 at 19:03.

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to measure how long I pressed a button ?

    Quote Originally Posted by fratello View Post
    How to see if button is released ? IF DataW.... it's not enough ?
    i possibly could be enough but you never reevaluate it until the whole timing mess has long completed
    Warning I'm not a teacher

Similar Threads

  1. TMR1 & Button - long delay (1 min) before starting
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 14th March 2017, 13:03
  2. Long / short press button...but reading ADC
    By fratello in forum mel PIC BASIC Pro
    Replies: 37
    Last Post: - 10th July 2016, 08:37
  3. Replies: 5
    Last Post: - 26th February 2011, 05:51
  4. Replies: 1
    Last Post: - 14th October 2010, 18:33
  5. Button pressed time control
    By tanero in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 15th August 2005, 15:17

Members who have read this thread : 2

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