Morse Code - Serial Monitor Input to Morse Code
The project will wait for an input, and then turn it into Morse Code! (Alphabet only) Component List:- 1 x Arduino Uno R3
- 1 x Buzzer
- Connect the positive (longer) leg of the buzzer to digital pin 6 on the Arduino.
- Connect the negative (shorter) leg of the buzzer to the GND pin on the Arduino (or breadboard).
CODE:
String keyWord=""; //Variable to store the current keyword entered
int buzzer=6; //setting the buzzer pin
int longBlink=400; //all these values can be easily customized
int shortBlink=50;
int letterDelay=350; //if the delay is too fast the buzz is not heard clearly
char letters[]= {'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'}; //This is so we can get the index number of the letter
//This is the Alphabet morse code, it can easily be extended further
char morseCode[26][4] = {
{'1','2','0','0'}, //A
{'2','1','1','1'}, //B
{'2','1','2','1'}, //C
{'2','1','1','0'}, //D
{'1','0','0','0'}, //E
{'1','1','2','1'}, //F
{'2','2','1','0'}, //G
{'1','1','1','1'}, //H
{'1','1','0','0'}, //I
{'1','2','2','2'}, //J
{'2','1','2','0'}, //K
{'1','2','1','1'}, //L
{'2','2','0','0'}, //M
{'2','1','0','0'}, //N
{'2','2','2','0'}, //O
{'1','2','2','1'}, //P
{'2','2','1','2'}, //Q
{'1','2','1','0'}, //R
{'1','1','1','0'}, //S
{'2','0','0','0'}, //T
{'1','1','2','0'}, //U
{'1','1','1','2'}, //V
{'1','2','2','0'}, //W
{'2','1','1','2'}, //X
{'2','1','2','2'}, //Y
{'2','2','1','1'}, //Z
};
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buzzer,OUTPUT); //buzzer pin set to output
}
void loop()
{
while (Serial.available() == 0) //Wait for a user input
{
}
keyWord=Serial.readString(); //Parse the value as a string
MorseCodeMatch(keyWord); //send to our function
}
void MorseCodeMatch(String keyWord)
{
keyWord.toLowerCase(); //to avoid any issues with case sensitivity
int wordLen=keyWord.length()+1;
char keyChar[wordLen]; //create a character type value array, of the length of the word we are processing
keyWord.toCharArray(keyChar,wordLen); //Break the string into characters
for (int i=0; i<wordLen;i++) //go through each letter of the kerWord
for (int j=0; j<sizeof(letters);j++) //go through the alphabet to find that letter.
if(keyChar[i]==letters[j]) //trying to match it
blinkCodeLED(morseCode[j]); //send the letter to be blinked
Serial.println("morseCode Function Done");
}
void blinkCodeLED(char morseMessage[])
{
for (int i=0;i<4;i++) //Length of the Morse code per letter is 4, so loop 4 times
{
if(morseMessage[i]=='1') //if Dot
{
digitalWrite(LED_BUILTIN, HIGH);
tone(buzzer, 5000, shortBlink);
Serial.print("."); //Dot
}
else if(morseMessage[i]=='2') //if Dash
{
digitalWrite(LED_BUILTIN, HIGH);
tone(buzzer, 5000, longBlink);
Serial.print("-"); //Dash
}
digitalWrite(LED_BUILTIN, LOW); //Reset LED and put a delay between letters
delay(letterDelay);}
Serial.print(" "); //Space
delay(500);
}
Looking to build this project? Check out our custom, Bahraini Assembled, starter kit here!
https://www.bitware.store/product/arduino-uno-r3-compatible-with-arduino-ide/
[bitware_arabic_portfolio_content]
Project Details | |
| Level: | Intermediate (3/5) |
| Category: | Loops & Logic |
| Platform: | Arduino |
| Creator: | BitWare Store |