carlosvolt
Published © LGPL

DTMF decoder for handy with arduino

In this tutorial we will see how to make a circuit to connect to our handy, in this case a Baofeng UV-5R (although it also works for other b

IntermediateProtip112
DTMF decoder for handy with arduino

Story

Read more

Code

Source Code

C/C++
Source Code
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X32_UNIVISION_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   //Display SSD1306 de 128x32

int codigo;
const int Pin12 = 12;  
const int Pin11 = 11;  
const int Pin10 = 10;  
const int Pin9 = 9;  
const int Pin8 = 8;  
const int Pin7 = 7;  
const int Pin6 = 6;     
const int Pin5 = 5;
const int Pin4 = 4;     
const int Pin3 = 3;
const int Pin2 = 2;

int EstadoPin2 = 0;         
int EstadoPin3 = 0;
int EstadoPin4 = 0;         
int EstadoPin5 = 0;
int EstadoPin6 = 0;         

void setup() {
   Serial.begin(9600);//Velocidad del puerto serial
   u8g2.begin();//Inicializamos el display
  // Ponemos los pines 2 al 6 como entradas para el circuto integrado MT8870DE
  pinMode(Pin2, INPUT);
  pinMode(Pin3, INPUT);
  pinMode(Pin4, INPUT);
  pinMode(Pin5, INPUT);
  pinMode(Pin6, INPUT);
  //Salidas control de luces, motores, relay, triac, etc
  pinMode(Pin7, OUTPUT);
  pinMode(Pin8, OUTPUT);
  pinMode(Pin9, OUTPUT);
  pinMode(Pin10, OUTPUT);
  pinMode(Pin11, OUTPUT);
  pinMode(Pin12, OUTPUT);
  //Inicializamos el display con la leyenda "Preparado"
  codigo = 17;
  displayOled();  
}

void loop() {

  // Leemos el estado de los pines 2 al 6
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  delay(110);
//Boton 1  10001
  if (EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("1");
  while(EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  codigo = 1; 
  digitalWrite(Pin7, HIGH);
  displayOled();   
     }
  }
  //Boton 2 01001
  if (EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("2");
  while(EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin7, LOW);
  codigo = 2;
  displayOled();
     }
  }
  //Boton 3 11001
  if (EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("3");
  while(EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin8, HIGH);
  codigo = 3;
  displayOled();     
     }
  }
  //Boton 4 00101
  if (EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("4");
  while(EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin8, LOW);
  codigo = 4; 
  displayOled();    
     }
  }
  //Boton 5 10101
  if (EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("5");
  while(EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin9, HIGH);
  codigo = 5; 
  displayOled();    
     }
  }
  //Boton 6 01101
  if (EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("6");
  while(EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin9, LOW);
  codigo = 6; 
  displayOled();  
     }
  }
  //Boton 7 11101
  if (EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("7");
  while(EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin10, HIGH);
  codigo = 7; 
  displayOled();     
     }
  }
  //Boton 8 00011
  if (EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("8");
  while(EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin10, LOW);
  codigo = 8; 
  displayOled();     
     }
  }
  //Boton 9 10011
  if (EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("9");
  while(EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin11, HIGH);
  codigo = 9; 
  displayOled();     
     }
  }
  //Boton 0 01011
  if (EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("0");
  while(EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin11, LOW);
  codigo = 0; 
  displayOled();    
     }
  }
  //Boton * 11011
  if (EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("*");
  while(EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == LOW && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin12, HIGH);
  codigo = 11; 
  displayOled();    
     }
  }
  //Boton # 00111
  if (EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("#");
  while(EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  digitalWrite(Pin12, LOW);
  codigo = 12; 
  displayOled();    
     }
  }
//Boton MENU 10111
  if (EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("MENU");
  while(EstadoPin6 == HIGH && EstadoPin5 == LOW && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  codigo = 13; 
  displayOled();     
     }
  }
  //Boton ALTO 01111
  if (EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("UP");
  while(EstadoPin6 == LOW && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  codigo = 14; 
  displayOled();   
     }
  }
  //Boton BAJO 11111
  if (EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ) {

  Serial.print("DOWN");
  while(EstadoPin6 == HIGH && EstadoPin5 == HIGH && EstadoPin4 == HIGH && EstadoPin3 == HIGH && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  codigo = 15; 
  displayOled();     
     }
  }
  //Boton EXIT 00001
  if (EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ) {

  Serial.print("EXIT");
  while(EstadoPin6 == LOW && EstadoPin5 == LOW && EstadoPin4 == LOW && EstadoPin3 == LOW && EstadoPin2 == HIGH ){
  EstadoPin2 = digitalRead(Pin2);
  EstadoPin3 = digitalRead(Pin3);
  EstadoPin4 = digitalRead(Pin4);
  EstadoPin5 = digitalRead(Pin5);
  EstadoPin6 = digitalRead(Pin6);
  codigo = 16; 
  displayOled();     
     }
  }
}

//Visualización el display
 void displayOled(){
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB10_tr);
    if(codigo == 1){
    u8g2.drawStr(0,24,"Codigo: 1");
    }
     if(codigo == 2){
    u8g2.drawStr(0,24,"Codigo: 2");
    }
     if(codigo == 3){
    u8g2.drawStr(0,24,"Codigo: 3");
    }
     if(codigo == 4){
    u8g2.drawStr(0,24,"Codigo: 4");
    }
     if(codigo == 5){
    u8g2.drawStr(0,24,"Codigo: 5");
    }
     if(codigo == 6){
    u8g2.drawStr(0,24,"Codigo: 6");
    }
     if(codigo == 7){
    u8g2.drawStr(0,24,"Codigo: 7");
    }
     if(codigo == 8){
    u8g2.drawStr(0,24,"Codigo: 8");
    }
     if(codigo == 9){
    u8g2.drawStr(0,24,"Codigo: 9");
    }
     if(codigo == 0){
    u8g2.drawStr(0,24,"Codigo: 0");
    }
     if(codigo == 11){
    u8g2.drawStr(0,24,"Codigo: *");
    }
     if(codigo == 12){
    u8g2.drawStr(0,24,"Codigo: #");
    }
     if(codigo == 13){
    u8g2.drawStr(0,24,"Codigo: MENU");
    }
     if(codigo == 14){
    u8g2.drawStr(0,24,"Codigo: UP");
    }
     if(codigo == 15){
    u8g2.drawStr(0,24,"Codigo: DOWN");
    }
     if(codigo == 16){
    u8g2.drawStr(0,24,"Codigo: EXIT");
    }
     if(codigo == 17){
    u8g2.drawStr(0,24,"Preparado");

    }
    } while ( u8g2.nextPage() );
 }

Credits

carlosvolt
34 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.