Long / short press button...but reading ADC


Closed Thread
Results 1 to 38 of 38

Hybrid View

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


    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

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

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


    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

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

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    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!

  6. #6
    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" ...

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,694


    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 ?
    do you in fact have multiple buttons each with a different R4 ?
    did you try my second example ? is it even relevant if not what needs to happen differently ?

    at this stage I still can't see how an array sorting routine is in any way useful for detecting button presses long or short .
    why not post the code for what have you tried

    dt_elapsed will not run on a12f675

Similar Threads

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

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