Hello. I've been working on a project and created a button object class to read the button inputs I need for the project and learn how to make a object class. Everything is working the way I want it to for a single instance of the class but as soon as I add two more for the other buttons it stops working. It's like the instances are not unique but I'm not sure what I'm missing or if I'm describing this issue right.
My sketch, header, and source files are below. I've been going over and over this and am not sure what I'm missing. I've looked at lots of examples and still am not seeing it. I appreciate any help or guidance.
Sketch:
#include <Arduino.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <Button.h>
// Configure input button pins
const byte BTN_ENTER = 25;
const byte BTN_INC = 26;
const byte BTN_DEC = 27;
Button btnEnter(BTN_ENTER, buttonEnterDelay);
Button btnInc(BTN_INC, buttonEnterDelay);
Button btnDec(BTN_DEC, buttonEnterDelay);
void setup() {
Serial.begin(115200); // Initialize serial
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
lcd.clear(); // Clear LCD
}
void loop() {
btnEnter.Pressed(); // Read enter button
btnInc.Pressed(); // Read increase button
btnDec.Pressed(); // Read decrease button
updateDisplay = false;
}
Header:
#ifndef Button_h
#define Button_h
#include <Arduino.h>
/*
* @breif Mutli-function and debounce button object
*/
class Button {
private:
byte _pin;
u_int16_t _LONG_PRESS_MS; // Long press delay Ms
u_int32_t _btnReadPrevMs = 0; // Previous button read Ms
u_int32_t _pressedPrevMs = 0; // Previous button pressed Ms
u_int32_t _currMs;
public:
Button(byte pin, u_int16_t LONG_PRESS_MS);
boolean isPressed = false; // Button is currently pressed
boolean isLongPress = false; // Button is current pressed for > long time
boolean longPress = false; // Button was released - Press was short
boolean shortPress = false; // Button was released - Press was long
u_int8_t btnState = 0; // Button state
void Pressed(); // Button function
void ResetPressed(); // Reset button press triggers
};
#endif
Source File:
#ifndef Button_h
#define Button_h
#include <Arduino.h>
/*
* @breif Mutli-function and debounce button object
*/
class Button {
private:
byte _pin;
u_int16_t _LONG_PRESS_MS; // Long press delay Ms
u_int32_t _btnReadPrevMs = 0; // Previous button read Ms
u_int32_t _pressedPrevMs = 0; // Previous button pressed Ms
u_int32_t _currMs;
public:
Button(byte pin, u_int16_t LONG_PRESS_MS);
boolean isPressed = false; // Button is currently pressed
boolean isLongPress = false; // Button is current pressed for > long time
boolean longPress = false; // Button was released - Press was short
boolean shortPress = false; // Button was released - Press was long
u_int8_t btnState = 0; // Button state
void Pressed(); // Button function
void ResetPressed(); // Reset button press triggers
};
#endif