How to measure how long I pressed a button ?


Results 1 to 19 of 19

Threaded View

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

    Default How to measure how long I pressed a button ?

    Howdy ! I'm back on the forum after some absence (I had some personal problems).
    I am trying to develop a project. I use a PIC12F675 with which I read the value of a group of 6 resistors, of different values, individually, to perform 6 specific tasks.
    I am attaching the diagram. I am also attaching the code used so far, by which I try, in the first phase, to read if a button was pressed briefly or long, in order to make two different decisions depending on the duration of the press.
    I wasted many hours with gemini/chatgpt, none of them managed to identify the problem of the code not working. I tried dozens of options, without any results.
    What proved to me, once more, that we are superior (still?) to artificial intelligence, at least in certain aspects.

    Can you help me identify and solve the problem? Thank you in advance !


    Code:
    @ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON & _CP_ON 
    
    DEFINE OSC  4
    INCLUDE "modedefs.bas"
    CMCON    = 7
    TRISIO   = %00001001
    INTCON   = 0 
    IOC      = 0
    GPIO     = 0
    ANSEL    = %00110001
    
    
    
    ' Constante
    longpress CON 5000 ' maximum press (5 sec)
    shortpress CON 100 ' minimum press
    
    ' Variabile
    
    durata 		var word
    PressStartTime 	var word
    overflow_counter_low var word
    T1CON = %00000110 ' Prescaler  1:64, activating Timer1
    TMR1H = 0 ' Resetam Timer1
    TMR1L = 0
    
    GPIO.2 = 0
    ADCON0 = %10000001
    PAUSEUS 100 
    
    Main:
    
        durata = 0
        overflow_counter_low = 0
    
    
        ADCON0.1 = 1
        While ADCON0.1 = 1 : Wend 
    
    
        DataW.HighByte = ADRESH 
        DataW.LowByte = ADRESL
    
    
    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
    
    
            if b_level <> 0 then
                TMR1H = 0
                TMR1L = 0
                PressStartTime = TMR1L
    
    	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
    
    	durata = durata - PressStartTime
            PAUSE 100
    	SEROUT GPIO.2, 2, ["Press for : ", #durata, " ms", 13, 10]
    
            endif
    
    endif
    
    
    GOTO Main
    Name:  schematic.BMP
Views: 16966
Size:  36.2 KB
    Last edited by fratello; - 27th March 2024 at 17:55.

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, 14:03
  2. Long / short press button...but reading ADC
    By fratello in forum mel PIC BASIC Pro
    Replies: 37
    Last Post: - 10th July 2016, 09:37
  3. Replies: 5
    Last Post: - 26th February 2011, 06:51
  4. Replies: 1
    Last Post: - 14th October 2010, 19:33
  5. Button pressed time control
    By tanero in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 15th August 2005, 16:17

Members who have read this thread : 20

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