u/PrimeEmre

My local AI assistant shows what it's doing with 2 LEDs and a servo (one-byte serial protocol, no delay())

Small side project: I run a local AI assistant on my PC (Python + Ollama) and wanted to see when it's busy without watching a terminal. So the Python side sends a single character over USB serial whenever its state changes, and an Uno shows it:

  • B researching: blue LED pulses, servo sweeps back and forth
  • Y debating: yellow LED pulses faster, servo keeps sweeping
  • W done: LEDs off, servo parks at 90°
  • 0 idle

The sketch never uses delay(). The LED pulse and the servo sweep both run off millis(), so the board keeps reading serial while it animates and a new state takes effect right away:

void pulse(int pin, unsigned long intervalMs) {
  unsigned long now = millis();
  if (now - lastLedToggle >= intervalMs) {
    pulseOn = !pulseOn;
    lastLedToggle = now;
  }
  digitalWrite(pin, pulseOn ? HIGH : LOW);
}

Wiring is simple: blue LED on pin 12, yellow on pin 13 (220-330Ω each), servo signal on pin 8.

Two gotchas worth knowing:

  • An SG90 off the Uno's 5V pin is fine, but anything bigger can brown out the board mid-sweep (LEDs flicker, board resets). Give it its own 5V supply with a shared ground.
  • On the Python side, pip install serial is not pyserial. If you get module 'serial' has no attribute 'Serial', uninstall both and reinstall only pyserial.

Sketch and Python code: https://github.com/PrimeEmre/Jarvis-Hardware-Program--local

Write-up with the full wiring steps: https://blog.emreguzel.ca/2026/07/19/architecting-autonomous-ai-agents-with-hardware-integration/

If anyone has a better idea for a "busy" indicator than a sweeping servo, I'm all ears.

reddit.com
u/PrimeEmre — 11 days ago

Why your C program loops forever when you type a letter, and how to read input safely

A question that comes up a lot: "I used scanf("%d", &x) and when I type abc my program spins forever."

Why it happens: when scanf can't parse the input as a number, it doesn't consume it. abc stays in the input buffer, the next scanf fails on the same characters, and so on forever.

The fix: check the return value (the number of items read successfully), and throw away the bad line when it fails:

int age;
printf("Enter your age: ");
while (scanf("%d", &age) != 1) {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) { }  /* discard the bad input */
    if (c == EOF) return 1;
    printf("That's not a number. Try again: ");
}

A few other input traps worth knowing:

  • Strings with spaces: scanf("%s") stops at the first space. Use fgets(buf, sizeof buf, stdin), which also can't overflow the buffer.
  • fgets keeps the newline: strip it with buf[strcspn(buf, "\n")] = '\0';
  • Never use gets(): it was removed in C11 because it has no length limit.
  • Missing &: scanf("%d", age) passes a value where an address is expected, which is undefined behavior and usually a crash.

I wrote this up in more detail for beginners, with a worked example and a section on using the input with if/else (including the classic if (x = 5) bug): https://blog.emreguzel.ca/2026/09/29/user-input-in-c-a-complete-guide-for-beginners/

u/PrimeEmre — 12 days ago