Skip to content

Instantly share code, notes, and snippets.

@lizzybrooks
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lizzybrooks/b88531b906860d4fc4af to your computer and use it in GitHub Desktop.
Save lizzybrooks/b88531b906860d4fc4af to your computer and use it in GitHub Desktop.
/* Basic Digital Read and Rotary Phone Dial Reader
* ------------------
* This code reads whether the phone is on the hook by treating that hook like a button that is either open or depressed
AND it reads out the number dialed on a rotary phone dial by counting the pulses made by the spinning dial wheel.
*/
#define INITPIN 12
#define NUMPIN 11
int counter; // holds the pulse count for each dial spin
int currentValue = 0;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 5; // the debounce time; increase if the output flickers
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup(){
Serial.begin(9600);
//configure the dial reading inputs
pinMode(INITPIN, INPUT_PULLUP);
pinMode(NUMPIN, INPUT_PULLUP);
pinMode(inPin, INPUT); // declare phone hook as input
}
void loop(){
do { stopOperator(); } //when the phone is on the hook, don't play the operator's voice (or the dial tone)
while (digitalRead(inPin) == HIGH ); // wait for phone to be picked up for the first time
delay(100); // wait 100 milliseconds
Serial.println("play operator"); // tell processing to turn on operator voice
do { if (digitalRead (INITPIN) == LOW) { //if the wheel starts turning
stopOperator(); //stop the operator voice
readNumber(); //read and react to the numbers
}
} while (digitalRead(inPin) == LOW); //while the phone is off the hook
delay(100); // chill for a sec
}
void stopOperator(){
Serial.println("stop operator");
}
void endRecording(){
Serial.println("stop recording");
}
void readNumber(){
int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
static int lastValue = HIGH; // holds the last read from the pulse pin.
if (initRead == LOW) { // If the wheel is turning....
int newValue = digitalRead (NUMPIN); // check the pulse pin.
if (newValue != lastValue) { // if it's CHANGED from the last read...
lastDebounceTime = millis(); // save its clock cycle; we need to check it.
}
// If enough time has passed (aka, it's not just a "bounce" from the
// analog signal)...
if ((millis() - lastDebounceTime) > debounceDelay) {
// and if the current value is DIFFERENT than the one you just read...
if (currentValue != newValue) {
currentValue = newValue; // make them the same.
if (newValue == 1) { // If you just set it to a 1...
counter++; // it just finished a pulse, so add one to the counter.
}
}
}
lastValue = newValue; // Your new value becomes the old one for comparison.
} else {
// once the dial returns home and switches the initializing pin OFF,
// you can be sure that there will be no more pulses coming.
// "Counter" is the number dialed. You may now use it for whatever you want.
// This is adjusted for the special case of "0" actually being 10 pulses.
if (counter > 0 && val!=HIGH) {
if (counter == 10) {
Serial.println (0);
}
else if(counter == 6) { // if the number is six
Serial.println("play response"); // play something
}
else if (counter == 7) { // if the number is seven
Serial.println("record"); //record
}
else {
Serial.println (counter);
}
// After you're done using it, reset counter so it can start again on the
// next dial.
counter = 0;
}
}
}
/*Basic rotary phone recorder and playback machine. For use with Arduino and a phone.
*--------------------------
*This code reads strings from Arduino and then uses the Minim player to record and playback a series of audio files
*/
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player;
AudioPlayer player2;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup() {
minim = new Minim(this);
in = minim.getLineIn();
recorder = minim.createRecorder(in, "myrecording.wav"); //record audio to the file myrecording.wav in the sketch root folder
player = minim.loadFile("operator.aif"); // load the operator track
player2 = minim.loadFile("myrecording.wav"); //load the recorded file for playback (this file should exist in the folder before you open the sketch for the first time. It will be written over each time)
String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
background(0);
readSerial(); //read the data coming from arduino
if (val.equals("play operator")){ //if you get the string for operator, play that file
player.play();
println("playing operator");
}
if (val.equals("stop operator")){ //if you got the sign to shut up, do so:
player.pause(); //silence the operator
player.rewind(); //go back to the beginning of the song
player2.pause(); // silence the recorded file
player2.rewind(); // rewind the recorded file
if ( recorder.isRecording() ) // if the recorder is still going, close it and save the file
{
recorder.endRecord();
recorder.save();
}
println("shhhh");
}
if (val.equals("play response")){ //if you get the sign to play the recorded file, do so
player2.play();
println("playing recorded message");
}
if (val.equals("record")){ //if you get the sign to record, do so
println("recording message");
recorder.beginRecord();
}
}
void readSerial(){
do{
val = myPort.readStringUntil('\n'); // read incoming data and store it in val
} while (val == null);
val=trim(val);
}
@lizzybrooks
Copy link
Author

This program uses Arduino and Processing (Java) to interface with a rotary phone. The phone plays an operator's voice (audio file 1) when it comes off the hook. It stops playing that file when the phone begins dialing. If the user dials 7, she can record a message which will be stored in the program. The program records until the phone is replaced on the hook. Another user can pick up the phone and dial 6 to hear the previous user's message. Wow! So cool!

There are a few more steps that need to happen now:

  1. The recording function should create multiple files, not just write every recording to the same file, e.g. instead of rewriting all the recordings over each other, it should create unique message files.

  2. The playback function should draw from a folder of all the message files and play them at random until the phone is hung up.

  3. Eventually the phone will ring!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment