const byte button_pin [] = {13, 12, 11, 10}; // buttons are connected to these pins
const byte led_echo_pin [] = { 9, 8, 7, 6}; // likewise for the leds
const byte led_long_pin [] = { 5, 4, 3, 2};
class Button {
const byte _button;
const byte _led_echo;
const byte _led_long;
int _state;
unsigned long _buttonDownMs;
public:
Button(byte index) :
_button ( button_pin [index] ), // Initialization List,
_led_echo( led_echo_pin[index] ), // initialize <_led_echo> to the value of <led_echo_pin[index]>
_led_long( led_long_pin[index] )
{}
void setup() {
pinMode(_button, INPUT_PULLUP);
pinMode(_led_echo, OUTPUT );
pinMode(_led_long, OUTPUT );
_state = HIGH;
}
void loop() {
int prevState = _state;
_state = digitalRead(_button); // check button
if (prevState != _state) { // button state changed
digitalWrite(_led_echo, !_state);
if (_state == LOW) { // button pressed
_buttonDownMs = millis();
}
else {
if (millis() - _buttonDownMs < 100) { // ignore this for debounce
}
else if (millis() - _buttonDownMs < 1000) { // short click
digitalWrite(_led_long, HIGH);
}
else { // long click
digitalWrite(_led_long, LOW);
}
}
}
}
};
Button MyButton [] = {Button(0), Button(1), Button(2), Button(3)};
//Button MyButton[] = {13, 12, 11, 10}; // buttons do not actuate
void setup() {
for (int i = 0; i<4; i++) MyButton[i].setup();
}
void loop() {
for (int i = 0; i<4; i++) MyButton[i].loop();
}