//https://gemini.google.com/app/112f649df421ec58
#include <Adafruit_NeoPixel.h>
#define DIAL_PIN 2
#define LED_PIN 6
#define NUM_LEDS 12
#define PULSE_TIMEOUT 700 // Time to wait to confirm digit is finished
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int pulseCount = 0;
bool lastState = HIGH;
unsigned long lastPulseTime = 0;
bool isDialing = false;
void setup() {
pinMode(DIAL_PIN, INPUT_PULLUP);
strip.begin();
strip.setBrightness(255);
strip.show();
Serial.begin(9600);
}
void loop() {
bool currentState = digitalRead(DIAL_PIN);
// 1. DETECTION OF THE PULSE
if (currentState != lastState) {
if (currentState == LOW) { // The dial circuit just broke/pulsed
// TRIGGER: If this is the start of a brand new number, blank everything first
if (!isDialing) {
strip.clear();
strip.show();
pulseCount = 0;
isDialing = true;
Serial.println("New number started. Clearing display...");
}
pulseCount++;
lastPulseTime = millis();
updateGauge(pulseCount);
}
delay(20); // Debounce
}
lastState = currentState;
// 2. TIMEOUT: The dial has returned to the home position
if (isDialing && (millis() - lastPulseTime) > PULSE_TIMEOUT) {
Serial.print("Final Count for this digit: ");
Serial.println(pulseCount);
// We stop the "isDialing" state so that the NEXT pulse triggers a blank
isDialing = false;
}
}
// 3. COLOR LOGIC (Changes based on how many pulses have arrived)
void updateGauge(int count) {
uint32_t color;
switch (count) {
case 1: color = strip.Color(139, 69, 19); break; // Brown
case 2: color = strip.Color(255, 0, 0); break; // Red
case 3: color = strip.Color(255, 120, 0); break; // Orange
case 4: color = strip.Color(255, 255, 0); break; // Yellow
case 5: color = strip.Color(0, 255, 0); break; // Green
case 6: color = strip.Color(0, 255, 255); break; // Cyan
case 7: color = strip.Color(0, 0, 255); break; // Blue
case 8: color = strip.Color(128, 0, 128); break; // Purple
case 9: color = strip.Color(255, 20, 147); break; // Pink
case 10: color = strip.Color(255, 255, 255);break; // White
default: color = strip.Color(255, 255, 255);break;
}
// We clear and refill inside the gauge function to ensure
// the color change applies to the whole "bar"
strip.clear();
for (int i = 0; i < count; i++) {
if (i < NUM_LEDS) {
strip.setPixelColor(i, color);
}
}
strip.show();
}