Displaying DTMF Buffer Array in Serial Print [SOLVED]

:slight_smile:
Hello Fellow Arduino Community,
I’m having a hard time printing the array from my buffer. I’m using a DTMF module (DTMF Module Datasheet )to create an array in a buffer then to compare it to my ‘Master’. My master array is fine, but for some reason my Data array is blank.
Thanks for any help.

My Code:
{Removed since I added the whole thing below}

My Output:

{Removed since I added it below}

Please post your code.

Well, you commented out the line

//      char array = char(tmp,BIN);

which looks as if it is related to filling the array. So...

aarg:
Well, you commented out the line

**// char array = char(tmp,BIN); **

which looks as if it is related to filling the array. So...

I still have to take time to look at the OP's problem, however this line of code is quite flawed.

BIN is a macro with value 2.

tmp,BIN is an expression using the comma operator, which in effect makes the line equivalent to:
char array = 2;

AWOL:
Please post your code.

Here is all my code:

#include <Tone.h>

#define Password_Length 4 // Give enough room x characters +  1 NULL char
char Data[Password_Length]; // 6 is the number of chars it can hold + null
char Master[Password_Length] = "222"; //debugging password
byte data_count = 0, master_count = 0; 
bool Pass_is_good;
char customKey;
//char strcmp;



// using Tone library
Tone freq1;
Tone freq2;
// create variable and string array for dtmf_freq1 and dtmf_freq2
const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477 };
const int DTMF_freq2[] = {  941,  697,  697,  697,  770,  770,  770,  852,  852,  852 };

//map the bits to a pin 0-7, + 1 ready bit
int dr = 12; //dr goes high when data ready
int d0 = 11; // 0 bit, bit 1
int d1 = 10; // 1 bit, bit 2
int d2 = 9;  // 2 bit, bit 3
int d3 = 8;  // 3 bit, bit 4
int a1 = 6;
int a2 = 7;
int buffer = 0;
char result;
//char reread;
String stringone; //use this to hold the current array

//LED Pin 
int ledPin = 13;

//sets data_ready flag to 0
//create data array and sent all to 0
char data_ready = 0, data[4] = {0, 0, 0, 0};
char data_ready_status = 0;
char dr_serviced = 0;
char tx_in_progress = 0;


void setup() {

  Serial.begin(9600);
  Serial.println("Super Secret Silo- Activation");

//intialize pins for Module
  pinMode(d0, INPUT);
  pinMode(d1, INPUT);
  pinMode(d2, INPUT);
  pinMode(d3, INPUT);
  pinMode(dr, INPUT);
  
  

  freq1.begin(a1);
  freq2.begin(a2);
  
  //setup LED pin
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  }



//**********************-BEGIN LOOP-**********************
void loop() {

  static unsigned long int time, task, tx_time;
  char tmp;

  time = millis();
  //data_ready_status is pin 6, HIGH or LOW, goes to high when ready
  data_ready_status = digitalRead(dr);
  
  
//  //*** UNCOMMENT THIS OUT IF YOU WANT THE AUTO LOOP OF RANDOM NUMBER*******
// // OTHERWISE USE AN EXTERANL DTMF DEVICE TO INJECT SIGNAL TO MODULE

  //    Serial.print("Task 1 ");
  //    Serial.println(task);
//  if (time - task > 1000) // send random char every 500ms
//  {
//
//
//    task = time;
//    //If something was already being read, time it out. Capture not complete
//    if (tx_in_progress) {
//      Serial.println("Rx Timeout");
//    }
//
//    //declare randNumber and set it to random 1-9
//    char randNumber = random(9);
//
//
//    Serial.print("\n\rTransmitt: ");
//    Serial.println(randNumber, DEC);
//
//    //Pass randNumber through playDTMF function
//    playDTMF(randNumber, 500);
//    //set bit to 1 to signify valid tone received
//    tx_in_progress = 1;
//  }

// If module flag "hears" tone, it sets to TRUE
  if (data_ready_status)
  {
    if (!dr_serviced)
    {
      
      
      //read data pins          
      data[0] = digitalRead(d0);
      data[1] = digitalRead(d1);
      data[2] = digitalRead(d2);
      data[3] = digitalRead(d3);

      tmp = dtmf_digit(); //assemble dtmf digit using the function below
      
//      char array = char(tmp,BIN);     
      Serial.print("Recieve: ");
      Serial.println(tmp, DEC);  // this display works fine
      Serial.println();
      Serial.print("******************");
      Serial.println();
      
      //STORE INTO ARRY
      Data[data_count] = tmp; // store char into data array
      
      String stringOne = Data;
      
      Serial.println();
      Serial.print("%%%%%%% ");  
      Serial.print("Current Array is  ");
      Serial.print(Data); // <<-------------THIS IS WHERE THE STRING IS EMPTY
      Serial.print(" %%%%%%%%"); 
      Serial.println();
       
      data_count++;
            
      if (data_count == Password_Length-1) {
        Serial.println();
        Serial.print("Password is ");


        if (!strcmp(Data, Master)) { // equal to (strcmp(Data, Master) == 0
        // Here we compare the Data array to the Master password

        Serial.print(" GOOD ");
        digitalWrite(ledPin, HIGH);
        delay(5000);
        data_count =0;
        }
        
        else 
        Serial.print(" BAD ");
        Serial.println();
        digitalWrite(ledPin, LOW);
        
        buffer = 0;
        data_count = 0;
        delay (1000);
        
        
      }
  
      

      // This just for indexing
      Serial.print("Index Counter is at: ");
      Serial.print(data_count);
      Serial.println();
      Serial.print("******************");
      Serial.println();
      
      
      dr_serviced = 1; //clear received flag
      tx_in_progress = 0;   //clear  tx in progress flag
    }

  }
  else
    dr_serviced = 0;


}
//END LOOP HERE(((((((((((((((((((((((((((((((((((((((((((((((())))))))))


