#define HEARTBEAT_LED LED_BUILTIN // GP25
#define BLINK_LED 15 // LED
#define BUTTON_PIN 2 // Push-button
#define POT_PIN 26 // ADC0
const unsigned long HEARTBEAT_INTERVAL = 500;
bool blinkEnabled = false; // Toggled by button
bool blinkLedState = false;
bool lastButtonReading = HIGH;// Button debounce
bool lastStableButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long DEBOUNCE_TIME = 30;
unsigned long lastHeartbeatTime = 0;
unsigned long lastBlinkTime = 0;
bool heartbeatState = false;// Current heartbeat state
unsigned long lastPrintTime = 0;
const unsigned long PRINT_INTERVAL = 200;
void setup() {
pinMode(HEARTBEAT_LED, OUTPUT);
pinMode(BLINK_LED, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(POT_PIN, INPUT);
digitalWrite(HEARTBEAT_LED, LOW);
digitalWrite(BLINK_LED, LOW);
Serial1.begin(115200);
Serial1.println("Pico blink + heartbeat + POT speed control started");
}
void loop() {
unsigned long now = millis();
if (now - lastHeartbeatTime >= HEARTBEAT_INTERVAL) {
lastHeartbeatTime = now;
heartbeatState = !heartbeatState;
digitalWrite(HEARTBEAT_LED, heartbeatState ? HIGH : LOW);
}
handleButton(now);
int potRaw = analogRead(POT_PIN); // 0–4095
unsigned long blinkInterval = map(potRaw, 0, 4095, 50, 1000);
if (blinkEnabled) {
if (now - lastBlinkTime >= blinkInterval) {
lastBlinkTime = now;
blinkLedState = !blinkLedState;
digitalWrite(BLINK_LED, blinkLedState ? HIGH : LOW);
}
} else {
digitalWrite(BLINK_LED, LOW);
blinkLedState = false;
}
if (now - lastPrintTime >= PRINT_INTERVAL) {
lastPrintTime = now;
Serial1.print("Button: ");
Serial1.print(blinkEnabled ? "ON" : "OFF");
Serial1.print(" | POT raw: ");
Serial1.print(potRaw);
Serial1.print(" | Blink interval (ms): ");
Serial1.print(blinkInterval);
Serial1.print(" | LED state: ");
Serial1.println(blinkLedState ? "HIGH" : "LOW");
}
}
void handleButton(unsigned long now) { // debounce
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonReading) {
lastDebounceTime = now;
lastButtonReading = reading;
}
if ((now - lastDebounceTime) > DEBOUNCE_TIME) {
if (reading != lastStableButtonState) {
lastStableButtonState = reading;
if (lastStableButtonState == LOW) {
blinkEnabled = !blinkEnabled;
Serial1.print("Button pressed -> Blink ");
Serial1.println(blinkEnabled ? "ENABLED" : "DISABLED");
}
}
}
}Loading
pi-pico-w
pi-pico-w