Last active
August 29, 2015 14:04
-
-
Save mapmeld/282dcd861dfbc8425675 to your computer and use it in GitHub Desktop.
Annie's Rotary Phone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
RotaryPhone.pde | |
- Put 3.3V power into the rotary phone | |
- Connect counter for dial to the Analog 0 pin | |
- Connect last-counter for dial to the Analog 2 pin | |
- Connect hangup / pickup to the Analog 5 pin | |
- setup() runs in the beginning | |
- loop() repeats every 30ms | |
--- inside the loop: read the Analog 5 pin (for hang up / pick up) | |
--- if the value is high or low, hung up the phone | |
--- if the value is fixed around 689 for 10 loops, phone has been picked up | |
--- inside the loop: read the Analog 0 pin | |
--- remember if this loop was ON or OFF | |
--- count goes +1 if this loop was ON and the last loop was OFF | |
--- count does not change if this loop was ON and the last loop was ON | |
--- when count goes +1, also reset a timer | |
--- after two seconds with no pulses (40 loops * 50ms), reset counter to 0 | |
--- if Analog 2 disconnects, stop counting and print final count | |
*/ | |
boolean ison; | |
boolean isengaged; | |
int count; | |
int timeSinceLastCount; | |
int confirmHangUp = 10; | |
int isDialing = 2; | |
int lastDialState = 0; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
// initialize serial communication at 9600 bits per second: | |
Serial.begin(9600); | |
ison = false; | |
isengaged = true; | |
count = 0; | |
timeSinceLastCount = 0; | |
pinMode(isDialing, INPUT); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
int hangupValue = analogRead(A5); | |
//Serial.print("hangup sensor = "); | |
//Serial.println(hangupValue); | |
if ( hangupValue < 600 || hangupValue > 700 ) { | |
confirmHangUp = 10; | |
if(isengaged) { | |
isengaged = false; | |
Serial.println("hung up!"); | |
} | |
} | |
else { | |
if(isengaged == false){ | |
confirmHangUp--; | |
if (confirmHangUp <= 0) { | |
Serial.println("picked up!"); | |
isengaged = true; | |
} | |
} | |
} | |
// read isDialing from digital pin 2 | |
int dialState = analogRead(A2); | |
//Serial.print("dial state = "); | |
//Serial.println(dialState); | |
if((dialState > 687 && dialState < 690) || (lastDialState > 687 && lastDialState < 690)) { | |
// read the input on analog pin 0: | |
int sensorValue = analogRead(A0); | |
if (isengaged && (sensorValue < 680 || sensorValue > 690)) { | |
//Serial.print("Reading this on A0: "); | |
//Serial.println(sensorValue); | |
if(ison == false) { | |
ison = true; | |
count++; | |
timeSinceLastCount = 0; | |
} | |
} | |
else { | |
ison = false; | |
if(count > 0){ | |
Serial.print("count = "); | |
Serial.println(count); | |
if(timeSinceLastCount > 40){ | |
count = 0; | |
} | |
timeSinceLastCount = timeSinceLastCount + 1; | |
} | |
} | |
} | |
else { | |
if (count > 0) { | |
Serial.print("final count = "); | |
Serial.println(count); | |
} | |
count = 0; | |
} | |
lastDialState = dialState; | |
delay(30); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment