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
    579

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

    Hi !
    I need again for your help ! I tried to build one simple schematic ; I used 12F675 for reading ADC. Works fine.
    But ... I have no clue how to "find" if the button is pressed short (let's say under 1 sec) or long ( > 1 sec) ...for having different commands.
    Can someone point me into the right direction ?
    Thanks in advance ! Have a nice day and weekend !

    Code:
    @ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON 
    
    DEFINE OSC  4
    
    CMCON    = 7
    TRISIO   = %00001001
    INTCON   = 0 
    IOC      = 0
    GPIO     = 0
    ANSEL    = %00110001
    ADCON0.7 = 1
    
    DataW    var WORD		' Just a WORD Temporary working variable
    
    Main:
    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
    
    If DataW > 0 AND DataW < 1023 then GPIO.2 = 1
    Pause 1000
    GPIO.2 = 0
    Goto Main
    Attached Images Attached Images  

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

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


    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

  4. #4
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    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 ?

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    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

  6. #6
    Join Date
    Aug 2011
    Posts
    412


    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?

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


    Did you find this post helpful? Yes | No

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

    its on a 12f683
    the interrupt use is unnecessary , i left it in for experimenting with
    Attached Files Attached Files

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