ArduinoArduino UnoExact chip

Rotary dial

An old phone’s dial: each digit counted from its pulses, a number built up on the serial monitor.

Rotary dial in Argynix Lab

The code

sketch.inoC++
// A rotary dial, as an old phone has it. Click a digit on the dial while it runs.
// DIAL closes to GND while the dial is away from rest; PULSE, closed at rest, opens
// once for each count as it runs back (ten for 0). With the pull-ups on: DIAL reads
// LOW while dialing, and PULSE goes HIGH once for each count.
const int DIAL = 2;
const int PULSE = 3;
String number = "";

void setup() {
  Serial.begin(9600);
  pinMode(DIAL, INPUT_PULLUP);
  pinMode(PULSE, INPUT_PULLUP);
  Serial.println("Dial a number.");
}

void loop() {
  if (digitalRead(DIAL) == HIGH) return;
  int count = 0;
  int last = digitalRead(PULSE);
  while (digitalRead(DIAL) == LOW) {
    int now = digitalRead(PULSE);
    if (now == HIGH && last == LOW) count++;
    last = now;
    delay(5); // the contacts bounce less than this
  }
  int digit = count == 10 ? 0 : count;
  number += String(digit);
  Serial.print("Dialed ");
  Serial.print(digit);
  Serial.print(", the number so far: ");
  Serial.println(number);
}