|
Code: --- |
|---|
|
``` class PushButton |
| { |
| public: |
| PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created) |
| : pincolor=#000000[/color] { // remember the push button pin |
| pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor |
| }; |
| bool isPressedcolor=#000000[/color] // read the button state check if the button has been pressed, debounce the button as well |
| { |
| bool pressed = false; |
| bool state = digitalReadcolor=#000000[/color]; // read the button's state |
| int8_t stateChange = state - previousState; // calculate the state change since last time |
if (stateChange == falling) { // If the button is pressed (went from high to low)
if (milliscolor=#000000[/color] - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange == rising) { // if the button is released or bounces
previousBounceTime = milliscolor=#000000[/color]; // remember when this happened
}
previousState = state; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin;
bool previousState = HIGH;
unsigned long previousBounceTime = 0;
const static unsigned long debounceTime = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
const uint8_t ledPins[] = {12, 11, 10, 9};
const uint8_t nb_leds = sizeofcolor=#000000[/color] / sizeofcolor=#000000[/color];
PushButton pushbutton = {2};
void setupcolor=#000000[/color] {
for (const uint8_t &ledPin : ledPins)
pinMode(ledPin, OUTPUT);
}
void loopcolor=#000000[/color] {
static uint8_t nb_presses = 0;
if color=#000000[/color] {
if (nb_presses == nb_leds) {
for (const uint8_t &ledPin : ledPins)
digitalWrite(ledPin, LOW);
nb_presses = 0;
} else {
digitalWrite(ledPins[nb_presses], HIGH);
nb_presses++;
}
}
}
```
|