DTMF control electric fence for animals

this code is not good, and Im not a programmer in any way, sorry
This was made to control fence with ht radios with sending tones back
because I'm using Arduino nano that puts pins in HIGH when pinMode(OUTPUT), I have to put this inside Loop to set it LOW when I needed
Question: how to run ''if" sections inside loop once, and wait for new dtmf code to be received

Need to be

  1. Tone Received
  2. Fence relay turns unit off
    3.second relay turns ptt on radio on
    4.Speaker pin sends tone signal to let me know fence unit is off
    5.second relay turns ptt on radio off
    6.waiting to new dtmf code to be received to do another "if"
    Please help, and sorry for my russian- //..........
int x,y,z,t;

int a=2;      // D2 вход с DTMF Q1

int b=3;      // D3 вход с DTMF Q1

int c=4;      // D4 вход с DTMF Q1

int d=5;      // D5 вход с DTMF Q1

int e=6;      // D6 реле fence on/off

int f=7;      // D7 выход светодиод "fence off"

int speakerpin = 8;  // D8 audio tone output

int h=9;       // D9 управление реле radio transmition on and off

int i=10;      // D10 выход светодиод " test radio in range"





void setup() {

delay(2000);
 
pinMode(a,INPUT);

pinMode(b,INPUT);

pinMode(c,INPUT);

pinMode(d,INPUT);




pinMode(f,OUTPUT);  // led "fence off"

pinMode(speakerpin,OUTPUT);  // audio out




pinMode(i,OUTPUT);  // led "test radio in range"



Serial.begin (9600);

}

void loop() {

x=digitalRead(a);

y=digitalRead(b);

z=digitalRead(c);

t=digitalRead(d);



delay(1000);



if (x==1 && y==0 && z==0 && t==0)     // DTMF 1  выключить пастух

{pinMode(e,OUTPUT); // relay fence on/off
pinMode(h,OUTPUT); // relay control test
digitalWrite(i,HIGH);  // test led off
digitalWrite(f,LOW);  // fence led on
digitalWrite(e,LOW);
delay(3000);           // fence discharge pause
digitalWrite(h,LOW);  // transmit radio on
delay(2000);
tone(speakerpin,600,1000);
delay(1000);
tone(speakerpin,1000,1000);
delay(2000);
digitalWrite(h,HIGH);
}

if (x==0 && y==1 && z==0 && t==0)     // DTMF 2 включить пастух

{pinMode(e,OUTPUT); // relay fence on/off
pinMode(h,OUTPUT); // relay control test
digitalWrite(f,HIGH);  // fence led off
digitalWrite(e,HIGH);  // fence relay on
delay(200);              // pause before msg
digitalWrite(h,LOW);
delay(5000);
tone(speakerpin,1000,1000);
delay(1000);
tone(speakerpin,600,1000);
delay(5000);
digitalWrite(h,HIGH);
}


if (x==0 && y==0 && z==1 && t==0)     //  DTMF 4 radio "in range" test on

{pinMode(e,OUTPUT); // relay fence on/off
pinMode(h,OUTPUT); // relay control test
digitalWrite(i,LOW); // test led on
delay(200);           // pause before msg
digitalWrite(h,LOW);
delay(5000);
tone(speakerpin,600,1000);
delay(1000);
tone(speakerpin,600,1000);
delay(1000);
tone(speakerpin,600,1000);
delay(5000);
digitalWrite(h,HIGH);
}

if (x==0 && y==0 && z==0 && t==1)   // DTMF 8 radio "in range" test off

{pinMode(e,OUTPUT); // relay fence on/off
pinMode(h,OUTPUT); // relay control test
digitalWrite(i,HIGH);  // test led off
digitalWrite(h,LOW);
delay(5000);
tone(speakerpin,1000,1000);
delay(1000);
tone(speakerpin,600,1000);
delay(5000);
digitalWrite(h,HIGH);
}
}

Can you please explain how you are receiving and converting DTMF tones to digital signals for the Arduino to read? The code you have is certainly not receiving tones.

Paul

Thank You for replying Paul KD7HB de KC9PMJ;)
HT with DTMF in my hands sends DTMF to another HT connected to DTMF controller and makes pins Q1,Q2,Q3,Q4 high or low when DTMF tones received

pins Q1 connected to D2, Q2-D3, Q3-D4, Q4-D5 Arduino ,then Arduino connected to relays and leds,
one of relays turns ptt on/off, another fence controller on/off
tones to mic Jack of HT

problem is with Loop, if fence relay can stay on or off no problem
then relay for ptt, tone signal repeats on and off, but i need only run it once then stop, when waiting for another ''if'' condition

removed some junk from code

int x,y,z,t;

int a=2; // D2 DTMF Q1
int b=3; // D3 DTMF Q2
int c=4; // D4 DTMF Q3
int d=5; // D5 DTMF Q4
int e=6; // D6 relay fence on/off
int f=7; // D7 led "fence off"
int speakerpin = 8; // D8 audio tone output
int h=9; // D9 ptt relay radio transmition on and off

