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();
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
led-red:A
led-red:C
led-green:A
led-green:C
led-blue:A
led-blue:C
led-yellow:A
led-yellow:C
btn-red:1.l
btn-red:2.l
btn-red:1.r
btn-red:2.r
btn-green:1.l
btn-green:2.l
btn-green:1.r
btn-green:2.r
btn-blue:1.l
btn-blue:2.l
btn-blue:1.r
btn-blue:2.r
btn-yellow:1.l
btn-yellow:2.l
btn-yellow:1.r
btn-yellow:2.r
led-red1:A
led-red1:C
led-green1:A
led-green1:C
led-blue1:A
led-blue1:C
led-yellow1:A
led-yellow1:C