Can't seem to debounce buttons...

Status
Not open for further replies.

AndreasP

New member
I've been stuck for a day trying to get buttons to debounce properly.
What am I trying to do:
I have a Teensy 3.2 with 2 buttons, 16x2 LCD, and one analog input setup up. The buttons two are called "UP" and "Down". Every time someone presses the UP or DOWN buttons, I need a increment or decrement a variable so it stays between betwen 0 and 255. The variable is written out with analogWrite to control the brightness of the LCD back light. I'm using Bounce2 to de-bounce all of this.
I'm not sure if it makes a difference, but the Teensy 3.2 is externally powered. (not by USB). All the grounds are tied together.
I am also measuring an analoginput with a voltage divider and writing the output to the LCD. That part is working fine. It's just the buttons that refuse to cooperate.
What's the problem:
No matter which button I press, they both trigger. It's like there is only one button. I checked all connections with a meter.. Things are where they are supposed to be. No crossed wires, etc..
The setup:
The DOWN button is a normally open button connected between GND and pin 23.
The UP button is a normally open button is connected between GND and pin 22.
THe PWM output pin is pin 10.
The analog input is A0 (pin 14).


Code:
// include the library code:
#include <LiquidCrystal.h>

//button debouncer
#include <Bounce2.h>

int i = 0;
int lcdbrighness = 128;
int val = 0;

const int ENTERbuttonPin = 19;
const int RIGHTbuttonPin = 20;
const int LEFTbuttonPin = 21;
const int UPbuttonPin = 22;
const int DOWNbuttonPin = 23;

const int RS_Pin = 5;
const int Enable_Pin = 6;
const int D4_Pin = 7;
const int D5_Pin = 8;
const int D6_Pin = 9;
const int D7_Pin = 11;
const int LCDBrightness_Pin = 10;

Bounce UPbutton = Bounce();  
Bounce DOWNbutton = Bounce();  nce

// initialize the library with the numbers of the interface pins
LiquidCrystal   lcd(RS_Pin, Enable_Pin, D4_Pin, D5_Pin, D6_Pin, D7_Pin);

void setup() {
  Serial.begin(9600);
 
  pinMode(UPbuttonPin, INPUT_PULLUP);
  UPbutton.attach(UPbuttonPin);
  UPbutton.interval(50);

  pinMode(DOWNbuttonPin, INPUT_PULLUP);
  DOWNbutton.attach(DOWNbuttonPin);
  DOWNbutton.interval(50);

  pinMode(RS_Pin, OUTPUT);
  pinMode(Enable_Pin, OUTPUT);
  pinMode(D4_Pin, OUTPUT);
  pinMode(D5_Pin, OUTPUT);
  pinMode(D6_Pin, OUTPUT);
  pinMode(D7_Pin, OUTPUT);

  pinMode(LCDBrightness_Pin, OUTPUT);

  analogWrite(LCDBrightness_Pin, lcdbrighness);


  lcd.begin(16, 2);
  lcd.noAutoscroll();
  lcd.clear();
  lcd.print("Setup complete");
  Serial.println ("Setup complete");
  delay(1000);
}

void loop() {
  UPbutton.update();
  DOWNbutton.update();

    if (UPbutton.fell()) {
      Serial.println("UPButtonfallingEdge");
      if (lcdbrighness <= 204) {
        lcdbrighness =  lcdbrighness + 51;
      }
    }
    if (DOWNbutton.fell()) {
      Serial.println("DOWNButtonrisingEdge");
      if (lcdbrighness >= 51) {
        lcdbrighness = lcdbrighness - 51;
      }
    }

  analogWrite(LCDBrightness_Pin, lcdbrighness);

  val = analogRead(0);
  lcd.setCursor(0, 0);
  // print the number of seconds since reset:
  lcd.print("BRIGHTNESS: ");
  lcd.print(lcdbrighness);
  lcd.setCursor(0, 1);
  lcd.print("C: ");
  lcd.print(val);
  lcd.print("  V:");
  lcd.print(val * 0.0209);
}


It all compiles and uploads clean.. When I press one button, I see ""UPButtonfallingEdge" and ""DOWNButtonfallingEdge" in the serial monitor. THe vairable is incremented/decremented in the same loop, so it never changes..

Any hints would be appreciated.
 
Not seeing anything amiss with the code and getting two versions of bounce running, what happens you you run a test program that just prints the button state without the bounce function running?

Also, what does 'nce' do after the down button line? Not something I've seen before so hoping to learn something here
 
What's the problem:
No matter which button I press, they both trigger. It's like there is only one button. I checked all connections with a meter.. Things are where they are supposed to be. No crossed wires, etc..
The setup:
The DOWN button is a normally open button connected between GND and pin 23.
The UP button is a normally open button is connected between GND and pin 22.

I just ran your code on a Teensy 3.2 here (after deleting the extra chars after the 2nd Bounce object). It had nothing else connected, just a Teensy 3.2 and a USB cable.

When I tap a wire between GND and pin 23, I see only "DOWNButtonrisingEdge" in the serial monitor. When I tap the wire to pin 22, I see only "UPButtonfallingEdge".

sc.png

I know you've already checked your hardware, but perhaps try running your code the way I just did, with nothing at all connected to the Teensy and use a wire to (carefully) touch between GND and pins 22 or 23.

It's most certainly a hardware problem. Hopefully this can help you focus your efforts on finding it.
 
Status
Not open for further replies.
Back
Top