// // Seeed XIAO RP2040 button, blink, echo hello-world // // based on code by Neil Gershenfeld // //#define allows the programmer to give a name to a constant value before the program is compiled #define B_button_pin D1 #define B_led_pin D9 #define R_button_pin D2 #define R_led_pin D7 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pins for LEDS as an output and buttons as INPUTS pinMode(B_led_pin, OUTPUT); pinMode(B_button_pin,INPUT_PULLUP); pinMode(R_led_pin, OUTPUT); pinMode(R_button_pin,INPUT_PULLUP); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps //Serial.setTimeout(10); //sets the maximum milliseconds to wait for serial data Serial.println("Input B to turn blue LED and R to turn red LED"); } bool B_button_up =true; //bool holds one of two values, true or false bool R_button_up =true; // the loop function runs over and over again forever void loop() { if (Serial.available()) { char comdata = char(Serial.read()); if (comdata == 'B') { digitalWrite(B_led_pin,HIGH); Serial.print("you typed:"); Serial.println(comdata); delay(500); digitalWrite(B_led_pin,LOW); } else if (comdata == 'R') { digitalWrite(R_led_pin,HIGH); Serial.print("you typed:"); Serial.println(comdata); delay(500); digitalWrite(R_led_pin,LOW); } } // Check Blue Button if ((digitalRead(B_button_pin) == LOW) && B_button_up) { digitalWrite(B_led_pin,HIGH); Serial.println("blue button down"); B_button_up = false; } if ((digitalRead(B_button_pin) == HIGH) && !B_button_up) { digitalWrite(B_led_pin,LOW); Serial.println("blue button up"); B_button_up = true; } // Check Red Button if ((digitalRead(R_button_pin) == LOW) && R_button_up) { digitalWrite(R_led_pin,HIGH); Serial.println("red button down"); R_button_up = false; } if ((digitalRead(R_button_pin) == HIGH) && !R_button_up) { digitalWrite(R_led_pin,LOW); Serial.println("red button up"); R_button_up = true; } delay(50); }