The Shop > Electronics & IC Programing
Button Acceleration
(1/1)
Will_D:
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?
sparky961:
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: ---// 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;
}
}
--- End code ---
Will_D:
Cheers Sparky, yes timing it seems a very good idea.
Thanks again
Will
Navigation
[0] Message Index
Go to full version