Long / short press button...but reading ADC


Closed Thread
Results 1 to 38 of 38

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Code from Mr.Richard (Thanks !), see post #3, works just fine.
    But now I'm stuck again ...
    For accurate reading of ADC I use bubble-sort algoritm, how it's presented on this forum.
    Code:
    Main: 
    nr=1	
    for nr = 1 to 2
    For CounterA=0 to 8
    ADCON0 = %10000001
    		Pauseus 50			' Wait for channel to setup
    	        ADCON0.1 = 1			' Start conversion
    		While ADCON0.1=1:Wend		' Wait for conversion
    		DataW.HighByte=ADRESH		' Read variable from ADC and save
    		DataW.LowByte=ADRESL
    		RawData(CounterA)=DataW
    Next CounterA
    CounterA=0 
    Gosub Getsort
    next nr
    if adca=adcb then gosub efectuez		
    goto main
    
    
                             
    ;============================================================================
    GetSort:
    	If RawData(CounterA+1) < RawData(CounterA) then
    		DataW=RawData(CounterA)
    		RawData(CounterA)=RawData(CounterA+1)
    		RawData(CounterA+1)=DataW
    		If CounterA>0 then CounterA=CounterA-2
    		endif
    	CounterA=CounterA+1
    	If CounterA<9 then goto GetSort
    
    
    	DataW=0
    	For CounterA=3 to 6 
    		DataW=DataW+RawData(CounterA)
    		Next CounterA
    	ADCValue=DataW>>2
    if nr = 1 then 
    adca = adcvalue
    else
    adcb = adcvalue		
    endif
    Return
    ;============================================================================
    The code works amazing, almost no error ... for different value of ADC, I can do different tasks.
    BUT ... I can not figure out how to use the procedure of short/long press in this code... since even a short press makes the code to jump to "efectuez" (where I have different commands defined).
    I tried to use the same procedure as in post #3 ; I tried to put it into different places of the code, but without result ! Whatever I do, it's like pressing the button for a short time.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    can you explain what you are trying to achieve , and maybe another schematic .
    at this stage I can't see how an array sorting routine is useful for detecting button presses long or short .
    post the code for what have you tried

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


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    In the schematic posted in #1, R4 have different values ... for different commands.
    The code above is for reading accurate ADC ; works very, very fine.
    Tried to read short/long press of button ... but no succes. No matter what I do, I always have same result, as pressed "short".
    Sorry for my poor english, I use g...translate for many words.

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


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    i'm assuming you have multiple buttons each with a different R4 so that in this example ---- with b1 pressed adc read is < 600,b2 pressed adc read is < 500,b3 pressed adc read is < 400.

    this pretty much the same as my first example but it keeps track of which button of the three is pressed and for how long.

    untested
    Code:
    @ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON 
    DEFINE OSC  4
    long_press_threshold con 40   ;2 sec
    press_threshold      con 8   ;.4 sec
    trigger_thresshold_1   con 600  ; adc reading must fall below this to count  as a pressed button 1
    trigger_thresshold_2   con 500  ; adc reading pressed button 2
    trigger_thresshold_3   con 400  ; adc reading pressed button 3
    CMCON    = 7
    TRISIO   = %00001001
    INTCON   = 0 
    IOC      = 0
    GPIO     = 0
    ANSEL    = %00110001
    ADCON0.7 = 1
    last_b_level  var byte      ;   last button 
    b_level       var byte      ;   which button
    DataW         var WORD  '  WORD Temporary working variable
    b_cnt         var byte
    b_ACT         var byte      ;0 not pressed ,1 pressed for 1 sec ,2 pressed for 2 sec
    clear
    Main:
        gosub chk_sw
        Pause 50
        if b_ACT then
        if b_level==2 then        ;button 2
            if b_ACT ==1 then
               GPIO.2 = 0
            else
                GPIO.2 = 1
            endif
            b_ACT=0
            b_cnt=0
            b_level=0
        endif
        endif
    Goto Main    
        
    
    chk_sw:
    ADCON0 = %10000001 ;ch0
    Pauseus 50  ' Wait for channel to setup
    ADCON0.1 = 1  ' Start conversion
    While ADCON0.1=1:Wend ' Wait for conversion
    DataW.HighByte=ADRESH  ' Read variable from ADC and save
    DataW.LowByte=ADRESL
       if DataW <  trigger_thresshold_1     then
          b_level =1
       elseif  DataW < trigger_thresshold_2 then
          b_level =2
       else  DataW < trigger_thresshold_3   then
          b_level =3
       endif
       if   b_cnt==0 then  last_b_level= b_level  ;first press begin
       if b_level= last_b_level then      ; same  btn is active add to count
          
          b_cnt=b_cnt+1   ;if btn is active add to count
          IF  b_cnt > long_press_threshold THEN b_ACT= 2  
       ELSE    ;button released
            if b_cnt > press_threshold THEN   ;press_threshold met
                IF  b_cnt > long_press_threshold THEN
                   b_ACT= 2           ;LONG press >2 SEC
                ELSE
                   b_ACT= 1
                ENDIF
            ELSE       ;press_threshold not met
            b_ACT= 0
            b_cnt=0    ;reset count
            b_level=0
            ENDIF
       endif
    return

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


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Thanks again for support !
    Unfortunately I can not solved this ... because "For accurate reading of ADC I use bubble-sort algoritm, how it's presented on this forum."
    No matter what I tried ; it's available ONLY short press.
    Regards !

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    What if you start a timer?

    DT's routines could tell you how long a button is pressed.

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

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


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Thanks !
    I thought about this ... Should I use this ?
    I don't think I have "space" ...

Similar Threads

  1. Replies: 5
    Last Post: - 26th February 2011, 05:51
  2. Button press or not
    By lerameur in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th November 2010, 20:37
  3. 4 Bytes one button press
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th January 2010, 22:36
  4. Sleep until button press?
    By kevj in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th October 2007, 03:47
  5. Button press and press & hold how to ?
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd August 2007, 03:37

Members who have read this thread : 3

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