#define LED_PIN D6
#define BUTTON_PIN D2
bool ledState = LOW;
int buttonReading;
int lastButtonReading = HIGH;
int stableButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(LED_PIN, LOW);
}
void loop()
{
buttonReading = digitalRead(BUTTON_PIN);
if (buttonReading != lastButtonReading)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (buttonReading != stableButtonState)
{
stableButtonState = buttonReading;
if (stableButtonState == LOW)
{
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
}
}
lastButtonReading = buttonReading;
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6