Help required for DFrobot DTMF shield

In my project, I am using the DFRobot DTMF Shield (mounted on an arduino uno) with a phone shield (also mounted onto another arduino uno and a XBee unit). Basically, the DTMF shield would recognize the digits input from the phone shield and if it recognizes the digit ‘1’, it will trigger the attached wireless XBee to send a signal.

At the moment, my project works if there is only one number input ie. ‘1’. However, when I tried to modify my code for recognizing a string of consecutive digits ie. ‘1,5’ this will not work. I appreciate any advice on how I can overcome this issue.

Many thanks!

Here is the code for my DTMF shield:

#include "SoftwareSerial.h"
#include "dtmf.h"

DTMF dtmf; // default set to DFRobot DTMF board pins, Speaker pins

SoftwareSerial XBee(2, 3);

void setup()
{
Serial.begin(9600);
XBee.begin(9600);
}

void loop()
{
int myDtmf;
myDtmf = dtmf.getDTMF();
if(myDtmf != -1) Serial.println(myDtmf);
if (myDtmf ==1){
if (myDtmf ==5){ //When this is added, it cannot work
XBee.write('1');
}
}
delay(80); // to avoid getting repeated output.
}

HI @Terence16448,

Looks like you have an if condition nestled inside another, and it’s doing what it’s told.

if (myDtmf ==1){
	if (myDtmf ==5){ //When this is added, it cannot work
		XBee.write('1');
	}
}

Given the above logic, "XBee.write(‘1’); will only trigger when the integer myDtmf equals both 1 && 5 at the same time (not possible - albeit stray radiation that nudges the byte in RAM between cycles).

Thanks Graham for helping to point out this error. I now understand why the code wouldn’t work. How should I then code such that consecutive digits are recognised by the dtmf shield?

I guess you need a way to determine what has happened in the past. Perhaps use the lower and upper bytes of an unsigned integer to record the last two values. Pushing each byte left 8 steps each time; and comparing the value with 15h to check the last two entered values (note the use of hexidecimal to make it easier to read … the decimal value of 1-upperbyte and 5-lowerbyte in unsigned integer format would be 21).

1 Like

Yep, a fairly easy way is to create a char buffer for the incoming values. If you create a char Array, then store each incoming digit in the LSB (least significant bit), then shift everything 1 bit to the left (towards the MSB), then you can use an array index to get each value separately.