// ----DTMF receive functions ----
char dtmf_digit ( void  )  //assemble the bits into a digit
{
  char dtmf_digit;
 
   // dtmf digit is decoded per Page 5 Table 1 of chip datasheet
  // http://www.zarlink.com/zarlink/mt8870d-datasheet-oct2006.pdf

  dtmf_digit = 8 * data[3] +  4 * data[2] + 2 * data[1] + data[0] ;
  

  if (dtmf_digit == 10)
    dtmf_digit = 0;

  return dtmf_digit;
}

// ----DTMF Send functions ----
void playDTMF(uint8_t number, long duration)
{
  freq1.play(DTMF_freq1[number], duration);
  freq2.play(DTMF_freq2[number], duration);
}

int interpret() {
 
//  int Q1State = digitalRead(Q1);
//  int Q2State = digitalRead(Q2);
//  int Q3State = digitalRead(Q3);
//  int Q4State = digitalRead(Q4);
 
  if (0){result = '1' ;}
  if (0){result = '2' ;}
  if (0){result = '3' ;}
  if (0){result = '4' ;}
  if (0){result = '5' ;}
  if (0){result = '6' ;}
  if (0){result = '7' ;}
  if (0){result = '8' ;}
  if (0){result = '9' ;}
  if (0){result = '0' ;}
 
  return result;
}

void clearData()
{
  while(data_count !=0)
  { // This can be used for any array size.
    Data[data_count--] = 0; //clear array for new data
  }
  Serial.println();
  Serial.print("Buffer Cleared");
  Serial.println();

  return;
  
}

Here is the output after pressing '222' on my DTMF device:

Notice the "Current Array is {blank}"

Super Secret Silo- Activation
Recieve: 2


%%%%%%% Current Array is %%%%%%%%
Index Counter is at: 1


Recieve: 2


%%%%%%% Current Array is %%%%%%%%
Index Counter is at: 2


Recieve: 2


%%%%%%% Current Array is %%%%%%%%

Password is BAD
Index Counter is at: 0


Let's take this from the top. What do these arrays do? I see that they have frequencies in them, but why are there duplicates?

// create variable and string array for dtmf_freq1 and dtmf_freq2
const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477 };
const int DTMF_freq2[] = {  941,  697,  697,  697,  770,  770,  770,  852,  852,  852 };

What are you up to?

But to leap ahead a bit,

      Serial.print(Data); // <<-------------THIS IS WHERE THE STRING IS EMPTY

For this to work, Data[] has to contain a null terminated ASCII string. In your case, I don't see that you have any code to put one there.

#define Password_Length 4 // Give enough room x characters +  1 NULL char
char Data[Password_Length]; // 6 is the number of chars it can hold + null

Err, no.

  if (0){result = '1' ;}
  if (0){result = '2' ;}
  if (0){result = '3' ;}
  if (0){result = '4' ;}
  if (0){result = '5' ;}
  if (0){result = '6' ;}
  if (0){result = '7' ;}
  if (0){result = '8' ;}
  if (0){result = '9' ;}
  if (0){result = '0' ;}

