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 ?

    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

  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 ?

    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 !

  3. #3
    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

  4. #4
    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.

  5. #5
    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.

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


    Did you find this post helpful? Yes | No

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

    No, I did not forget to add a label.
    EXIT is a keyword used to break out of the current loop.

    AI, simulation, virtualization.... What happened to the good old breadboard?

    Do I really need to spin this up on my dev board here or can you do some troubleshooting on your own and not just say "it's not working". You did include the same setupcode used before didn't you?

  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 ?

    It works ! Finally, the first program that shows me the duration. BUT .... the correct identification of the pressed button does not work ! I pressed ONLY button 0 : short or long ...It doesn't matter which button I press: if I press it briefly it indicates button 0, if I press it long it indicates button 5. Why?
    Anyway thanks for pointing into a good direction !
    Name:  term.jpg
Views: 66853
Size:  35.5 KB


    Solved !
    Name:  term2.jpg
Views: 66686
Size:  29.6 KB
    Last edited by fratello; - 29th March 2024 at 10:23.

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