Arduino Trials: Morse code translator

This program is for translating a word to morse code by using a buzzer. It only works with single words just as an example of how you can do a morse code project with Arduino.

To do it you need:

- Arduino UNO

- Protoboard

- Buzzer

- 3 push buttons

- 3 330Ω resistors

- Connection wires

Here is the connection diagram:

And finally, here is the code. Hope it is useful for you.

//General variables: i=cursor_char, j=cursor_char2
int i=0; int j=0;
//Buzzer
int buzzer=9;
//Buttons: bm=change, bs=select, bp=translate
int bm=7; int bs=6; int bp=5;
//Array with alphabet characters
char Char[]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
             'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
/*Array to save characters we´ve selected.
-> The maximum amount of characters can save here is 20 but it´s up to you to change it.*/
char Char2[20];

void setup(){
  pinMode(buzzer,OUTPUT); pinMode(bm,INPUT); pinMode(bs,INPUT); pinMode(bp,INPUT);
  Serial.begin(9600);
  Serial.println(""); Serial.println(""); Serial.println(" << MORSE TRANSLATOR >>"); Serial.println("");
  Serial.println("Press 'change' to choose other character and 'select' to save it.");
  Serial.println("A"); //Print "A" because otherwise you wouldn´t know if the program has started.
}

void loop(){
  if(digitalRead(bm)==1){
    i++; //The cursor moves forward the array each time we press 'change'.
    if(i==26){i=0;} //When the cursor comes to 'Z', it moves back to 'A'.
    Serial.println(Char[i]); //Print current character.
    delay(300);
  }

  if(digitalRead(bs)==1){
    Serial.print("-> You selected: "); Serial.println(Char[i]); //Print current selected character.
    Char2[j]=Char[i]; //Save current selected character in the second array.
    j++; //The cursor moves to the next space in the array because the previus one now is occupied.
    i=0; //The cursor moves back to 'A'.
    Serial.println("A"); //Print "A" to show we can choose another character.
    delay(300);
  }

  if(digitalRead(bp)==1){
    Serial.print("-> PALABRA: "); //Once we selected all the characters we wanted, we press 'translate'.
    for(int x=0; x<=j-1; x++){ //The cycle takes evey character in the second array and calls its function.
      if(Char2[x]=='A'){Serial.print(Char2[x]); A();}
      if(Char2[x]=='B'){Serial.print(Char2[x]); B();}
      if(Char2[x]=='C'){Serial.print(Char2[x]); C();}
      if(Char2[x]=='D'){Serial.print(Char2[x]); D();}
      if(Char2[x]=='E'){Serial.print(Char2[x]); E();}
      if(Char2[x]=='F'){Serial.print(Char2[x]); F_();} //F_() because Arduino doesn´t allow the name F().
      if(Char2[x]=='G'){Serial.print(Char2[x]); G();}
      if(Char2[x]=='H'){Serial.print(Char2[x]); H();}
      if(Char2[x]=='I'){Serial.print(Char2[x]); I();}
      if(Char2[x]=='J'){Serial.print(Char2[x]); J();}
      if(Char2[x]=='K'){Serial.print(Char2[x]); K();}
      if(Char2[x]=='L'){Serial.print(Char2[x]); L();}
      if(Char2[x]=='M'){Serial.print(Char2[x]); M();}
      if(Char2[x]=='N'){Serial.print(Char2[x]); N();}
      if(Char2[x]=='O'){Serial.print(Char2[x]); O();}
      if(Char2[x]=='P'){Serial.print(Char2[x]); P();}
      if(Char2[x]=='Q'){Serial.print(Char2[x]); Q();}
      if(Char2[x]=='R'){Serial.print(Char2[x]); R();}
      if(Char2[x]=='S'){Serial.print(Char2[x]); S();}
      if(Char2[x]=='T'){Serial.print(Char2[x]); T();}
      if(Char2[x]=='U'){Serial.print(Char2[x]); U();}
      if(Char2[x]=='V'){Serial.print(Char2[x]); V();}
      if(Char2[x]=='W'){Serial.print(Char2[x]); W();}
      if(Char2[x]=='X'){Serial.print(Char2[x]); X();}
      if(Char2[x]=='Y'){Serial.print(Char2[x]); Y();}
      if(Char2[x]=='Z'){Serial.print(Char2[x]); Z();}
    }
    //Restart the program.
    Serial.println(""); Serial.println(""); Serial.println(" - TRANSLATION COMPLETED - ");
    Serial.println("");
    Serial.println("Press 'change' to choose other character and 'select' to save it.");
    i=0; j=0;
    Serial.println("A");
    delay(300);
  }
}

//Here are the functions used to create every character function.

void dot(){
  digitalWrite(buzzer,1);
  delay(100);
  digitalWrite(buzzer,0);
  delay(100);
}

void hyphen(){
  digitalWrite(buzzer,1);
  delay(300);
  digitalWrite(buzzer,0);
  delay(100);
}

/* Here are the functions which run the morse transtation for each character.
You can add more functions in order to employ more characters like numbers. */

void A(){
  dot(); delay(200);
  hyphen(); delay(200);
}

void B(){
  hyphen(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
}

void C(){
  hyphen(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
}

void D(){
  hyphen(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
}

void E(){
  dot(); delay(200);
}

void F_(){
  dot(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
}

void G(){
  hyphen(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
}

void H(){
  dot(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
}

void I(){
  dot(); delay(200);
  dot(); delay(200);
}

void J(){
  dot(); delay(200);
  hyphen(); delay(200);
  hyphen(); delay(200);
  hyphen(); delay(200);
}

void K(){
  hyphen(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
}

void L(){
  dot(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
}

void M(){
  hyphen(); delay(200);
  hyphen(); delay(200);
}

void N(){
  hyphen(); delay(200);
  dot(); delay(200);
}

void O(){
  hyphen(); delay(200);
  hyphen(); delay(200);
  hyphen(); delay(200);
}

void P(){
  dot(); delay(200);
  hyphen(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
}

void Q(){
  hyphen(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
}

void R(){
  dot(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
}

void S(){
  dot(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
}

void T(){
  hyphen(); delay(200);
}

void U(){
  dot(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
}

void V(){
  dot(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
}

void W(){
  dot(); delay(200);
  hyphen(); delay(200);
  hyphen(); delay(200);
}

void X(){
  hyphen(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
}

void Y(){
  hyphen(); delay(200);
  dot(); delay(200);
  hyphen(); delay(200);
  hyphen(); delay(200);
}

void Z(){
  hyphen(); delay(200);
  hyphen(); delay(200);
  dot(); delay(200);
  dot(); delay(200);
}

Follow me on social media as @yurrder to stay up to date with all the news about the blog and other projects.

Comments

Post a Comment