Oh, aye.

Well at least it doesn't do anything. Safer that way. :slight_smile:

aarg:
Let's take this from the top. What do these arrays do? I see that they have frequencies in them, but why are there duplicates?

// create variable and string array for dtmf_freq1 and dtmf_freq2

const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477 };
const int DTMF_freq2[] = {  941,  697,  697,  697,  770,  770,  770,  852,  852,  852 };



What are you up to?

But to leap ahead a bit,


Serial.print(Data); // <<-------------THIS IS WHERE THE STRING IS EMPTY



For this to work, Data[] has to contain a null terminated ASCII string. In your case, I don't see that you have any code to put one there.

I am using an external device to send the tone to the module. In this case I'm pressing '2' three times (hence my output).

const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477 };
const int DTMF_freq2[] = {  941,  697,  697,  697,  770,  770,  770,  852,  852,  852 };

^^--> This is part of the tone.h library, this works fine.

 Serial.print(Data); // <<-------------THIS IS WHERE THE STRING IS EMPTY

^^---> this is where the problem is. If you notice on my output, the character '2' is being detected but I'm unable to save that into the 3 string buffer.

Is my syntax correct or should I be using

Data[]

?

I got rid of this function. It was some residual code for something else I was playing around with.

I added brackets to the 'Data[]' argument but the compiler error'd out.

      Serial.println();
      Serial.print("%%%%%%% ");  
      Serial.print("Current Array is  ");
      Serial.print(Data[]); // <<-------------THIS IS WHERE THE STRING IS EMPTY
      Serial.print(" %%%%%%%%"); 
      Serial.println();

Error is:

Access_REV0.ino: In function 'void loop()':
Access_REV0.ino:136:25: error: expected primary-expression before ']' token
Error compiling.

Please post your code inside code tags. Then post your error message inside code tags. Don't take screen shots of them.

How to use this forum

Done.

      Serial.print(Data[]); // <<-------------THIS IS WHERE THE STRING IS EMPTY

I don't know what this line is trying to do, but syntactically it is wrong. Are you trying to print every element of an array? If so, you need a loop to do that.

I am using this to debug "why" my array is empty (I'm not a programmer so I see what works and what doesn't in code).

My array is set up to be complete after three digits. I would like to print the partial array (and ultimately the complete array) in the terminal.

This works fine:

//      char array = char(tmp,BIN);     
      Serial.print("Recieve: ");
      Serial.println(tmp, DEC);  // this display works fine
      Serial.println();
      Serial.print("******************");
      Serial.println();

I would like to print for example:

Cycle 1:

'2'

Cycle 2:

'22'

Cycle 3:

'222'

The problem is the

Data

variable is blank even though

tmp

always detects the correct digit.

Excuse my non-programmer talk. I am very illiterate when it comes to communicating with software developers.

Well, in non-programmer talk, there's nothing in your array because you never put anything there.

aarg:
Well, in non-programmer talk, there's nothing in your array because you never put anything there.

Well that settles that.

So even though

data_count

is incrementing, this is wrong?

      //STORE INTO ARRY
      Data[data_count] = tmp; // store char into data array
      String stringOne = Data;

Did you ever post that code? Oh, I see... you went back and edited a previous post.
Well, the values in that array can be printed like this:

Serial.print(Data[0]);

Substitute a variable for zero, and you can print the elements you want. Okay?

aarg:
Did you ever post that code? Oh, I see... you went back and edited a previous post.
Well, the values in that array can be printed like this:

Serial.print(Data[0]);

Substitute a variable for zero, and you can print the elements you want. Okay?

I tried

Serial.print(Data[0]); // <<-------------THIS IS WHERE THE STRING IS EMPTY

but its still prints empty.

Just as a sanity check I changed

char Master[Password_Length] = "234"; //debugging password

along with

Serial.print(Master[data_count]); // <<-------------THIS IS WHERE THE STRING IS EMPTY

and printed

Super Secret Silo- Activation
Recieve: 2


%%%%%%% Current Array is 2 %%%%%%%%
Index Counter is at: 1


Recieve: 2


%%%%%%% Current Array is 3 %%%%%%%%
Index Counter is at: 2


Recieve: 2


%%%%%%% Current Array is 4 %%%%%%%%

Password is BAD
Index Counter is at: 0


So I can force an array characters to print.

Perhaps the datatype for

tmp

is problematic? It returns a

char

from the function

dtmf_digiti()