I have a rotary dialing phone, and Im using an arduino to "read" the dialed number and verifies if it matches the sequence chosen previously. I already find and modify a code that does all of that, but now I want to add a button to indicate when a new phone number is being dialed. The problem is that a cant think of a way to when they are in the midle of dialing, like in the 3rd of a 7-number phone number, they can just press the button and reset.
This is my current code:
/*
Telephone Dial Interpreter and Game
LED - pin 13
Dialer - between pin 2 and ground assuming dialer is NC (normally closed)
Note: currently written to support a NC dialer.
Note: currently supports a dialer with a dialed 0 giving 1 pulse
Note: alter magicNumberSize && magicNumber to suit.
You may have to experiment with the timings depending on the dialer.
*/
const unsigned long dialingSessionTimeOut = 10000UL ; // 10 seconds of no activity
const unsigned long singleDigitDialTimeOut = 1000UL ; // 2000 mS of no activity
const unsigned long contactDebounce = 50UL ;
const unsigned long successPhaseDuration = 10000UL ; // how long LED (relay) etc. is activated if success.
const int magicNumberSize = 7 ; // number of digits to be dialed
const int magicNumber[ magicNumberSize ] = { 1, 2, 3, 4, 5, 6, 7 } ; // digit sequence to be dialed
const byte ledPin = 13 ;
const byte dialPin = 2 ;
const byte buttonPin=4;
bool inDialingSession = false ;
bool inSingleDigitDial = false ;
bool inSuccessPhase = false ;
unsigned long pulseReceivedAtMs = 0 ;
unsigned long successPhaseStartedAtMs = 0 ;
int numberOfPulsesReceivedForCurrentDigit = 0 ;
int currentDigitIndex = 0 ;
bool dialPinLast;
bool dialPinCurrent ;
int collectedDialedDigit[ magicNumberSize ] ; // here are our results
void setup() {
Serial.begin( 9600 ) ;
pinMode( ledPin, OUTPUT ) ;
pinMode( dialPin, INPUT_PULLUP ) ; // external pullup/down depending on dial NC = pullup, NO = pulldown
dialPinLast = digitalRead( dialPin ) ;
Serial.println( "Dialer Game Starting. . . " ) ;
}
void loop() {
if ( inSuccessPhase ) {
if ( millis() - successPhaseStartedAtMs < successPhaseDuration ) {
digitalWrite( ledPin, HIGH ) ;
// put code to hanle unlocking / relay etc. here
}
else {
digitalWrite( ledPin, LOW ) ;
inSuccessPhase = false ;
Serial.println( "End of SUCCESS period" ) ;
}
}
if ( inDialingSession ) digitalWrite( ledPin, digitalRead( dialPin ) ) ; // flash led with dial pulses
if ( inDialingSession && millis() - pulseReceivedAtMs > dialingSessionTimeOut ) {
// abandoned session - cleanup
inDialingSession = false ;
inSingleDigitDial = false ;
numberOfPulsesReceivedForCurrentDigit = 0 ;
Serial.println( "Dialing Session Timeout reached" ) ;
}
if ( inSingleDigitDial && millis() - pulseReceivedAtMs > singleDigitDialTimeOut ) {
// dialling of current Digit has ended
// Only one line below should be active depending on the type of dial uses:
// Case 1: a dialed 0 gives 1 pulse, then subtract 1 for all digits
//numberOfPulsesReceivedForCurrentDigit -- ;
// Case 2: a dialed 0 delivers 10 pulses
if (numberOfPulsesReceivedForCurrentDigit == 10 ){
numberOfPulsesReceivedForCurrentDigit = 0 ;
}
Serial.println () ;
Serial.print ("Digit " ) ;
Serial.print ( currentDigitIndex ) ;
Serial.print ("; pulses received= " ) ;
Serial.println ( numberOfPulsesReceivedForCurrentDigit ) ;
collectedDialedDigit[ currentDigitIndex ] = numberOfPulsesReceivedForCurrentDigit ;
if ( currentDigitIndex + 1 == magicNumberSize ) {
// we have all our digits 0..6 so teminate session
Serial.println ( "Terminating Current Dialing Session" ) ;
inDialingSession = false ;
bool success = true ;
for ( int i = 0 ; i < magicNumberSize ; i++ ) {
if ( magicNumber[ i ] == collectedDialedDigit[ i ] ) {
Serial.print("digit: " ) ;
Serial.print( i + 1) ;
Serial.println (" matched magic number" ) ;
}
else {
Serial.print("digit: " ) ;
Serial.print( i + 1 ) ;
Serial.println (" did NOT match magic number" ) ;
success = false ;
}
}
if (success) {
Serial.println ("SUCCESS" ) ;
inSuccessPhase = true ;
successPhaseStartedAtMs = millis() ;
}
else {
Serial.println ("FAILURE" ) ;
}
Serial.println () ;
Serial.println () ;
Serial.println () ;
// check here if it was a success
}
inSingleDigitDial = false ;
currentDigitIndex++ ;
}
dialPinCurrent = digitalRead( dialPin) ;
if ( dialPinCurrent != dialPinLast && dialPinCurrent == HIGH && millis() - pulseReceivedAtMs > contactDebounce ) {
if ( ! inDialingSession ) {
Serial.println() ;
Serial.println() ;
Serial.println( "Start new dialing session") ;
inDialingSession = true ;
// startOfDialingSessionMs = millis() ;
currentDigitIndex = 0 ;
inSingleDigitDial = false ; // force cleanup
}
if ( ! inSingleDigitDial ) {
Serial.println() ;
Serial.println( "Start new single digit") ;
inSingleDigitDial = true ;
numberOfPulsesReceivedForCurrentDigit = 0 ;
}
Serial.println ( "dial pulse received" ) ;
pulseReceivedAtMs = millis() ;
numberOfPulsesReceivedForCurrentDigit ++ ;
}
dialPinLast = dialPinCurrent ;
}