Sei sulla pagina 1di 4

#include <DTMF.

h> // Includes the DMTF decoder library

int sensorPin = A5; // This sets the sensor pin as analog pin 5

int star = 13; // This makes the variable 'star' represent digital pin 12
int hash = 12; // This makes the variable 'hash' represent digital pin 13

int last0 = 0; // These will be used to remember the state of each relay to be able
to tell if they are on or off
int last1 = 0;
int last2 = 0;
int last3 = 0;
int last4 = 0;
int last5 = 0;
int last6 = 0;
int last7 = 0;
int last8 = 0;
int last9 = 0;
int laststar = 0;
int lasthash = 0;

float n=128.0; // Some decoding stuff (doesn't have to do w/ standard arduino)


float sampling_rate=8926.0; // Sampling rate in Hz

DTMF dtmf = DTMF(n,sampling_rate); // Initialize the dtmf library with the number
of samples to be taken and the sampling rate.

void setup(){ // The setup protion of the program only runs once, this is just like
when programming Java
pinMode(2, OUTPUT); // This sets the following pins as outputs, sensorPin (analog
5) is not mentioned as analog pins are only for input
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(star, OUTPUT); // 'star' represents digital pin 12
pinMode(hash, OUTPUT); // 'hash' represents digital pin 13
Serial.begin(115200); // Begins serial communication at 115200 baud (can be set
to 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200
baud)
}

int nochar_count = 0;
float d_mags[8];

void loop(){ // The loop part of the program runs continuously, this is much like
putting your code in a for or while loop, that loops forever
char thischar;

dtmf.sample(sensorPin); // Takes a sample of the audio

dtmf.detect(d_mags,506);

thischar = dtmf.button(d_mags,4800.); // This stores the decoded character in


'thischar'. Default was 1800, changed to 4800 (this isn't important for you)

if(thischar){
Serial.println(thischar); // Prints 'thischar' through USB, can be read with
the serial monitor
nochar_count = 0;

// Note that the relays aren't wired for the DTMF tone they are triggered by;
DTMF 0 is pin 2, DTMF 1 is pin 3, etc...

if(thischar == '0'){ ////////////////////////// 0


if(last0 == 0){ // If this was off
digitalWrite(2, HIGH); // Turn it on
last0 = 1;
}
else{ // Otherwise // Otherwise
digitalWrite(2, LOW); // Turn it off
last0 = 0;
}
}
else if(thischar == '1'){ ////////////////////////// 1
if(last1 == 0){ // If this was off
digitalWrite(3, HIGH); // Turn it on
last1 = 1;
}
else{ // Otherwise
digitalWrite(3, LOW); // Turn it off
last1 = 0;
}
}
else if(thischar == '2'){ ////////////////////////// 2
if(last2 == 0){ // If this was off
digitalWrite(4, HIGH); // Turn it on
last2 = 1;
}
else{ // Otherwise
digitalWrite(4, LOW); // Turn it off
last2 = 0;
}
}
else if(thischar == '3'){ ////////////////////////// 3
if(last3 == 0){ // If this was off
digitalWrite(5, HIGH); // Turn it on
last3 = 1;
}
else{ // Otherwise
digitalWrite(5, LOW); // Turn it off
last3 = 0;
}
}
else if(thischar == '4'){ ////////////////////////// 4
if(last4 == 0){ // If this was off
digitalWrite(6, HIGH); // Turn it on
last4 = 1;
}
else{ // Otherwise
digitalWrite(6, LOW); // Turn it off
last4 = 0;
}
}
else if(thischar == '5'){ ////////////////////////// 5
if(last5 == 0){ // If this was off
digitalWrite(7, HIGH); // Turn it on
last5 = 1;
}
else{ // Otherwise
digitalWrite(7, LOW); // Turn it off
last5 = 0;
}
}
else if(thischar == '6'){ ////////////////////////// 6
if(last6 == 0){ // If this was off
digitalWrite(8, HIGH); // Turn it on
last6 = 1;
}
else{ // Otherwise
digitalWrite(8, LOW); // Turn it off
last6 = 0;
}
}
else if(thischar == '7'){ ////////////////////////// 7
if(last7 == 0){ // If this was off
digitalWrite(9, HIGH); // Turn it on
last7 = 1;
}
else{ // Otherwise
digitalWrite(9, LOW); // Turn it off
last7 = 0;
}
}
else if(thischar == '8'){ ////////////////////////// 8
if(last8 == 0){ // If this was off
digitalWrite(10, HIGH); // Turn it on
last8 = 1;
}
else{ // Otherwise
digitalWrite(10, LOW); // Turn it off
last8 = 0;
}
}
else if(thischar == '9'){ ////////////////////////// 9
if(last9 == 0){ // If this was off
digitalWrite(11, HIGH); // Turn it on
last9 = 1;
}
else{ // Otherwise
digitalWrite(11, LOW); // Turn it off
last9 = 0;
}
}
else if(thischar == '*'){ ////////////////////////// *
if(laststar == 0){ // If this was off
digitalWrite(star, HIGH); // Turn it on
laststar = 1;
}
else{ // Otherwise
digitalWrite(star, LOW); // Turn it off
laststar = 0;
}
}
else if(thischar == '#'){ ////////////////////////// #
if(lasthash == 0){ // If this was off
digitalWrite(hash, HIGH); // Turn it on
lasthash = 1;
}
else{ // Otherwise
digitalWrite(hash, LOW); // Turn it off
lasthash = 0;
}
}

delay(50); // Provides a 50ms delay (this is so relays are quickly activated and
then deactivated)