Question about Bounce library

Hello,

I am using the Bounce library for a project but it won't work. I have a idea why: I have the button connected to GND (button closed = low) and I'm using the internal pull-up resistors. Is the problem perhaps that the bounce library only works with switches what close to 5V? Here is my code:

#include <Utility.h>
#include <Bounce.h>

const byte keypadPins[] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

#define KEY1 23
#define KEY2 27
#define KEY3 31
#define KEY4 22
#define KEY5 26
#define KEY6 30
#define KEY7 21
#define KEY8 25
#define KEY9 29
#define KEY0 24
#define KEYE 28
#define KEYC 20

#define ENTER 10
#define CLEAR 11
#define NOTHING 13

#define GETKEY1 key1.read()
#define GETKEY2 key2.read()
#define GETKEY3 key3.read()
#define GETKEY4 key4.read()
#define GETKEY5 key5.read()
#define GETKEY6 key6.read()
#define GETKEY7 key7.read()
#define GETKEY8 key8.read()
#define GETKEY9 key9.read()
#define GETKEY0 key0.read()
#define GETKEYE keyE.read()
#define GETKEYC keyC.read()

Bounce key1 = Bounce(KEY1, 5);
Bounce key2 = Bounce(KEY2, 5);
Bounce key3 = Bounce(KEY3, 5);
Bounce key4 = Bounce(KEY4, 5);
Bounce key5 = Bounce(KEY5, 5);
Bounce key6 = Bounce(KEY6, 5);
Bounce key7 = Bounce(KEY7, 5);
Bounce key8 = Bounce(KEY8, 5);
Bounce key9 = Bounce(KEY9, 5);
Bounce key0 = Bounce(KEY0, 5);
Bounce keyE = Bounce(KEYE, 5);
Bounce keyC = Bounce(KEYC, 5); 

void setup() {
  delay(1000);
  Serial.begin(115200);
  delay(1000);
  foreach(keypadPins, 12, pinMode, INPUT); //set the keypad pins to inputs
  foreach(keypadPins, 12, digitalWrite, HIGH); //turn on the internal Pull-Up resistors
}

void updateKeypad(){
  key1.update();
  key2.update();
  key3.update();
  key4.update();
  key5.update();
  key6.update();
  key7.update();
  key8.update();
  key9.update();
  key0.update();
  keyE.update();
  keyC.update();
}

int getInt(){
  updateKeypad();
  if(GETKEY1 == HIGH) return 1;
  if(GETKEY2 == HIGH) return 2;
  if(GETKEY3 == HIGH) return 3;
  if(GETKEY4 == HIGH) return 4;
  if(GETKEY5 == HIGH) return 5;
  if(GETKEY6 == HIGH) return 6;
  if(GETKEY7 == HIGH) return 7;
  if(GETKEY8 == HIGH) return 8;
  if(GETKEY9 == HIGH) return 9;
  if(GETKEY0 == HIGH) return 0;
  if(GETKEYE == HIGH) return ENTER;
  if(GETKEYC == HIGH) return CLEAR;
  return NOTHING;
}

void loop() {
 int value = getInt();
 if(value != NOTHING && value != ENTER && value != CLEAR){
   Serial.print(value);
 }
}

