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

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

    Hello,
    This is my code for handling two buttons connected to ADC input.
    It works fine, and what was most important, it prevents repeated key action -
    user has to release the button and press it again, to call needed action again.
    However, there are quite a lot of variables, and their number will double with
    more buttons being added.
    So maybe there is a way to make it simpler, while still maintaining
    "do not repeat action" functionality?

    Code:
    menu: 'main loop for button handling
    adcin 1, adcval   'read keys
    if adcval=255 then n1=0: n2=0: left=0: right=0  'if no key, then reset both button variables
    if adcval<10 then left=left+1 'detect button presses
    if adcval>100 and adcval <130 then right=right+1 'detect button presses
    if left>100 and n1=0 then 'if button pressed enough and it was not pressed before
    left=0
    n1=1
    gosub action1 'button 1 tasks
    endif
    if right>100 and n2=0 then 'if button pressed enough and it was not pressed before
    right=0
    n2=1
    gosub action2 'button 2 tasks
    endif  
    pause 1 'input delay
    goto menu

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

    Code:
    adcin 1, adcval   'read keys
    SELECT CASE adcval
      CASE < 10 : left=left+1 'detect button presses
      CASE < 100
    @ NOP
      CASE < 130 : right=right+1 'detect button presses
    END SELECT
    You might try the SELECT CASE method of parsing through. The above snippet should give you an idea.[/FONT]

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


    Did you find this post helpful? Yes | No

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

    Thanks!
    My main concern is number of variables needed...

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


    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 snippets do not provide any where near the required info to provide a meaningful answer, not even one var definition is even provided

    for a meaningful answer
    1 how often is routine called
    2 is it called on a regular timed basis
    3 how responsive does it need to be

    the minimum key data necessary would be a count to detect a debounced press a bit to indicate a new press detected and a bit to indicate
    the press has been acknowledged and acted on provided the right conditions exist

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


    Did you find this post helpful? Yes | No

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

    1. At least 10 times per second.
    2. Yes
    3. As responsive as any other device, where user presses key and expects immediate feedback.

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


    Did you find this post helpful? Yes | No

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

    1. At least 10 times per second
    if right>100
    so button has to held for ten seconds till you get a response ?

    As responsive as any other device
    not in my book, i doubt the thing is working after 300mS

    2. Yes
    like how regular how often really
    Warning I'm not a teacher

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

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

    Why not use the genuine BUTTON command ?????????????????

    of course, needs some "brainwork" and practise - and of course at least " read that f...... manual " ... "

    but the result is worth the effort ...

    " Just my two cents " ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

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

    Read 1st post 1st

    This is ADC input.

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

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

    Quote Originally Posted by CuriousOne View Post
    Read 1st post 1st

    This is ADC input.
    ADC for ONLY two buttons and " immediate response " ( sic ) ??? ...

    if it's your choice ... go on ! ( ) ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

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

    adc key reading is a good tried and tested technique and can be very responsive, the question here is can it be done
    using less memory. the answer is yes of course, if done in a methodical way.
    c1 has opted not to discuss or elaborate on his implementation in any meaningful way. end of story
    Warning I'm not a teacher

  11. #11
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

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

    This is just an example for 2 buttons
    for 20 buttons there should be 40 variables
    I wanted to avoid that
    this is why I asked

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