// a Simple Button class
// brought the "debounce" example towards OOP
// brought the "blink without delay " example to a OOP
// https://forum.arduino.cc/t/kurzen-taster-impuls-mit-moba-tools-erzeugen/1126782/3
// https://forum.arduino.cc/t/taster-interrupts-durch-pwm-gestort/1184843/18
// 2023-05-14 by noiasca
/* ******************************************************************************************** */
/* die zwei Klassen können auch in einen separaten tab "klassen" der Arduino IDE kopiert werden */
class Button { // a simple class for buttons based on the "Debounce" example
const uint8_t buttonPin; // the GPIO / pin for the button
static constexpr byte debounceDelay = 30; // the debounce time; Static because we only need one value for all buttons
const bool active; // is the pin active HIGH or active LOW (will also activate the pullups!)
bool lastButtonState = HIGH; // the previous reading from the input pin
uint32_t lastDebounceTime = 0; // the last time the output pin was toggled
public:
/**
\brief constructor for a button
The constructor takes the GPIO as parameter.
If you omit the second parameter, the library will activate the internal pullup resistor
and the button should connect to GND.
If you set the second parameter to HIGH, the button is active HIGH.
The button should connect to VCC.
The internal pullups will not be used but you will need an external pulldown resistor.
\param buttonPin the GPIO for the button
\param active LOW (default) - if button connects to GND, HIGH if button connects to VCC
*/
Button(uint8_t buttonPin, bool active = LOW) : buttonPin(buttonPin), active(active) {}
/**
\brief set the pin to the proper state
Call this function in your setup().
The pinMode will be set according to your constructor.
*/
void begin() {
if (active == LOW)
pinMode(buttonPin, INPUT_PULLUP);
else
pinMode(buttonPin, INPUT);
}
/**
\brief indicate if button was pressed since last call
@return HIGH if button was pressed since last call - debounce
*/
bool wasPressed() {
bool buttonState = LOW; // the current reading from the input pin
byte reading = LOW; // "translated" state of button LOW = released, HIGH = pressed, despite the electrical state of the input pint
if (digitalRead(buttonPin) == active) reading = HIGH; // if we are using INPUT_PULLUP we are checking invers to LOW Pin
if ((millis() - lastDebounceTime) > debounceDelay) { // If the switch changed, AFTER any pressing or noise
if (reading != lastButtonState && lastButtonState == LOW) { // If there was a change and and last state was LOW (= released)
buttonState = HIGH;
}
lastDebounceTime = millis();
lastButtonState = reading;
}
return buttonState;
}
};
class Pulse { // a simple class for buttons based on the "Debounce" example
const uint8_t pin; // the GPIO / pin for the button
const uint16_t interval = 500;
uint32_t previousMillis = 0; // the last time the output pin was toggled - we check only ONE byte, so I didn't mess around with unsigned long
uint8_t state = 0;
public:
Pulse(byte pin, uint16_t interval = 500) : pin(pin), interval(interval) {}
void begin() {
pinMode(pin, OUTPUT);
}
void on() {
digitalWrite(pin, HIGH);
previousMillis = millis();
state = 1;
}
bool update() {
if (state && (millis() - previousMillis > interval)) {
digitalWrite(pin, LOW);
state = 0;
}
return state;
}
};
/* ende separater Tab */
/* ****************** */
Button buttonA{A0}; // einen Button anlegen
Pulse pulseA{13, 500}; // ein monoflop Ausgang anlegen
void setup() {
Serial.begin(115200);
buttonA.begin();
pulseA.begin();
}
void loop() {
if (buttonA.wasPressed()) {
Serial.println(F("ButtonA"));
pulseA.on(); // Pin Zeit starten
}
pulseA.update(); // muss im loop aufgerufen damit der Pin abschalten kann
}