DTMF toggle LED

Hi. I have a DTMF module connected to pins D2, D3, D4 and D5 of a nano. I have the DTMF connected by cable to a cell phone. I want to control pin D6 which is connected to an LED from a remote phone.
When I press digit 1 on the keypad of the remote phone pin D5 goes HIGH on the nano and I want this to turn ON the LED connected to pin D6. Easy so far.
I want to toggle the LED connected to pin6 by pressing digit 1 on the remote phone. In other words I want to toggle the state of the LED by pressing the same key, digit 1on the remote phone.
It's quite common to be able to do this with a physical toggle switch and I've seen examples on Youtube (see code below) where two variables are stored, the LED state and the physical button state but I can't figure out what variables I should use.
Any general help would be very much appreciated.
Thanks in advance.
Dermot
int LEDState = 0;
int LEDPin = 8;
int buttonPin = 12;
int buttonNew;
int buttonOld = 1;
int dt = 100; //delay time
void setup() {
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonNew = digitalRead(buttonPin);
if (buttonOld == 0 && buttonNew == 1) {
if (LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
} else {
digitalWrite(LEDPin, LOW);
LEDState = 0;
}
buttonOld = buttonNew;
delay(dt);
}
}

Would you supply a drawing of this?

if (digitalRead(5))
  digitalWrite(6, HIGH);

Does nothing.

"Turn on" and "toggle" are two different functions.

Please explain your plan for how pressing one particular key should decide which action to perform.

Fixedit.

int LEDState = 0;
int LEDPin = 8;
int buttonPin = 12;
int buttonNew;
int buttonOld = 0;
int dt = 100; //delay time

void setup() {
  Serial.begin(9600);
  pinMode(LEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonNew = digitalRead(buttonPin);
  Serial.print(buttonOld);
  Serial.print(buttonNew);
  Serial.print(LEDState);
  Serial.print(" ");

  if (buttonOld == 0 && buttonNew == 1) {
    if (LEDState == 0) {
      digitalWrite(LEDPin, HIGH);
      LEDState = 1;
    } else {
      digitalWrite(LEDPin, LOW);
      LEDState = 0;
    }
    // buttonOld = buttonNew;
    delay(dt);
  }
  delay(dt);
}

LED turns on when button is pressed, off when button is pressed again.

diagram.json for wokwi.com

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -118.8,
      "left": 32.6,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -134.5,
      "left": 99.5,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -52.8,
      "left": 95.45,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -43.2,
      "left": 18.65,
      "rotate": 90,
      "attrs": { "value": "330" }
    }
  ],
  "connections": [
    [ "nano:12", "btn1:2.l", "green", [ "v-144", "h105.8" ] ],
    [ "nano:5V", "btn1:1.r", "red", [ "h19.2", "v-144.2" ] ],
    [ "r2:1", "btn1:2.r", "green", [ "h0" ] ],
    [ "nano:GND.2", "r2:2", "black", [ "v0" ] ],
    [ "led1:A", "nano:8", "green", [ "v0" ] ],
    [ "r1:1", "led1:C", "green", [ "h0" ] ],
    [ "nano:GND.2", "r1:2", "black", [ "v0" ] ]
  ],
  "dependencies": {}
}

Hi, @dermotx
Welcome to the forum.

This link explains how to post your code.
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

What model Arduino are you using?

Thanks... Tom... :grinning: :+1: :coffee: :australia:

Hi Guys. Thanks very much for the replies.
I hope the attached diagram will help.
I want to ring from the remote phone to the phone attached to the DTMF module which will have automatic answering enabled. I will then key in the digit 1 and Q1 will go high and also pin A1 on the nano. With coding this will make pin D2 go high thereby turning on Relay 1 on the relay board. All this is easy enough, but I then want to key in digit 1 again on the remote phone to turn off relay 1.
Unfortunately the output from the DTMF module will not have changed, Q1 will be high and Q2,Q3 and Q4 will still be low.
Maye what I'm asking for isn't possible.
Thanks again for all the replies.
Dermot
DTMF toggle.pdf (186.1 KB)

Please answer the question in post #4.

Not accordianly to the diagram you link.

The DTMF module has four connections. You would need four to communicate which if 12 buttons was pressed, and what is missing is any signal that says a valid tone is being recognized and decoded.

Can you post a sketch that simply prints the numbers as you dial them digit by digit?

a7

Thanks for reply. By pressing digit 1 on the remote phone it will turn Q1 HIGH. As Q1 is attached to A1, A1 will also go HIGH. So in the code ....we digital read A1 and if A1 HIGH then D2 goes HIGH and hence relay 1 which is connected to D2 will activate or turn on.
I don't have a plan for pressing the same digit 1 again will turn off the relay. That is the problem I'm looking for the solution to.

It would make sense to simply have the "1" key toggle the output, rather than set it HIGH.

You can't have it both ways, unless you implement something like detecting a long or short press.

Thanks for reply. You are quite right. My apologies for my mistake. I'm trying a few different things to find a solution and have changed the circuit slightly since yesterday. The schematic is now as per the diagram attached.
Digit 1 on remote phone is pressed which results in A4,A3,A2 being low and A4 being HIGH. These states are read and in the code this puts D2 HIGH which turns the relay 1 ON.
I want to press the digit 1 on the remote phone again to turn the relay OFF and press it a further time to turn it on again, ie to toggle relay 1.

Hi, @dermotx
Schematic.

Tom... :grinning: :+1: :coffee: :australia:

That's exactly what I want to do, to toggle the relay. I don't care what it's initial state is.

Ahh, so the following statement was totally misleading! Thanks for clarifying.

When I press digit 1 on the keypad of the remote phone pin D5 goes HIGH on the nano and I want this to turn ON the LED connected to pin D6.

Easy enough to toggle instead.
if (digit1_detected) digitalWrite(6, !digitalRead(6));

You will want to detect when D5 becomes HIGH, rather than D5 is HIGH. Use a state machine to do that.

Thanks for that. I'll play around with that and try to find out what a "state machine" is.
Regards
Dermot

Arduino IDE Files>Examples>02.Digital>StateChangeDetection

Will do, much appreciated.

You probably meant A1 first. Or last.

What is the DTMF module? I would like to know what a1..a4 do in each of the 12 buttons pressed case.

Since DTMF is used for 16 key switches, I am also wondering if there is not a signal availbe saying yes, there is a button pressed or not.

That signal might be easier to monitor and react to if you go beyond wanting to react to more than just one button, even if none of the 12 keys reports all zeroes.

a7

Hello dermotx

Welcome to the world's best Arduino forum ever.

Post DTMF chip data sheet.

Have a nice day and enjoy coding in C++.


Do you have this MT8870 module?

If so, there is a "tone detected" output, in addition to the binary digit outputs.