Advertisement
RuiViana

DTMF

Apr 14th, 2020
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.20 KB | None | 0 0
  1. /*
  2.  
  3.   This code is a basic implementation of a DTMF decoder for detecting the 16 character
  4.   DTMF code from the analog pin A0 and gives the decoded output by checking for all the
  5.   Upper and lower tones in the DTMF matrix and gives us the corresponding number by turning
  6.   on the corresponding digital bit for the numbers 0-9 and by Serially printing the rest of
  7.   the characters.
  8.   This work is entirely based on the Kevin Banks code found at
  9.   http://www.embedded.com/design/embedded/4024443/The-Goertzel-Algorithm
  10.   so full credit to him for his generic implementation and breakdown.
  11.   The Goertzel algorithm is long standing so see
  12.   http://en.wikipedia.org/wiki/Goertzel_algorithm for a full description.
  13.   It is often used in DTMF tone detection as an alternative to the Fast
  14.   Fourier Transform because it is quick with low overheard because it
  15.   is only searching for a single frequency rather than showing the
  16.   occurrence of all frequencies.
  17.     THIS CODE IS Made/modified by "Mian Mohammad Shoaib" and  Released into the public domain.
  18.     for any querries related to the code feel free to ask at
  19.  
  20.   MMSHOAIB8452@GMAIL.COM
  21. */
  22.  
  23.  
  24. #include <Goertzel.h>
  25.  
  26. #include <LiquidCrystal_I2C.h>                                          // Biblioteca LCD I2C
  27. LiquidCrystal_I2C lcd(0x38,  2, 1, 0, 7, 6, 5, 4, 3, POSITIVE);         // Set the LCD I2C address
  28.  
  29. int sensorPin = A0;
  30. const int N = 100;                       //it is the number of samples code will take y0u can change for sensitivity and can if large it can slow the arduino
  31. const float threshold = 2000;            //minimum tone amplitude to be considered we can change it for more senstivity
  32. const float sampling_freq = 8900;        //maximum detectable frequency is the sampling rate/2 and arduino uno with 16Mhz can support sampling upto 8900 Hz
  33. float x_frequencies[4];                  // make two arrays for holding x and y axis frequencies to be detected
  34. float y_frequencies[4];
  35. //--------------------------------------------------------------------------
  36. void setup() {
  37.   pinMode(13, OUTPUT);                    //initalize blink led to show if any tone is detected
  38.   pinMode(2, OUTPUT);                     //initialize 10 pins as output to show the dtmf outputs from 2 to number 12 rest will be just derially printed to the monitor
  39.   pinMode(3, OUTPUT);
  40.   pinMode(4, OUTPUT);
  41.   pinMode(5, OUTPUT);
  42.   pinMode(6, OUTPUT);
  43.   pinMode(7, OUTPUT);
  44.   pinMode(8, OUTPUT);
  45.   pinMode(9, OUTPUT);
  46.   pinMode(10, OUTPUT);
  47.   pinMode(11, OUTPUT);
  48.   pinMode(12, OUTPUT);
  49.  
  50.   Serial.begin(115200);
  51.   lcd.begin(16, 2);                                                     // Inicialisa LCD
  52.   lcd.setCursor(0, 0);
  53.   lcd.print("Iniciando........");
  54.   delay(500);
  55.   lcd.setCursor(0, 0);
  56.   lcd.print("Frq1  Frq2 tecla");
  57.  
  58.   x_frequencies[0] = 1209;              //just initialize the arrays with the x and y axis tone frequencies along with their row and colon number
  59.   x_frequencies[1] = 1336;
  60.   x_frequencies[2] = 1477;
  61.   x_frequencies[3] = 1633;
  62.  
  63.   y_frequencies[0] = 697;
  64.   y_frequencies[1] = 770;
  65.   y_frequencies[2] = 852;
  66.   y_frequencies[3] = 941;
  67. }
  68. //--------------------------------------------------------------------------
  69. bool detect_tone(float freq)
  70. {
  71.   Goertzel goertzel = Goertzel(freq, N, sampling_freq);         //initialize library function with the given sampling frequency no of samples and target freq
  72.   goertzel.sample(sensorPin);                                   //Will take n samples
  73.   float magnitude = goertzel.detect();                          //check them for target_freq
  74.   if (magnitude > threshold)                                    //if you're getting false hits or no hits adjust the threshold
  75.   {
  76.     lcd.print(freq, 0);
  77.     return true;
  78.   }
  79.   else
  80.     return false;
  81. }
  82. //--------------------------------------------------------------------------
  83. void print_number(int row, int column) {
  84.   int number = 0;
  85.   if (row == 0) {                                         //find the number corresponding to the found row and column
  86.     if (column == 0)
  87.       number = 1;
  88.     else if (column == 1)
  89.       number = 2;
  90.     else if (column == 2)
  91.       number = 3;
  92.     else if (column == 3)
  93.       number = 10;
  94.   }
  95.   else if (row == 1) {
  96.     if (column == 0)
  97.       number = 4;
  98.     else if (column == 1)
  99.       number = 5;
  100.     else if (column == 2)
  101.       number = 6;
  102.     else if (column == 3)
  103.       number = 11;
  104.   }
  105.   else if (row == 2) {
  106.     if (column == 0)
  107.       number = 7;
  108.     else if (column == 1)
  109.       number = 8;
  110.     else if (column == 2)
  111.       number = 9;
  112.     else if (column == 3)
  113.       number = 12;
  114.   }
  115.   else if (row == 3) {
  116.     if (column == 0)
  117.       number = 14;
  118.     else if (column == 1)
  119.       number = 0;
  120.     else if (column == 2)
  121.       number = 15;
  122.     else if (column == 3)
  123.       number = 13;
  124.   }
  125.  
  126.   lcd.setCursor(13, 1);;
  127.   if (number < 10)
  128.   {
  129.     digitalWrite((number + 2), HIGH);
  130.     Serial.print("number "); Serial.println(number);
  131.     lcd.print(number);
  132.   }
  133.   else if (number == 10)
  134.   {
  135.     Serial.print("number ");Serial.print('A');
  136.     lcd.print('A');
  137.   }
  138.   else if (number == 11)
  139.   {
  140.     Serial.print("number ");Serial.println('B');
  141.     lcd.print('B');
  142.   }
  143.   else if (number == 12)
  144.   {
  145.     Serial.print("number ");Serial.println('C');
  146.     lcd.print('C');
  147.   }
  148.   else if (number == 13)
  149.   {
  150.     Serial.print("number ");Serial.println('D');
  151.     lcd.print('D');
  152.   }
  153.   else if (number == 14)
  154.   {
  155.     Serial.print("number ");Serial.println('*');
  156.     lcd.print('*');
  157.   }
  158.   else if (number == 15)
  159.   {
  160.     Serial.print("number ");Serial.println('#');
  161.     lcd.print('#');
  162.   }
  163.   delay(800);
  164. }
  165. //--------------------------------------------------------------------------
  166. void loop()
  167. {
  168.   int column = 0, row = 0;
  169.   int i = 0;
  170.   while (1)
  171.   {
  172.     lcd.setCursor(0, 1);
  173.     if (detect_tone(x_frequencies[i]) == true)
  174.     {
  175.       column = i;
  176.       digitalWrite(13, HIGH);
  177.       break;
  178.     }
  179.     i++;
  180.     if (i == 4)
  181.       i = 0;
  182.   }
  183.   i = 0;
  184.  
  185.   while (1)
  186.   {
  187.     lcd.setCursor(6, 1);
  188.     if (detect_tone(y_frequencies[i]) == true)
  189.     {
  190.       row = i;
  191.       digitalWrite(13, LOW);
  192.       break;
  193.     }
  194.     i++;
  195.     if (i == 4)
  196.       i = 0;
  197.   }
  198.   print_number(row, column);
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement