#include <Arduino.h>
// Define GPIO pins
#define LED_PIN 22 // Built-in LED on the Pico
#define BUTTON_PIN 28 // Button to turn LED ON/OFF
#define BLINK_BUTTON_PIN 16 // Button to start blinking
// Function to blink the LED
void blink_led(uint8_t pin, int count, int delay_ms) {
for (int i = 0; i < count; i++) {
digitalWrite(pin, HIGH); // Turn LED ON
delay(delay_ms);
digitalWrite(pin, LOW); // Turn LED OFF
delay(delay_ms);
}
}
void setup() {
// Initialize GPIO pins
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BLINK_BUTTON_PIN, INPUT_PULLUP);
// Initialize serial communication
Serial.begin(9600);
while (!Serial) {
delay(10);
}
Serial.println("Setup complete");
}
void loop() {
static bool led_on = false;
// Check the button to toggle LED ON/OFF
if (digitalRead(BUTTON_PIN) == LOW) {
led_on = !led_on;
digitalWrite(LED_PIN, led_on);
delay(200); // Debounce delay
}
// Check the button to start blinking
if (led_on && digitalRead(BLINK_BUTTON_PIN) == LOW) {
blink_led(LED_PIN, 5, 200); // Blink 5 times with 200ms delay
digitalWrite(LED_PIN, led_on); // Ensure LED retains its previous state
delay(200); // Debounce delay
}
delay(10); // Small delay to avoid high CPU usage
}