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,645


    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

  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

    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 ?

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

  4. #4
    Join Date
    Aug 2011
    Posts
    453


    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?

  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

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

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

  7. #7


    Did you find this post helpful? Yes | No

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

    I have used 'for loop' to test for switch press duration as such....you would use your adc reading instead of GPIO check

    Code:
    if GPIO.1= 1 then start	       ' gpio is 1 for no press, 0 for pressed....or do 1 adc read and check ..about 100 microseconds
    
    FOR B5= 1 TO 100        '1 SECOND FOR TIMER
     PAUSE 10
     if GPIO.1= 1 then start	'or do 1 adc read and check ..about 100 microseconds
     NEXT B5
     
     '''' switch still pressed ...continue to 2 second press check
     
     FOR B5 = 1 TO 100       '2 SECONDS FOR 6 HR
     PAUSE 10
     if GPIO.1=1 then TIMER     'press is greater than 1 second in this loop... up to 2 second
     NEXT B5
      
    continuing here if switch still pressed !!!!

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