class PushButton {
private:
int pin;
bool state;
void (*action)();
public:
PushButton(int pin, void (*action)()) {
this->pin = pin;
this->action = action;
pinMode(pin, INPUT);
state = digitalRead(pin);
}
void update() {
bool newState = digitalRead(pin);
if (newState != state) {
state = newState;
if (state == HIGH) {
action();
}
}
}
};
void say_hello() {
Serial.println("Hello!");
}
PushButton button(2, say_hello);
void setup() {
Serial.begin(9600);
}
void loop() {
button.update();
delay(10); // Jeda untuk stabilitas pembacaan tombol
}