void setup() {
delay(2000);
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(c,INPUT);
pinMode(d,INPUT);
pinMode(f,OUTPUT); // led "fence off"
pinMode(speakerpin,OUTPUT); // audio out
pinMode(i,OUTPUT); // led "test radio in range"
pinMode(e,OUTPUT); // relay fence on/off
pinMode(h,OUTPUT); // relay "test radio in range" on/off
Serial.begin (9600);}

void loop() {
x=digitalRead(a);
y=digitalRead(b);
z=digitalRead(c);
t=digitalRead(d);
delay(200);

if (x==1 && y==0 && z==0 && t==0) // DTMF Q1 (button 1 on HT) fence off

{digitalWrite(f,HIGH); // fence led on
digitalWrite(e,HIGH); // relay on (no voltage on fence wire)
delay(3000); // fence wire discharge pause
digitalWrite(h,HIGH); // transmit radio on
delay(5000);
tone(speakerpin,600,1000); // feedback tone
digitalWrite(h,LOW);}

if (x==1 && y==0 && z==0 && t==0) // DTMF Q2 (button 2 on HT) fence on

{digitalWrite(f,LOW); // fence led off
digitalWrite(e,HIGH); // fence relay on
delay(200); // pause before msg
digitalWrite(h,HIGH);
delay(5000);
tone(speakerpin,1000,1000);
digitalWrite(h,LOW);}

if (x==0 && y==0 && z==1 && t==0) // DTMF Q3 (button 4 on HT) radio "in range" test on

{digitalWrite(h,HIGH); // //Radio transmit on
digitalWrite(i,HIGH); // test led on
delay(5000); // pause before msg
tone(speakerpin,600,1000);
delay(1000);
tone(speakerpin,600,1000);
delay(1000);
tone(speakerpin,600,1000);
digitalWrite(h,LOW); //radio off
digitalWrite(i,LOW);} // test led off
}

You are likely to have timing issues because you are matching 2 systems together, that is the DTMF decoder and the Arduino, but, because of the delay() statements, the Arduino is not available all the time to process the signals from the DTMF decoder.

You might just get away with it here though, but it is normally much better to let the loop run freely, that is without delay() statements and set timers on events and watch for their expiry.

Anyway, when the DTMF decoder gets a signal, how long is that signal available for reading on Q1 through to Q4 ? A few mS or does it latch and has to be reset ?

Add this to after the delay(200) statement and add some Serial.print() statmentns within each of the other "if" blocks, so the program flow becomes clear to you:

if (x==0 && y==0 && z==0 && t==0) {  // no DTMF
   Serial.println( "no DTMF received" ) ;
}

Well it works, it receves DTMF turns relays on and off, turns leds just fine to, only problem is that feed back to send tone back, I need to send it back once and stop, turn radio of and waiting to next dtmf to be received, but instead in goes on sends tone, goes off, and on again sends tone and iver and over again,
Was trying to use
var++;, exit(0);
on end then it works, but stops loop forever, and not reacting on anything except reset
So some commands in loop must going again
But some needed to run once and waiting for another command

You can always leave the loop() with a "return" statement, but that may not help you if the conditions which caused the unwanted repetition still exist when the loop() is called again,

I'm trying to understand your problem in terms of the program code and not the behaviour of your hardware setup.

Suppose a DTMF signal Q1 is received, the following code will execute. Because of the delays, it runs for about 8 seconds. What part repeats after it has finished ? If it repeats, it will be because variable 'x' still has a value of 1. That is the reason for my question in the previous post.

if (x==1 && y==0 && z==0 && t==0)     // DTMF Q1 (button 1 on HT) fence off

{digitalWrite(f,HIGH);  // fence led on
digitalWrite(e,HIGH);  // relay on (no voltage on fence wire)
delay(3000);           // fence wire discharge pause
digitalWrite(h,HIGH);  // transmit radio on
delay(5000);
tone(speakerpin,600,1000); // feedback tone
digitalWrite(h,LOW);}

Ok it makes sense!, You right, Thank You! that is because Q1=D2is “on” HIGH, x==1,after it was triggered by DTMF, I was not thinking that way! So,some how I need to tell to controller check D2 (x==1)once,run program,and no matter if it still x==1 not running it over again, but if it gets LOW and HIGH again, repeating the program How i can do that? Thank You!

You are reading each output of the DTMF decoder on each loop iteration so the Arduino will always get the latest value that the decoder has presented. However, if the DTMF decoder always presents the last value it has received until, say , it is reset or receives a new DTMF signal which replaces the old one, you have a problem.

Your sketch can be programmed so it handles a specific value from the decoder, but uses it only once, and waits until the decoder presents some new value before continuing.

What DTMF decoder are you using ? Maybe there is a simple way to reset it in software after reading a valid code from it.

Mt8870ds is the chip and I think that I found solution there is “stq”and “in” inputs, that i can use, thank you so much for helping 6v6gt!!!It was a pleasure!!!