QRD: Quantum Rotary Dial, Part 3

Arduino Nano programming

Guidomax wrote this sketch for the Arduino in his instructables: https://www.instructables.com/Interface-a-rotary-phone-dial-to-an-Arduino/

int needToPrint = 0;
int count;
int in = 2;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;

// constants

int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;

void setup()
{
Serial.begin(9600);
pinMode(in, INPUT);
}

void loop()
{
int reading = digitalRead(in);

if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (needToPrint) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
Serial.print(count % 10, DEC);
needToPrint = 0;
count = 0;
cleared = 0;
}
}

if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
needToPrint = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
}

It worked perfectly for me. I ran this command from the terminal to confirm the Arduino was sending signals.

screen /dev/cu.usbserial-AM00GQIK 9600

You will probably have to change the serial port (/dev/cu.usb…) but the baud rate (9600) should be good.

Once everything was wired up and the Arduino was programmed, I was ready to move onto the python script! This script would listen to the serial port and react to the dialed in numbers.

Python programming

This was the bulk of work I needed to do. I didn’t want a rotary dial which only set the volume. I wanted it to be a working number pad. I wanted to be able to type with it. I wanted it to trigger Alfred workflows. I wanted it to type common phrases I use a lot (not passwords!)

Since the script would interpret the rotary dial’s input differently depending on the mode it was in, I also wanted this script to provide some basic UI to let the user know the active mode. The rotary phone didn’t have any lights (and I didn’t want to add any) so I opted to create a little statusbar app.

The code is available here: https://github.com/Nathancooke7/quantum_rotary_dial

The README covers how to use the dial and the various features I’ve implemented.

Conclusions

This was a really fun project! I got to work on my soldering skills a bit. I learned about programming Arduinos. I learned the basics of GUI.

I’m using the rotary phone to trigger Alfred workflows which message a Slack channel to let people know I’ve signed in/out of work or gone on my lunch break. I literally dial in every morning.

I hope I’ve inspired you to pursue a side project, especially this specific one. I’d love to hear how people are using QRD to improve their lives.