// Define the pin numbers for the LED and button #define led_pin 0 #define button_pin 7 // Initial setup void setup() { pinMode(led_pin, OUTPUT); // Set the LED pin as an output pinMode(button_pin, INPUT_PULLUP); // Set the button pin as an input with an internal pull-up resistor Serial.begin(115200); // Start serial communication (UART1) at 115200 baud Serial.setTimeout(10); // Set serial read timeout to 10ms } // Flag to track button state (true = not pressed) bool button_up = true; // Main loop void loop() { // Handle serial input if (Serial.available()) { // Check if there is incoming serial data digitalWrite(led_pin, HIGH); // Turn on the LED String s = Serial.readString(); // Read the received string Serial.print("you typed: "); // Send "you typed: " to serial Serial.println(s); // Echo back the input string delay(1000); // Keep the LED on for 1 second digitalWrite(led_pin, LOW); // Turn off the LED } // Handle button press if ((digitalRead(button_pin) == LOW) && button_up) { // If the button is pressed and was previously released digitalWrite(led_pin, HIGH); // Turn on the LED Serial.println("button down"); // Send "button down" to serial button_up = false; // Update button state to pressed (false) } // Handle button release else if ((digitalRead(button_pin) == HIGH) && !button_up) { // If the button is released and was previously pressed digitalWrite(led_pin, LOW); // Turn off the LED Serial.println("button up"); // Send "button up" to serial button_up = true; // Update button state to released (true) } }