Ohjelma
#include <SPI.h>
#include <SD.h>
#include <pcmRF.h>
#include <pcmConfig.h>
#include <TMRpcm.h>
#define DEBUG
//CONSTANTS
//Configuration
//Also plug in white wire from phone to Arduino GND
// TÄMÄ ON TOIMIVA VERSIO!
const byte phonePin = 9; //red wire from phone, ääni
const byte hookPin = 8; // green wire from phone, pulssi, luuri hook tai unhook
const byte chipSelectPin = 4;
const unsigned long debounceDelay = 5; //ms, pulssit erottava aika
const unsigned long maxPulseInterval = 250; //ms
const int numDigitsInPhoneNumber = 5;
//GLOBALS
//Declare a global TMRpcm object for controlling audio playback
TMRpcm tmrpcm;
// The char representing of the number dialled (+1 to allow for string-terminating character \o)
char number [numDigitsInPhoneNumber + 1];
// The digit number currently beeing dialled
int currentDigit;
//How many pulses have been detected for current digit
int pulseCount;
//States in which the phone can be
typedef enum { ON_HOOK, OFF_HOOK, DIALLING, CONNECTED } stateType;
// Assume that the handset starts "on hook" eli luuri paikallaan
stateType state = ON_HOOK;
//In order to record "pulses" on the line, we keep track of the last pin reading...
int previousPinReading = HIGH;
//...the time at which the pin last changed value...
unsigned long timePinChanged;
//...and the current time
unsigned long now = millis ();
void setup () {
// Both pins will initially be set as inputs (with internal pullup resistors), although
// may be reassigned as outputs later
pinMode (phonePin, INPUT_PULLUP);
pinMode (hookPin, INPUT_PULLUP);
// Start the serial connection
Serial.begin (9600);
Serial.println (F("Serial connection started"));
// Open connection to the SD card
if (!SD.begin(chipSelectPin)) { //see if card is present and can be initialized:
Serial.println("SD card initialization failed!");
return; // don't do anything more if not working
}
// Volume range from 0 to 7
tmrpcm.setVolume (4);
// Enable 2x oversampling
tmrpcm.quality (1);
Serial.println ("Setup Complete");
}
void loop () {
// Is the receicer is lifted off the handset?
int hookValue = digitalRead(hookPin);
// If the receiver is lifted, but was not previously
if(hookValue == 0 && state == ON_HOOK) {
// Print some debug info
#ifdef DEBUG
Serial.println ("Receiver Lifted");
#endif
// Update the state
state = OFF_HOOK;
}
// If the receiver is on the hook, but wasn't previously
else if(hookValue == 1 && state != ON_HOOK) {
// Print some debug info
#ifdef DEBUG
Serial.println("Receiver Replaced");
#endif
// Update the puzzle state
state = ON_HOOK;
// Clear any information about the number we were dialling
pulseCount = 0;
currentDigit = 0;
// Stop any audio
tmrpcm.stopPlayback();
// Put the pin back into input state
pinMode(phonePin, INPUT_PULLUP);
}
if(state == OFF_HOOK || state == DIALLING) {
// Record the current timestamp
now = millis();
// Test the value of the phone pin
int pinReading = digitalRead(phonePin);
// If the value has changed
if (pinReading != previousPinReading) {
// The user is dialling a number
state = DIALLING;
// "Debouncing" method to ignore jittery fluctations in readings
// If the time elapsed since the last time this pin was changed is only a small amount of time
if (now - timePinChanged < debounceDelay) {
// Don't do anything
return;
}
// A HIGH signal means that a dialling pulse has been detected
if(pinReading == HIGH) {
pulseCount++;
}
// Update the stored time and reading values for further comparison
timePinChanged = now;
previousPinReading = pinReading;
}
// WE've recorded a sequence of pulses, and the time since the last pulse was detected
// is longer than the maxPulseInterval
if (((now - timePinChanged) >= maxPulseInterval) && pulseCount > 0) {
// If we haven't yet dialled a complete number
if (currentDigit < numDigitsInPhoneNumber) {
// The digit '0' is represented by 10 pulses
if (pulseCount == 10) { pulseCount = 0; }
#ifdef DEBUG
Serial.print (F("Digit dialled: "));
Serial.println (pulseCount);
#endif
// Append the most recent digit dialled onto the end of the number array
number[currentDigit] = pulseCount | '0';
// Increment the counter
currentDigit++;
// Initialize the next value
number[currentDigit] = 0;
}
// If we've dialed the correct number of digits
if (currentDigit == numDigitsInPhoneNumber) {
// Print some debug information
#ifdef DEBUG
Serial.print (F("Number dialled: "));
Serial.println (number);
#endif
// This number plays a recorded message
if (strcmp(number, "12345") == 0) {
#ifdef DEBUG
Serial.println (F("Playing sound"));
#endif
// Now, we set the pin as OUTPUT for the audio signal
pinMode(phonePin, OUTPUT);
//Set the TMRPCM library to use the pin for output
tmrpcm.speakerPin = 9; // Must be 9 on UNO
// Play the appropriate sound file
tmrpcm.play("1.WAV");
//Wait until the receiver is replaced on the handset
while(!digitalRead(hookPin)){ delay(1000);}
}
// If an incorrect number was dialled
else {
// Set the pin as OUTPUT
pinMode(phonePin, OUTPUT);
// Set the TMRPCM library to use the pin for output
tmrpcm.speakerPin = 9; // Must be 9 with UNO
//Play the appropriate sound file
tmrpcm.play("2.WAV");
// Now wait for the audio to play
delay(8500);
}
// Set the puzzle state to complere
state = CONNECTED;
}
// This digit has been processed, so reset the pulse counter for the next digit
pulseCount = 0;
}
}}
#include <SD.h>
#include <pcmRF.h>
#include <pcmConfig.h>
#include <TMRpcm.h>
#define DEBUG
//CONSTANTS
//Configuration
//Also plug in white wire from phone to Arduino GND
// TÄMÄ ON TOIMIVA VERSIO!
const byte phonePin = 9; //red wire from phone, ääni
const byte hookPin = 8; // green wire from phone, pulssi, luuri hook tai unhook
const byte chipSelectPin = 4;
const unsigned long debounceDelay = 5; //ms, pulssit erottava aika
const unsigned long maxPulseInterval = 250; //ms
const int numDigitsInPhoneNumber = 5;
//GLOBALS
//Declare a global TMRpcm object for controlling audio playback
TMRpcm tmrpcm;
// The char representing of the number dialled (+1 to allow for string-terminating character \o)
char number [numDigitsInPhoneNumber + 1];
// The digit number currently beeing dialled
int currentDigit;
//How many pulses have been detected for current digit
int pulseCount;
//States in which the phone can be
typedef enum { ON_HOOK, OFF_HOOK, DIALLING, CONNECTED } stateType;
// Assume that the handset starts "on hook" eli luuri paikallaan
stateType state = ON_HOOK;
//In order to record "pulses" on the line, we keep track of the last pin reading...
int previousPinReading = HIGH;
//...the time at which the pin last changed value...
unsigned long timePinChanged;
//...and the current time
unsigned long now = millis ();
void setup () {
// Both pins will initially be set as inputs (with internal pullup resistors), although
// may be reassigned as outputs later
pinMode (phonePin, INPUT_PULLUP);
pinMode (hookPin, INPUT_PULLUP);
// Start the serial connection
Serial.begin (9600);
Serial.println (F("Serial connection started"));
// Open connection to the SD card
if (!SD.begin(chipSelectPin)) { //see if card is present and can be initialized:
Serial.println("SD card initialization failed!");
return; // don't do anything more if not working
}
// Volume range from 0 to 7
tmrpcm.setVolume (4);
// Enable 2x oversampling
tmrpcm.quality (1);
Serial.println ("Setup Complete");
}
void loop () {
// Is the receicer is lifted off the handset?
int hookValue = digitalRead(hookPin);
// If the receiver is lifted, but was not previously
if(hookValue == 0 && state == ON_HOOK) {
// Print some debug info
#ifdef DEBUG
Serial.println ("Receiver Lifted");
#endif
// Update the state
state = OFF_HOOK;
}
// If the receiver is on the hook, but wasn't previously
else if(hookValue == 1 && state != ON_HOOK) {
// Print some debug info
#ifdef DEBUG
Serial.println("Receiver Replaced");
#endif
// Update the puzzle state
state = ON_HOOK;
// Clear any information about the number we were dialling
pulseCount = 0;
currentDigit = 0;
// Stop any audio
tmrpcm.stopPlayback();
// Put the pin back into input state
pinMode(phonePin, INPUT_PULLUP);
}
if(state == OFF_HOOK || state == DIALLING) {
// Record the current timestamp
now = millis();
// Test the value of the phone pin
int pinReading = digitalRead(phonePin);
// If the value has changed
if (pinReading != previousPinReading) {
// The user is dialling a number
state = DIALLING;
// "Debouncing" method to ignore jittery fluctations in readings
// If the time elapsed since the last time this pin was changed is only a small amount of time
if (now - timePinChanged < debounceDelay) {
// Don't do anything
return;
}
// A HIGH signal means that a dialling pulse has been detected
if(pinReading == HIGH) {
pulseCount++;
}
// Update the stored time and reading values for further comparison
timePinChanged = now;
previousPinReading = pinReading;
}
// WE've recorded a sequence of pulses, and the time since the last pulse was detected
// is longer than the maxPulseInterval
if (((now - timePinChanged) >= maxPulseInterval) && pulseCount > 0) {
// If we haven't yet dialled a complete number
if (currentDigit < numDigitsInPhoneNumber) {
// The digit '0' is represented by 10 pulses
if (pulseCount == 10) { pulseCount = 0; }
#ifdef DEBUG
Serial.print (F("Digit dialled: "));
Serial.println (pulseCount);
#endif
// Append the most recent digit dialled onto the end of the number array
number[currentDigit] = pulseCount | '0';
// Increment the counter
currentDigit++;
// Initialize the next value
number[currentDigit] = 0;
}
// If we've dialed the correct number of digits
if (currentDigit == numDigitsInPhoneNumber) {
// Print some debug information
#ifdef DEBUG
Serial.print (F("Number dialled: "));
Serial.println (number);
#endif
// This number plays a recorded message
if (strcmp(number, "12345") == 0) {
#ifdef DEBUG
Serial.println (F("Playing sound"));
#endif
// Now, we set the pin as OUTPUT for the audio signal
pinMode(phonePin, OUTPUT);
//Set the TMRPCM library to use the pin for output
tmrpcm.speakerPin = 9; // Must be 9 on UNO
// Play the appropriate sound file
tmrpcm.play("1.WAV");
//Wait until the receiver is replaced on the handset
while(!digitalRead(hookPin)){ delay(1000);}
}
// If an incorrect number was dialled
else {
// Set the pin as OUTPUT
pinMode(phonePin, OUTPUT);
// Set the TMRPCM library to use the pin for output
tmrpcm.speakerPin = 9; // Must be 9 with UNO
//Play the appropriate sound file
tmrpcm.play("2.WAV");
// Now wait for the audio to play
delay(8500);
}
// Set the puzzle state to complere
state = CONNECTED;
}
// This digit has been processed, so reset the pulse counter for the next digit
pulseCount = 0;
}
}}