Author Topic: Button Acceleration  (Read 3552 times)

Offline Will_D

  • Hero Member
  • *****
  • Posts: 668
  • Country: ie
    • National Homebrew Club of Ireland
Button Acceleration
« on: March 16, 2015, 05:46:26 PM »
I am stumped by this bit of code design for an Arduino project:

I want the user to be able to set a large number ( say 0 to 1000 ).

The user interface uses two buttons "Up" and "Down" and a third to confirm the selection.

In order to allow large numbers the buttons need to modify the step size as they are held down.

So initially they step +/- 1
Hold down for (say 10 increments) the step size jumps to 10
Likewise over a hundred step size jumps to 100

On release the step size resets to 1.

Has any one got a code fragment or is their a library with this functionality?
Engineer and Chemist to the NHC.ie
http://www.nationalhomebrewclub.ie/forum/

Offline sparky961

  • Hero Member
  • *****
  • Posts: 844
  • Country: ca
Re: Button Acceleration
« Reply #1 on: March 17, 2015, 09:27:43 PM »
Here's an off-the-cuff attempt.... although it may not be perfect, it might be enough of a clue for you to run with.  Note: interpret this as pseudo-code.... probably looks a lot like C# since that's what I've been using most recently.

This works on the amount of time the button has been held, but you can apply the same logic based on the number of increments.  Myself, I'd find a time-based holding of the button to be more logical from a user interface perspective.

Code: [Select]
// globals
int increment;
int direction;
int setting = 0;
timer buttonTimer; // milliseconds timer

ButtonInterruptHandler()
{
    if (button == DOWNBUTTON)
        direction = -1; // counting down
    else if (button == UPBUTTON)
        direction = 1; // counting up

    buttonTimer.start; // start counting how long the button has been held
}

TimerTickInterruptHandler() // Note, this would need to be scaled down a bit using a counter to slow how often the setpoint is increased.  Otherwise it would go so fast you wouldn't even see it
{
    if (button == down) { // check that button is still down
        if (timer.value >= 1000) // button has been held for more than a second
        {
            if (setting >= 1000) // set point is over 1000
            {
                increment = 100; // ... so we're stepping up/down by 100
            }
            else if (setting >= 100) // set point is over 100
            {
                increment = 10; // ... so we're stepping up/down by 10
            }
            else // set point is _under_ 100
            {
                increment = 1; // ... so we're stepping up/down by 1
            }
        }
        else // button has not been held for more than a second
        {
           increment = 1; // ... so we're stepping up/down by 1
        }

        setting = setting + (increment * direction);

    }
    else // when we see the button back up, reset everything
    {
        buttonTimer.stop;
        buttonTimer.reset;
    }
}
« Last Edit: March 17, 2015, 10:19:21 PM by sparky961 »

Offline Will_D

  • Hero Member
  • *****
  • Posts: 668
  • Country: ie
    • National Homebrew Club of Ireland
Re: Button Acceleration
« Reply #2 on: March 18, 2015, 05:21:15 AM »
Cheers Sparky, yes timing it seems a very good idea.

Thanks again

Will
Engineer and Chemist to the NHC.ie
http://www.nationalhomebrewclub.ie/forum/