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


    Did you find this post helpful? Yes | No

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

    your correct tumbleweed , I think there is another issue there with that code too but fratello has not indicated that multiple buttons actually are on the agenda so I left it as is.
    if anyone is interested I do have a multiple button version that is complete and works .i'm quite surprised just how well the idea actually works so I developed it a bit further .its a good option if pins are limited.
    I would be interested to see fratellos raw data and see if noise is really the issue
    Attached Images Attached Images    

  2. #2
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

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

    Another thing that the sort and average routine in post #11 does is to perform the routine twice and compare the two resulting averages before declaring "I see a valid key" (if adca=adcb then gosub efectuez).

    That could be adapted to fit into the "check_sw" portion of your code by changing some of the post#11 averaging variables around and only
    changing the "result" DataW on a match...
    Code:
    if adca=adcb then
      DataW = adca
    endif
    return
    How well all that works depends on the noise, how many switches (and how far apart the values are), etc, but I could see where it'd be better than just using a single adc value for sure.


    if anyone is interested I do have a multiple button version that is complete and works
    Sure. Love to see it.

  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 all for support !
    Very interesting suggestions and points-of-view ...
    @Richard :
    if anyone is interested I do have a multiple button version that is complete and works ...
    Of course !!!

  4. #4
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

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

    I could be wrong but I have to ask about using "CALL" instead of GOSUB for the sub routines CHECK and achizitie.

    From the MCS Help section:
    CALL Label

    Execute the assembly language subroutine named Label. GOSUB is normally used to execute a PicBasic subroutine. The main difference between GOSUB and CALL is that with CALL, the existence of Label is not checked until assembly time. Using CALL, a Label in an assembly language section can be accessed that is otherwise inaccessible to PicBasic.

    Example

    CALL pass ' Execute assembly language subroutine named _pass
    I've never used it in place of GOSUB so don't know if a RETURN works the same in this case.
    Louie

  5. #5


    Did you find this post helpful? Yes | No

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

    I've done multi-button with long press (and timed no-button auto-backout) on a '88 using ADC to save pins, and it worked fine, but field EMF noise can wreak absolute havoc on your results (fluorescent lights much?) if you're not using twisted, and preferably shielded wire. That could be what's throwing you out of your "consistent" results needed to qualify for a long-press. Ceradiodes or double-zeners can bleed off freak transients, and a capacitor at the chip A/D input pin would help smooth things out.

  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

    @Mr.Richard : Tried to figure out how your soft work ... But in Proteus nothing happens.
    I do something wrong ? I "addapted" the "elseif" command, since is not present in PBP 2.6 (?).
    Thanks in advance for support !
    Code:
    ;pic12f683
    
    DEFINE OSC  8
    include "dt_ints-14.bas"
    include "REENTERPBP.bas"
    
    @timer1 =TMR1L
    wsave   VAR BYTE    $20     SYSTEM      ' location for W if in bank0
    wsave1  VAR BYTE    $A0     SYSTEM      ' location for W if in bank1
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,           Type,   ResetFlag?
            INT_Handler   TMR1_INT,   _chk_sw,      PBP,  yes
         endm
        INT_CREATE      
    ENDASM     
    
    long_press_threshold   con 200   ;1 sec       200*5( time const)=1000mS
    short_press_threshold  con 5 
    btn_tol                con 25   ;ADC READING TOLORACE
    number_of_buttons      con 3    ;MAX 7       
    ;4K7 TO 5V                                         
    '                               3k3            /  
    '                         ___  /\  /\  /\  ___/ ^____ btn1
    '                        |   \/  \/  \/  \/          |
    '   5v     4k7           |       2k2           /     |
    '    ___  /\  /\  /\  ___!___  /\  /\  /\  ___/ ^____!btn2
    '       \/  \/  \/  \/   |   \/  \/  \/  \/          |
    '                        |       1k            /     |
    '    ____________________!___  /\  /\  /\  ___/ ^____!btn3
    '    an3  gpio4              \/  \/  \/  \/          |
    '                                                    |
    '    ________________________________________________! 
    '    gnd                                             |
    '    ___________|\|__led_____________________________|
    '    gpio2      |/|
    
    OSCCON=$70
    CMCON0    = 7
    TRISIO   = %111011
    ANSEL    = %01011000        ;fosc/16 ,an3
    btn_adc var word[number_of_buttons]
    b_cnt   var byte[number_of_buttons]
    b_state var byte[number_of_buttons]     ;0 not pressed ,1 short ,2 long
    button_index  var byte      ;which button
    led           var gpio.2    ;led
    btn_flg       var byte 
    abp           var BIT  
    adcr          var WORD 	    ;adc reading
    timer1        VAR WORD EXT
    tmp           var word     
    FLASH_ON      VAR WORD      ;led
    FLASH_OFF     VAR WORD      ;led
    FLASH_DUTY    VAR WORD      ;led
    led = 0 
    clear
    
    btn_adc[0]=209           ;adc read for buttons
    btn_adc[1]=327           ;adc read for buttons
    btn_adc[2]=422           ;adc read for buttons
    
    timer1=55543             ;5mS  time const
    T1CON=1
    @ INT_ENABLE  TMR1_INT
    
    FLASH_OFF=200
    FLASH_On=0
    ADCON0 = %10001101     ;warm up the adc  ,right shift   ,an3
    ADCON0.1 = 1            ' Start a conversion
    
    Main:
       Pause 5   ;loop time const  
        IF ABP THEN 
           button_index= 0
           tmp=20
          
           while abp     
               if  B_STATE[button_index]=2 then
                   FLASH_OFF= tmp
                   abp=0
                   led=0
                   FLASH_DUTY=0
               else 
                   if   B_STATE[button_index]=1 then
                   FLASH_On=tmp
                   abp=0
                   led=0
                   FLASH_DUTY=0   
                   endif
               endif
               tmp=tmp+100
               B_STATE[button_index]=0
               button_index=button_index+1
               if  button_index= number_of_buttons then abp=0  ;clr error  if req 
            wend          
        ENDIF  
         IF  FLASH_ON THEN
           IF FLASH_DUTY=0 THEN
           led = ! LED     ; flash led
            	IF LED THEN
             	FLASH_DUTY = FLASH_ON
            	ELSE
             	FLASH_DUTY = FLASH_OFF
            	ENDIF
           ELSE
           FLASH_DUTY=FLASH_DUTY-1
           ENDIF       
         ENDIF        
        
    Goto Main     
       
    chk_sw:
        T1CON.0=0
        timer1=timer1+55543 
        T1CON.0=1
        ;ADCON0 = %10001101 ; NO NEED since ch never changes and ADC is left ON 
        ;Pauseus 50		'  aqusition  time  if req
        ;ADCON0.1 = 1		' Start conversion
        ;While ADCON0.1=1:Wend	' Wait for conversion
        adcr.HighByte = ADRESH	' Read variable from ADC 
        adcr.LowByte  = ADRESL
        FOR button_index = 0 TO 2
              if ! B_STATE[button_index] then
                  IF    abs (adcr- btn_adc[button_index] )< btn_tol  then
                       if  btn_flg.0[button_index]  THEN
                            IF b_cnt[button_index] < 250 THEN  b_cnt[button_index]=b_cnt[button_index]+1   
                            ELSE   ;FIRST PRESS  FOR  THIS BUTTON 
                            b_cnt[button_index]=0
                            btn_flg.0[button_index]=1
                            ENDIF            
                       ELSE
                       IF   btn_flg.0[button_index]  THEN    ;  HAD   THIS BUTTON  BEEN PRESSED  ?
                           IF   b_cnt[button_index] > long_press_threshold THEN                                           
                           B_STATE[button_index]=2
                           abp=1 
                           ELSE
                           IF   b_cnt[button_index] > short_press_threshold THEN  
                           B_STATE[button_index] =1
                           abp=1
                           ENDIF 
                       endif
                       btn_flg.0[button_index]=0   
                  ENDIF
              ENDIF
              endif
        next 
        ADCON0.1 = 1     ' Start conversion  for next pass
    @ INT_RETURN
    Name:  TEST.jpg
Views: 7012
Size:  119.3 KB

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


    Did you find this post helpful? Yes | No

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

    Tried to figure out how your soft work ... But in Proteus nothing happens
    did you try it on a real chip ?
    Warning I'm not a teacher

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

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