// Week 10 — WS2812B (10 LEDs) + push button on XIAO ESP32-C3 // Button: D0 (same net as Week 8 PCB — press = HIGH with pull-down wiring) // NeoPixel data: D1 #include #define PIN_NEO D3 #define PIN_BTN D0 #define NUMPIXELS 10 Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEO, NEO_GRB + NEO_KHZ800); const unsigned long DEBOUNCE_MS = 50; const unsigned long CELEBRATE_MS = 5000; const unsigned long BLINK_MS = 250; int pressCount = 0; int buttonState = LOW; int lastReading = LOW; unsigned long lastDebounceTime = 0; bool celebrating = false; unsigned long celebrateStart = 0; void showProgress(int count) { pixels.clear(); for (int i = 0; i < count; i++) { pixels.setPixelColor(i, pixels.Color(0, 80, 255)); // blue } pixels.show(); } void startCelebration() { celebrating = true; celebrateStart = millis(); } void setup() { pinMode(PIN_BTN, INPUT); pixels.begin(); pixels.setBrightness(80); pixels.clear(); pixels.show(); Serial.begin(115200); Serial.println(F("WS2812B button demo: press to light LEDs one by one.")); } void loop() { if (celebrating) { unsigned long elapsed = millis() - celebrateStart; if (elapsed >= CELEBRATE_MS) { celebrating = false; pressCount = 0; pixels.clear(); pixels.show(); Serial.println(F("Reset — all LEDs off.")); return; } if (((elapsed / BLINK_MS) % 2) == 0) { for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(255, 255, 255)); } } else { pixels.clear(); } pixels.show(); return; } int reading = digitalRead(PIN_BTN); if (reading != lastReading) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > DEBOUNCE_MS) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { pressCount++; Serial.print(F("Press #")); Serial.println(pressCount); if (pressCount >= NUMPIXELS) { showProgress(NUMPIXELS); startCelebration(); } else { showProgress(pressCount); } } } } lastReading = reading; }