//XIAO RP2040 // LED State Machine // Press button to toggle LED state // LED @D8 (1 ON, 0 OFF) // PB @D9 (1 OPEN, 0 CLOSED) // // constants won't change. They're used here to set pin numbers: const int buttonPin = D9; // the number of the pushbutton pin const int ledPin = D8; // the number of the LED pin int ledState = 0; // start with void setup() { // set pin directions pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); // use INPUT_PULLUP or INPUT_PULLDOWN if using internal resistor } void loop() { // wait for keypress buttonState = digitalRead(buttonPin); if (digitalRead(buttonPin) == 0) { //buttonpin pressed delay(15); //switch debounce while (digitalRead (buttonPin) == 0); //wait for buttonPin realease if (ledState == 0) // current state 0 ledState = 1; // update state else ledState = 0; //ledState = 1, reset to 0 } // Update LED depending on ledState if (ledState == 0) digitalWrite(ledPin, HIGH); // turn off LED else if (ledState == 1) digitalWrite(ledPin, LOW); // turn off LED }