My code for ADC button handling, it works, but can it be slimmed down?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    No it does not.
    It does not do "do not repeat action until user releases and pushes key again"

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    Code:
    if !flag then
        adcin 1, adcval   'read keys
        SELECT CASE adcval
          CASE < 10 : left=left+1:flag=1 'detect button presses
          CASE < 100
        @ NOP
          CASE < 130 : right=right+1 :flag=1'detect button presses
        END SELECT
    endif
    Something like that then?

    Ioannis
    Last edited by Ioannis; - 7th January 2022 at 20:40.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    Yes and we again will have extra "left" and "right" variables, one per each button - what I wanted to avoid

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    maybe I am a bit slow.. But how you can have 40 buttons but not 40 variables to distinct between them?

    Ioannis

  5. #5
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    ADCIN X,ADVAL
    IF ADVAL=20 then Gosub Y1
    IF ADVAL=10 then GOSUB Y2
    IF ADVAL=30 then GOSUB Y3

    and so on.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    So at any given moment you will not know which button was hit and will have to scan again.

    But even so, a flag for each button pressed is needed to keep it from regarding it as new press.

    Ioannis

  7. #7
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    Regarding flags, here is a trick I use:
    Code:
    ButtonA VAR WORD
     Button1 VAR ButtonA.0
     Button2 VAR ButtonA.1
     ......
     Button16 VAR ButtonA.15
    This creates bit flags. To use them:
    Code:
    Button4 = PORTC.3

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: My code for ADC button handling, it works, but can it be slimmed down?

    its a fairly simple process , 10 bits per button is easy , 8 bits with a bit of effort
    providing the button check is called in a loop at a relatively consistent rate.
    the program structure matters , spaghetti code won't get good results
    sudo method
    Code:
    b1cnt  var byte
    newb1  var bit 
    b1dun  var bit
    b2cnt  var byte
    newb2  var bit 
    b2dun  var bit
    osc=8
    t1con=$01;32mS
    main
    if pir1.0
    pir1.0=0
    gosub getkey
    if newb1 gosub dob1 
    if newb2 gosub dob2
    endif
    
    
    goto main
    
    
    
    
    getkey:
    read adc
     b1cnt = b1cnt<<1
     b2cnt = b2cnt<<1
    if adc in b1 range 
      b1cnt = b1cnt+1
    elseif if adc in b2 range
      b2cnt = b2cnt+1
    endif
    if !b1dun
      if b1cnt=255 then newb1=1  ;32*8mS
    else
      if b1cnt=0 then b1dun=0
    endif
    
    
    if !b2dun
       if b2cnt=255 then newb2=1
    else
       if b2cnt=0 then b2dun=0
    endif
    
    
    return
    
    
    dob1:
    b1dun=1
    newb1=0
    do b1 stuff
    return
    
    
    dob2:
    b2dun=1
    newb1=0
    do b2 stuff
    return
    Warning I'm not a teacher

Similar Threads

  1. Long / short press button...but reading ADC
    By fratello in forum mel PIC BASIC Pro
    Replies: 37
    Last Post: - 10th July 2016, 08:37
  2. Replies: 42
    Last Post: - 18th March 2012, 23:02
  3. How come my DS18S20 code still works?
    By Wilson in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 30th July 2011, 02:59
  4. 12 bit or higher ADC on a PIC that works with PBP?
    By Brandon in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 11th November 2007, 17:19
  5. ADC -how it works?, pic -
    By matias in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 13th March 2007, 21:54

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