Arduino: build Morse translators - how it works
Related Videos: Arduino Morse Code Translator (May 2024).
In this practical tip you will learn how you can easily build a Morse translator with your Arduino.
Building Morse translators with Arduino - this is needed
First of all, of course, you need an Arduino.
- You should also get an active buzzer and an LED.
- Connect the buzzer to pin 9 and the LED to pin 13.
Arduino: program Morse translator
Here we have programmed a small sample code for you, which you are welcome to use. Simply upload it to the Arduino and start the serial monitor.
- #define debug true
- int ledPin = 13;
- int speakerPin = 9;
- int pitch1 = 140;
- int pitch2 = 140;
- int dot = 50;
- int dash = 3 * dot;
- String morseATable [] = {".-", "-...", "-.-.", "- ..", ".", "..-.", "-.", ". ... ", " .. ", " .--- ", " _._ ", " .- .. ", " - ", " -. ", " --- ", " .-- . ", " --.- ", " .-. ", " ... ", " - ", " ..- ", " ...- ", " .-- ", " -..- ", " -.-- ", " - .. "};
- String morseNTable [] = {"-----", ".----", "..---", "...--", "....-", "... .. ", " -.... ", " --... ", " --- .. ", " ----. "};
- void setup () {
- pinMode (ledPin, OUTPUT);
- pinMode (speakerPin, OUTPUT);
- Serial.begin (9600);
- if (debug) {
- Serial.println ( "\ nstart");}
- }
- void loop () {
- if (true) {
- convert2Morse (Serial.readString ());
- }
- }
- void convert2Morse (String message) {
- for (int pos = 0; pos <message.length (); pos ++) {
- if (debug) {
- Serial.print (message.charAt (pos));
- Serial.print ("");
- }
- if ((message.charAt (pos)> = 97) && (message.charAt (pos) <= 122)) {
- morseLED (morseATable [message.charAt (pos) -97]);}
- if ((message.charAt (pos)> = 48) && (message.charAt (pos) <= 57)) {
- morseLED (morse table [message [pos] -48]);}
- if (message.charAt (pos) == 32) {
- morseLED ("");}
- if (debug) {
- Serial.println ();
- }
- }}
- void morseLED (String sequence) {
- for (int pos = 0; pos <sequence.length (); pos ++) {
- FlashLED (sequence.charAt (pos));}
- delay (2 * dot);
- }
- void flashLED (char dotOrDash) {
- if (debug) {
- Serial.print (dotOrDash);}
- if (dotOrDash == 46) {
- digitalWrite (ledPin, HIGH);
- tone (speakerPin, pitch1);
- delay (dot);
- digitalWrite (ledPin, LOW);
- noTone (speakerPin);
- delay (dot);
- } else if (dotOrDash == 45) {
- digitalWrite (ledPin, HIGH);
- tone (speakerPin, pitch2);
- delay (dash);
- digitalWrite (ledPin, LOW);
- noTone (speakerPin);
- delay (dot);
- } else if (dotOrDash == 32) {
- delay (dot);}
- }
In the next practical tip, we will show you how you can use a GSM module on the Arduino.