/*
Forum: https://forum.arduino.cc/t/problem-sending-program-change-through-arduino-pro-micro/1405537
Wokwi: https://wokwi.com/projects/441426463323247617
ec2021
2025/09/07
*/
constexpr byte buttonPin {2}; // pushbutton pin
constexpr byte ledPin {10}; // led pin
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP); // Configure button as Input
pinMode(ledPin, OUTPUT);
}
void loop() {
if (buttonPressed()) {
changeLed();
}
}
void changeLed() {
static byte ledState = HIGH;
digitalWrite(ledPin, ledState);
Serial.println((ledState) ? "On" : "Off");
ledState = !ledState;
}
boolean buttonPressed() {
constexpr unsigned long debounceTime {50}; // ms
static unsigned long lastChange = 0;
static byte state = HIGH;
static byte lastState = LOW;
byte actState = digitalRead(buttonPin);
if (actState != lastState) {
lastState = actState;
lastChange = millis();
}
if (state != actState && millis() - lastChange >= debounceTime) {
state = actState;
return !state;
}
return false;
}