As you may see this code is going to interface to a keypad with 12 keys (0-9, #(enter) and *(clear)) witch has a common connection (not matrix).

Jan

P.S. I get "1" in the serial monitor the whole time, even before I have pressed to first key.

You are using pull-ups, which means when the button is not pressed the Pin will be sitting at 5V. If you press and hold button 1, I would expect you would see 2 in the serial monitor (repeating.)

The library's read() method returns current state of the Pin. I haven't used the library before, but it looks like what you actually want to do is call update() and check to see if it returns TRUE or FALSE. This tells you if the pin's state changed. If the state changed, then you call read() to see what the new value is.

I've updated my code. Now I first get "Ready" then, one second later I get "1", even if I didn't do anything. The I get a 1 every time I press a key.

As you may see I also added some delays around Serial.begin(); because I got some strange figures then starting the Serial Monitor, but that helped to get rid of them.

#include <Utility.h>
#include <Bounce.h>

const byte keypadPins[] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

#define KEY1 23
#define KEY2 27
#define KEY3 31
#define KEY4 22
#define KEY5 26
#define KEY6 30
#define KEY7 21
#define KEY8 25
#define KEY9 29
#define KEY0 24
#define KEYE 28
#define KEYC 20

#define ENTER 10
#define CLEAR 11
#define NOTHING 13

#define GETKEY1 key1.read()
#define GETKEY2 key2.read()
#define GETKEY3 key3.read()
#define GETKEY4 key4.read()
#define GETKEY5 key5.read()
#define GETKEY6 key6.read()
#define GETKEY7 key7.read()
#define GETKEY8 key8.read()
#define GETKEY9 key9.read()
#define GETKEY0 key0.read()
#define GETKEYE keyE.read()
#define GETKEYC keyC.read()

Bounce key1 = Bounce(KEY1, 5);
Bounce key2 = Bounce(KEY2, 5);
Bounce key3 = Bounce(KEY3, 5);
Bounce key4 = Bounce(KEY4, 5);
Bounce key5 = Bounce(KEY5, 5);
Bounce key6 = Bounce(KEY6, 5);
Bounce key7 = Bounce(KEY7, 5);
Bounce key8 = Bounce(KEY8, 5);
Bounce key9 = Bounce(KEY9, 5);
Bounce key0 = Bounce(KEY0, 5);
Bounce keyE = Bounce(KEYE, 5);
Bounce keyC = Bounce(KEYC, 5); 

void setup() {
  delay(1000);
  Serial.begin(9600);
  delay(1000);
  Serial.println("Ready");
  foreach(keypadPins, 12, pinMode, INPUT); //set the keypad pins to inputs
  foreach(keypadPins, 12, digitalWrite, HIGH); //turn on the internal Pull-Up resistors
}

boolean updateKeypad(){
  int state1 = key1.update();
  int state2 = key2.update();
  int state3 = key3.update();
  int state4 = key4.update();
  int state5 = key5.update();
  int state6 = key6.update();
  int state7 = key7.update();
  int state8 = key8.update();
  int state9 = key9.update();
  int state0 = key0.update();
  int stateE = keyE.update();
  int stateC = keyC.update();
  
  if(state1 || state2 || state3 || state4 || state5 || state6 || state7 || state8 || state9 || state0 || stateE || stateC) return true;
  else return false;
}

int getInt(){
  if(!updateKeypad()) return false;
  if(GETKEY1 == HIGH) return 1;
  if(GETKEY2 == HIGH) return 2;
  if(GETKEY3 == HIGH) return 3;
  if(GETKEY4 == HIGH) return 4;
  if(GETKEY5 == HIGH) return 5;
  if(GETKEY6 == HIGH) return 6;
  if(GETKEY7 == HIGH) return 7;
  if(GETKEY8 == HIGH) return 8;
  if(GETKEY9 == HIGH) return 9;
  if(GETKEY0 == HIGH) return 0;
  if(GETKEYE == HIGH) return ENTER;
  if(GETKEYC == HIGH) return CLEAR;
  return NOTHING;
}

void loop() {
 int value = getInt();
 if(value != NOTHING && value != ENTER && value != CLEAR && value == true){
   Serial.print(value);
 }
}

Jan

Again, you only want to call keyX.read() if keyX.update() is true. The first "1" is because the library thinks a toggle occurred from 0 (its initial state) to 1 (the pulled-up value). This could probably be fixed by running keyX.write(1) in setup(), before the first keyX.update() is called.

The 1s from other button presses are because you correctly check if any of the keyX.update()'s were called. However, because key1.read() will always return 1 (unless it is pressed down) any button will appear to return a 1 since you then try to check each key, starting with 1.

You only want to call a key's read() if its update() is TRUE.

You want to be using code more like this:

int updateKeypad(){
  if (key1.update()) {
    if (!key1.read()) {
      return 1;
    }
  }
  if (key2.update()) {
    if (!key2.read()) {
      return 2;
    }
  }
  if (key3.update()) {
    if (!key3.read()) {
      return 3;
    }
  }
  if (key4.update()) {
    if (!key4.read()) {
      return 4;
    }
  }
  if (key5.update()) {
    if (!key5.read()) {
      return 5;
    }
  }
  if (key6.update()) {
    if (!key6.read()) {
      return 6;
    }
  }
  if (key7.update()) {
    if (!key7.read()) {
      return 7;
    }
  }
}

You need to individually check the status and value of each button.

Thank you, now it worked.

Jan