Number input with 2 or 3 buttons

Status
Not open for further replies.

DaQue

Well-known member
I am looking for a sample implementation of how to enter a number with 2 or 3 buttons. The kind I'm looking for has up/down buttons and could have an enter button. If you hold down an up/down button for a while it starts counting in 10's then 100, then 1000's etc. I'm not sure if there is a name for that but its used a lot of places, for example on various soldering irons I have used to set the temperature . What is the proper name or terms to search for or a pointer to an example would be nice. The OLED feather has 3 buttons on it and would be perfect with the teensy feather adaptor for some projects I have in mind.
 
This sort of thing is pretty simple to build with just reading the pins, using elapseMillis or similar techniques to know when enough time has elapsed, and a bit of code to do the actions (and somehow display the number).

It might look something similar to this...

Code:
static int number = 0;
static elapsedMillis msec = 0;

if (digitalRead(2) == HIGH) {
  // button not pressed
  msec = 0;
} else {
  // button pressed
  if (msec > 500) {
    // every half second of holding the button, increase by 10
    msec = msec - 500;
    number = number + 10;
    lcd.print(number);
  }
}

If you also wish to detect the change of buttons, best to use the Bounce library to automatically deal with mechanical chatter in the switches.
 
Thanks Paul. I was pretty sure I could make it and also sure it's been done a million times before so why not ask if there was a library for it. I'm grateful for the response and sorry it took you away from more important issues for even a moment. The stuff you do is awesome. If it's it worth anything at all I'm up to owning 3 3.2s and a 3.6. One more is coming from Osh park and I hope will be purple.
 
Here is a stripped down version of what I came up with just to show how I did it. This might not compile but gives the basic flow I used and the full version works like I want it to. It is not complete code as I ripped out all the OLED stuff and what I do with the number. I know its a beginner program and my code shows I'm not up to speed yet, but If anyone wants the full version I will recreate it with all of Adafuit's comments and the OLED code still in place.
Code:
#include <Bounce.h>

#define BUTTON_A 4
#define BUTTON_B 3
#define BUTTON_C 8

#define MyMin 1
#define MyMax 9999
#define MyMaxInc 1000

Bounce bouncer_A = Bounce( BUTTON_A, 5 );
Bounce bouncer_B = Bounce( BUTTON_B, 5 );
Bounce bouncer_C = Bounce( BUTTON_C, 5 );


static int value = 0;
static int number = 900;
static int increment_A = 1;
static int decrement_B = 1;
static elapsedMillis msec_A_held  = 0;
static elapsedMillis msec_B_held  = 0;
static elapsedMillis msec_C_held  = 0;
static elapsedMillis msec_A_held2  = 0;
static elapsedMillis msec_B_held2 = 0;
static bool RunStop = 0;
void setup()   {

  // SET UP HOW YOU WILL SEE NUMBERS CHANGE HERE

  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);

}
 
void loop() {
// output here so you can see number change

  bouncer_A .update( );
  value = bouncer_A.read();
  if ( value == LOW) {
    if (msec_A_held >= 150) {
      number = number + increment_A;
     msec_A_held = 0;
      if (number > MyMax ) number = MyMax ;
    }
    if (msec_A_held2 >= 1501) {
      increment_A = increment_A * 10;
      msec_A_held2 = 0;
      if (increment_A > MyMaxInc) increment_A = MyMaxInc;
    }
  }

  else {
    msec_A_held  = 0;
    msec_A_held2  = 0;
    increment_A = 1;
  }

  bouncer_B .update( );
  value = bouncer_B.read();
  if ( value == LOW) {
    if (msec_B_held >= 150) {
      number = number - decrement_B;
      msec_B_held = 0;
      if (number < MyMin) number = MyMin;
    }
    if (msec_B_held2 >= 1501) {
      decrement_B = decrement_B * 10;
      msec_B_held2 = 0;
      if (decrement_B > MyMaxInc) decrement_B = MyMaxInc;
    }
  }

  else {
    msec_B_held  = 0;
    msec_B_held2  = 0;
    decrement_B = 1;
  }

  bouncer_C .update ( );
  value = bouncer_C.read();

  if ( value == LOW) {
    if (msec_C_held >= 100) RunStop = false;
    if (msec_C_held >= 2000) RunStop = true;
  }
  else {
    msec_C_held  = 0;

  }
// do something using number or set state so it runs


  if (RunStop == true){
 // stop something or set state so nothing else updates number 
  }
  else {
  // start it backup 
  }

  
}
 
Status
Not open for further replies.
Back
Top