Components and supplies
Buzzer
Arduino UNO
Resistor 10k ohm
Resistor 221 ohm
LCD 16 x 2 with I2C
Button
Jumper wires (generic)
USB-A to B Cable
Apps and platforms
Arduino IDE
Project description
Code
Morse Code Converter - Programming
arduino
1#include <LiquidCrystal_I2C.h> 2#include <Wire.h> 3 4int timeStart = 0; 5int buttonMorse = 13; 6int buttonEnd = 12; 7int buttonMorseState = 0; 8int buttonEndState = 0; 9int timeButtonPressed = 0; 10int buttonStartEnd = 11; 11int buttonStartEndState= 0; 12int cont = 0; 13int timeButtonNotPressed_Start = 0; 14int timeButtonNotPressed = 0; 15int buzzer = 10; 16int lcdPos = 0; 17 18LiquidCrystal_I2C lcd(0x3F,16,2); 19 20// morse character 21String character = ""; 22// list of letters and numbers 23String lettersAndNumbers[36]={"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","0","1","2","3","4","5","6","7","8","9"}; 24// respective morse code for each letter and number 25String morseCode[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..", 26"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."}; 27 28void setup() { 29 // put your setup code here, to run once: 30 pinMode(buttonMorse, INPUT); 31 pinMode(buttonEnd, INPUT); 32 pinMode(buttonStartEnd, INPUT); 33 pinMode(buzzer, OUTPUT); 34 35 lcd.init(); // Inicialize communication with the display 36 lcd.backlight(); // Turn the display lights on 37 lcd.clear(); // Clean the display 38 lcd.setCursor(0,0); 39 Serial.begin(9600); 40 41} 42 43void loop() { 44 if (lcdPos%32==0) { 45 // Everytime all the 32 positions of the display are being used, clean it up 46 lcd.clear(); 47 } 48 buttonStartEndState = digitalRead(buttonStartEnd); 49 // Everytime the button thats start and pauses the program is pressed, the program increases 'cont' in one 50 if(buttonStartEndState){ 51 cont++; 52 delay(500); 53 } 54 // If 'cont' is even, the program starts, if it's odd, the program pauses 55 if(cont%2==1){ 56 // When the program is running, the time that the button of the morse code is not pressed is count 57 timeButtonNotPressed = millis(); 58 // If the button is not pressed for 3 seconds, a space is done 59 if (timeButtonNotPressed % 3000 == 0){ 60 Serial.print(" "); 61 lcd.print(" "); 62 lcdPos ++; 63 delay(300); 64 } 65 buttonMorseState = digitalRead(buttonMorse); 66 buttonEndState = digitalRead(buttonEnd); 67 // timeStart restart before press the button 68 timeStart = millis(); 69// while the button is pressed the time is count 70 while (buttonMorseState){ 71 buttonMorseState = digitalRead(buttonMorse); 72 tone(buzzer, 800); 73 } 74 noTone(buzzer); 75// finalize the time and store in timeButtonPressed 76 timeButtonPressed = millis() - timeStart; 77// if the button is pressed for less than 200 miliseconds and more than 5 miliseconds, so the character is "." 78 if (timeButtonPressed < 200 and timeButtonPressed > 5){ 79 character = character + "."; 80 // the time that the button is not pressed is turned to 0 81 timeButtonNotPressed = timeButtonNotPressed - timeButtonNotPressed; 82 } 83 // if the button is pressed for more than 200 seconds, so the character is "-" 84 if (timeButtonPressed > 200) { 85 character = character + "-"; 86 // the time that the button is not pressed is turned to 0 87 timeButtonNotPressed = timeButtonNotPressed - timeButtonNotPressed; 88 } 89 // if the button that ends the character is pressed 90 if (buttonEndState){ 91 // compare the list of characters with the list of morse 92 for(int i = 0;i < 36; i++){ 93 // if the character made is equal to the respective morse code, print the character that have the same index 94 if(morseCode[i]==character){ 95 Serial.print(lettersAndNumbers[i]); 96 lcd.print(lettersAndNumbers[i]); 97 // Position of the display's cursor is increased in one 98 lcdPos ++; 99 // If the position is in the end of the first line, everytime lcdPos is divisible by 16, the cursor go to the second line, position 0,1 100 if(lcdPos%16==0){ 101 lcd.setCursor(0,1); 102 } 103 } 104 } 105 // character is restarted 106 character = ""; 107 delay(500); 108 } 109 } 110}
Downloadable files
Circuit
Circuit
Comments
Only logged in users can leave comments
Morse Code Converter | Arduino Project Hub