Safety control code

i have a dtmf module for decode dtmf tones. i have connected the 4 digitals output of the decoder on 4 digital inputs of arduino nano .with my code i drive 4 relays ,every time i send a dtmf tone. the problem is that i want to put before run my program a safety 4 digits binary code that when i send it with dtmf run the rest of my program.other wise return back and aspect the right 4 digit code to run the program .can somebody help me do it?


// Arduino Digital pins for Relay module 
const byte R1 = 12;
const byte R2 = 11;
const byte R3 = 10;
const byte R4 = 9;

// Arduino Digital pins for DTMF module 
const byte Q1 = 8;
const byte Q2 = 7;
const byte Q3 = 6;
const byte Q4 = 5;
const byte STQ = 4;

// A counter
byte count = 0;

// Variables for reading the DTMF module pin status
boolean S_Q1 = LOW;
boolean S_Q2 = LOW;
boolean S_Q3 = LOW;
boolean S_Q4 = LOW;
boolean S_STQ = LOW;

const byte PTT = 2; // Push To Talk (PTT) button on radio

void setup()
  {
  // Identify the input and output pins
  
  // Arduino input pins from DTMF module output pins
  pinMode(Q1, INPUT); // Q1 LED 0001
  pinMode(Q2, INPUT); // Q2 LED 0010
  pinMode(Q3, INPUT); // Q3 LED 0100
  pinMode(Q4, INPUT); // Q4 LED 1000
  pinMode(STQ, INPUT); // STQ goes high/on when a tone is detected

  
  pinMode(PTT, OUTPUT); // D2 connect to a 330 ohm resister to pin 1 of the optocoupler 

  pinMode(13, OUTPUT); // onboard LED

  // Pins on relay board.  A LOW turns on the relay.
  pinMode(R1, OUTPUT); // LED or control relay #1
  pinMode(R2, OUTPUT); // LED or control relay #2
  pinMode(R3, OUTPUT); // LED or control relay #3
  pinMode(R4, OUTPUT); // LED or control relay #4

  digitalWrite(R1, HIGH); // Turn off relay 1
  digitalWrite(R2, HIGH); // Turn off relay 2
  digitalWrite(R3, HIGH); // Turn off relay 3
  digitalWrite(R4, HIGH); // Turn off relay 4 
}


void loop()
{
  
      S_STQ = digitalRead(STQ);
      if (S_STQ == HIGH)
        {
        // Read the status of the DTMF module pins
        S_Q1  = digitalRead(Q1);
        S_Q2  = digitalRead(Q2);
        S_Q3  = digitalRead(Q3);
        S_Q4  = digitalRead(Q4);
        delay (3000); // pause 3 second for transmission to end
        digitalWrite(PTT, HIGH); // Turn PTT on
        digitalWrite(13, HIGH); // Turn on LED
        delay (1000); // pause 1 second before transmitting
        
        //if (S_Q1 == HIGH)
        if (S_Q1 == HIGH && S_Q2 == LOW && S_Q3 == LOW && S_Q4 == LOW) //DTMF KEY 1
          {
          digitalWrite(R1, LOW); // turn on relay #1
          
          count = count + 1;
          }
        //else
        if (S_Q1 == HIGH && S_Q2 == LOW && S_Q3 == HIGH && S_Q4 == LOW)   // DTMF KEY 5
          {
          digitalWrite(R1, HIGH); // turn off relay #1
          }

          //if (S_Q2 == HIGH)
          if (S_Q1 == LOW && S_Q2 == HIGH && S_Q3 == LOW && S_Q4 == LOW) //DTMF KEY 2
          {
          digitalWrite(R2, LOW); // turn on relay #2
          
          count = count + 1;
          }
        
        if (S_Q1 == LOW && S_Q2 == HIGH && S_Q3 == HIGH && S_Q4 == LOW) // DTMF KEY 6
          {
          digitalWrite(R2, HIGH); // turn off relay #2
                   }

          if (S_Q1 == HIGH && S_Q2 == HIGH && S_Q3 == LOW && S_Q4 == LOW)  //DTMF KEY 3
          
          {
          digitalWrite(R3, LOW); // turn on relay #3
          
          count = count + 1;
          }
      
      if (S_Q1 == HIGH && S_Q2 == HIGH && S_Q3 == HIGH && S_Q4 == LOW) //DTMF KEY 7
          {
          digitalWrite(R3, HIGH); // turn off relay #3
          
          }

          //if (S_Q4 == HIGH)
          if (S_Q1 == LOW && S_Q2 == LOW && S_Q3 == HIGH && S_Q4 == LOW) //DTMF KEY 4
          {
          digitalWrite(R4, LOW); // turn on relay #4
          
          count = count + 1;
          }
        
        if (S_Q1 == LOW && S_Q2 == LOW && S_Q3 == LOW && S_Q4 == HIGH) //DTMF KEY 8
          {
          digitalWrite(R4, HIGH); // turn off relay #4
          
          }
        
          if (S_Q1 == HIGH && S_Q2 == LOW && S_Q3 == LOW && S_Q4 == HIGH) //DTMF KEY9
          {
          digitalWrite(R1, HIGH); digitalWrite(R2, HIGH); digitalWrite(R3, HIGH); digitalWrite(R4, HIGH); 
          
          }
        
        
        digitalWrite(PTT, LOW); // turn PTT off
        digitalWrite(13, LOW); // Turn off LED
        count = 0; // Reset counter
        delay(100);
        }
      }
  }
}


πληκτρολογήστε ή επικολλήστε τον κώδικα εδώ

Welcome to the forum

Start by posting you current sketch, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Hello teletronservice

Welcome to the best Arduino forum ever.

Take a view to gain the knowledge and get to started.

Array´s are your friend to handle DTMF tone sequences.

hth

does your code even compile?

Not in the sense of writing your code, but maybe give some hints. A possible process could be like this:

Add a clause to the if-statements that trigger the relays, checking if a boolean variable (e.g. "bool authorized") is true. Make this boolean 'false' by default.

Make a function that you run as long as 'authorized' is false. It could do something along the following lines:

1: Wait for input and compare DTMF input to #1 digit of PIN code. If it matches, go to 2. If not, or timeout waiting for input, repeat.
2: repeat step 1 for digit #2. If it matches, go to 3. If not, or timeout, go back to 1.
3: repeat step 1 for digit #3. If it matches, go to 4. If not, or timeout, go back to 1.
4: repeat step 1 for digit #4. If it matches, set the bool 'authorized' to true. And maybe store a millis() timestamp.

Note that this will keep you 'authorized' for the rest of eternity. So I'd also include a check in loop that monitors how long ago 'authorized' was set to true. If this exceeds a certain time (e.g. a minute), then reset 'authorized' to false.

This is how I might approach it; see if you can translate the above into code. You can make the code a little less redundant than the description by using arrays and the same code for steps 1 through 4 of the PIN-check function.

1 Like

Have you decided if the binary code is entered left to right or right to left?

note that there is a library that could make your life easier

then you could use it by applying the principles from Serial Input Basics but instead of checking Serial.available() you check DTMF.available()

see the sample code MT8870/examples/MT8870_demo/MT8870_demo.ino at master · RobTillaart/MT8870 · GitHub

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.