Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahwalter/af08e030f9eba9a10ea1 to your computer and use it in GitHub Desktop.
Save micahwalter/af08e030f9eba9a10ea1 to your computer and use it in GitHub Desktop.
/*
Rotary Phone Dial Reader
This sketch reads out the number dialed on a rotary phone dial.
The rotary dial has two signals:
1) turns ON when the wheel is turning
2) pulses a count based on the number dialed.
The results are printed to the serial monitor.
Supplies:
* Teensy 3.0
* Deconstructed rotary phone
The circuit:
* ON/OFF "switch" wires attached from PIN 12 to GND
* PULSE wires attached from PIN 11 to GND
Since it's readong an analog signal, there's a bit of "bounce" in each pulse.
The code adjusts for that using the "debounce" method. You can adjust the
amount of delay in "debounceDelay" if you get read errors.
Made by LIANA, with help from NAIM, with some borrowed code from ARDUINO EXAMPLES.
2014 Feb 13
*/
#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
/* Basic Digital Read
* ------------------
*newcode
*/
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
/* Basic Digital Read
* ------------------
* end newcode
*/
void setup(){
//start serial connection
Serial.begin(9600);
// configure the two inputs, and the onboard LED.
pinMode(INITPIN, INPUT_PULLUP);
pinMode(NUMPIN, INPUT_PULLUP);
/* Basic Digital Read
* ------------------
*newcode
*/
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
/* Basic Digital Read
* ------------------
* end newcode
*/
}
void loop(){
/* Basic Digital Read
* ------------------
*newcode
*/
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED OFF
Serial.println("Hello, world!");
//delay(1); // delay in between reads for stability
} else {
digitalWrite(ledPin, LOW); // turn LED ON
Serial.println("dial tone");
// delay(1); // delay in between reads for stability
}
/* Basic Digital Read
* ------------------
* end newcode
*/
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) {
if (counter == 10) {
Serial.println (0);
} else {
Serial.println (counter);
}
}
// After you're done using it, reset counter so it can start again on the
// next dial.
counter = 0;
}
}
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
AudioInput input;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup() {
size(400,400);
stroke(255);
minim = new Minim(this);
player = minim.loadFile("GuitarMan.mp3");
input = minim.getLineIn();
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
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);
// If data is available,
if ( myPort.available() > 0)
{
// get the value from the serial port
val = myPort.readStringUntil('\n'); // read it and store it in val
// print every value received to the console so we know what we got
println(val);
if (val == "dial tone"){
// do whatever you want to do when you receive a dial tone
player.play();
} else if (val == "hello world"){
// do whatever you want to do when you receive a hang up
} else {
// anything else and its input from the phone
// so do whatever you want if you receive input from the phone here...
}
}
}
void mousePressed()
{
}
void mouseReleased()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment