Created
December 28, 2018 20:03
-
-
Save Adam-Ant/e4e61693c23110e7356b77889e72b972 to your computer and use it in GitHub Desktop.
DTMF Arduino
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
// Timer library for easy interrupt management | |
#include <TimerOne.h> | |
// Define this so we can safely use interrupts with CircularBuffer | |
#define CIRCULAR_BUFFER_INT_SAFE | |
#include <CircularBuffer.h> | |
const int stqPin = 8; | |
const int q1Pin = 9; | |
const int q2Pin = 10; | |
const int q3Pin = 11; | |
const int q4Pin = 12; | |
const int pulsePin = 13; | |
CircularBuffer<int,50> buffer; | |
bool stqRead = false; | |
int dialPulses = 0; | |
void setup() { | |
//Timer defaults to 1 second | |
Timer1.initialize(); | |
// Init DTMF Pins | |
pinMode(q1Pin, INPUT); | |
pinMode(q2Pin, INPUT); | |
pinMode(q3Pin, INPUT); | |
pinMode(q4Pin, INPUT); | |
pinMode(pulsePin, OUTPUT); | |
digitalWrite(pulsePin, HIGH); | |
// DEBUG: Init serial | |
//Serial.begin(9600); | |
} | |
//DEBUG: Print the buffer contents | |
void debugBuffer() { | |
Serial.print("Buffer contains "); | |
Serial.print(buffer.size()); | |
Serial.print(" elements: "); | |
// iterates over the events in chronological order | |
for (byte i = 0; i < buffer.size(); i++) { | |
// retrieves the i-th element from the buffer without removing it | |
Serial.print(buffer[i]); | |
Serial.print(" "); | |
} | |
Serial.println(); | |
} | |
void pulseOff () { | |
digitalWrite(pulsePin, HIGH); | |
Timer1.attachInterrupt(pulseOn); | |
Timer1.setPeriod(67000); | |
} | |
void pulseOn () { | |
digitalWrite(pulsePin, LOW); | |
dialPulses = dialPulses - 1; | |
//Serial.println(dialPulses); | |
if (dialPulses > 0) { | |
Timer1.attachInterrupt(pulseOff); | |
Timer1.setPeriod(33000); | |
} | |
else { | |
Timer1.detachInterrupt(); | |
} | |
} | |
int getDTMFValue() { | |
int DTMFRead = 0; | |
if (digitalRead(q1Pin) == HIGH) { | |
DTMFRead += 1; | |
} | |
if (digitalRead(q2Pin) == HIGH) { | |
DTMFRead += 2; | |
} | |
if (digitalRead(q3Pin) == HIGH) { | |
DTMFRead += 4; | |
} | |
if (digitalRead(q4Pin) == HIGH) { | |
DTMFRead += 8; | |
} | |
//DEBUG | |
//Serial.println(DTMFRead); | |
return DTMFRead; | |
} | |
void loop() { | |
if (!buffer.isEmpty() && dialPulses == 0) { | |
// We need to start a new number | |
dialPulses = buffer.shift(); | |
// Hold the relay on for 1 second before dialing starts | |
digitalWrite(pulsePin, LOW); | |
Timer1.attachInterrupt(pulseOff); | |
Timer1.setPeriod(1000000); | |
} | |
// Look at stq pin first, if low, set back for new number | |
if(stqRead) { | |
stqRead = digitalRead(stqPin); | |
} | |
// If Read isn't set, read the buffer and set the flag to true | |
if (stqRead == false && digitalRead(stqPin) == HIGH) { | |
buffer.push(getDTMFValue()); | |
stqRead = true; | |
//DEBUG: Print buffer | |
// debugBuffer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment