Long / short press button...but reading ADC


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

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

    Fratello, fiest I would change the value of R4 to say... 100 Ohms. That way you are giving the port something less than 1/2 vcc to be seen as a low. Next I would on the trailing edge of the button press, start a loop counter and count some number of time the button is in the down state. When the button is released check the time against a constant of your choosing and see if it longer or shorter than that.
    Dave Purola,
    N8NTA
    EN82fn

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


    Did you find this post helpful? Yes | No

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

    don't see this working
    won't your adc reading always be > 0 and it can never be higher than 1023

    If DataW > 0 AND DataW < 1023 then GPIO.2 = 1

    as dave says something like this

    Code:
    @ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON 
    DEFINE OSC  4
    long_press_threshold con 20   ;2 sec
    press_threshold      con 10   ;1 sec
    trigger_thresshold   con 600  ; adc reading must fall below this to countr as pressed
    CMCON    = 7
    TRISIO   = %00001001
    INTCON   = 0 
    IOC      = 0
    GPIO     = 0
    ANSEL    = %00110001
    ADCON0.7 = 1
    DataW    var WORD  ' Just a 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 100
        if b_ACT then
            if b_ACT ==1 then
               GPIO.2 = 0
            else
                GPIO.2 = 1
            endif
            b_ACT=0
            b_cnt=0
        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  then
          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
            ENDIF
       endif
    return
    Last edited by richard; - 1st April 2016 at 12:17. Reason: timing

  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

    Thank you both for such fast reply !
    Very good info !!! I will test and search for help again if something going wrong...

    LE : If DataW > 0 AND DataW < 1023 then GPIO.2 = 1
    DataW, with R3=R4=1k, it's "512", don't ?

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


    Did you find this post helpful? Yes | No

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

    I set the
    trigger_thresshold con 600 ; adc reading must fall below this to countr as pressed
    to 600 to allow a little safety margin
    ooops
    note I edited my post to fix timing Pause 100 instead of 50 but I forgot to press the save button till now sorry

  5. #5
    Join Date
    Aug 2011
    Posts
    461


    Did you find this post helpful? Yes | No

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

    I suppose there's some good reason for using the ADC to detect the keypress vs just looking for a digital high/low on the pin?

  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

    Yes, I NEED to use ADC instead "usual" button(s).

  7. #7
    Join Date
    Jan 2005
    Location
    France
    Posts
    97


    Did you find this post helpful? Yes | No

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

    just my two cents ..
    remove the Quartz and capacitors
    an use the internal oscillator if you don't need a precison time ..
    I love to use those small 12F675 ..
    Take care at programmation time you have datas (2 bytes) at the end of the eprom.
    read it before programming
    Francois

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 : 1

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