/*
Morse code generator based on Elegoo lesson 22 for learning how to control the 16x2 LCD display.
This version starts with a splash screen announcing "Your initials in Morse code"
A select button is used to select the number of initials in your name 1, 2 or 3
An enter button is pressed to confirm.
The select button is then pressed to cycle through the alphabet.
The select button is released and Enter button pressed to confirm the letter
This is repeated for number of the letters selected above.
On the last enter the letter string is played in morse and the letters are
shown as morse on the display. This repeats until the Enter button is pressed
The program then loops ready for entry of the next set of initials.
Parts of this code were suggested by ChatGPT
*/
//www.elegoo.com
//2016.12.9
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
LCD RS pin to digital pin 7
LCD Enable pin to digital pin 8
LCD D4 pin to digital pin 9
LCD D5 pin to digital pin 10
LCD D6 pin to digital pin 11
LCD D7 pin to digital pin 12
LCD R/W pin to ground
LCD VSS pin to ground
LCD VCC pin to 5V
10K resistor:
ends to +5V and ground
wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
const int selectButton = 6 ;
const int enterButton = 5 ;
const int Speaker = 13 ;
int numLetters = 0 ;
String wholeString = "" ;
String selectedLetter = "" ;
String morseResult = "" ;
String wholeMorseResult = "" ;
int charCode = 64 ;
const int dotDuration = 100; // duration of a dot in milliseconds
const int dashDuration = dotDuration * 3; // duration of a dash in milliseconds
const int spaceBetweenPartsOfSameLetter = dotDuration; // space between dots and dashes within a letter
const int spaceBetweenLetters = dotDuration * 3; // space between letters
const int spaceBetweenWords = dotDuration * 7; // space between words
const String morseString[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
const char letters[26] = {'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'};
#include <LiquidCrystal.h> // include the library code:
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // initialize the library with the numbers of the interface pins
//################################ getLengthOfString function ####################################
int getLengthOfString () {
int numOfLetters = 0 ;
lcd.setCursor (3 , 1) ; // Set the cursor to lower row 4th character position
lcd.write (" Select! ") ; // print the text "Select btn" to the lcd screen
while (digitalRead (enterButton) == HIGH) { // do this while loop while enterButton is not pressed
lcd.setCursor (0 , 0) ; // Set the cursor to top row fist character position
lcd.write ("Num of letters?") ; // Print the text "Num of letters?" to the lcd screen
while (digitalRead(selectButton) == LOW) { // do this while loop when selectButton is pressed
numOfLetters = numOfLetters + 1 ; // increase the value of numOfLetters by 1
if (numOfLetters >= 4 ) { // if numOfLetters reaches 4 then
numOfLetters = 1 ; // reset numOfLetters to value 1
} // end of if function
lcd.setCursor (0 , 1) ; // Set the cursor to lower row fist character position
lcd.print (numOfLetters) ; // Print the value of numOfLetters to the lcd screen
lcd.setCursor (3 , 1) ; // Set the cursor to lower row 4th character position
lcd.write (" Enter! ") ; // Print the text " Enter btn" to the lcd screen
//Serial.print (" numLetters =") ;
//Serial.println (numOfLetters) ;
delay (1000) ;
} // repeat the selectButton while loop while selectButton is pressed
delay (1000) ;
} // repeat the enterButton loop while enterButton is not pressed
return numOfLetters ; // return the value of numOfLetters to the calling code
}
//################################ getChosenCharacter function #######################################
String getChosenCharacter () {
String letter = "" ;
charCode = 64 ;
lcd.setCursor (3 , 1) ; // Set the cursor to lower row 4th character position
lcd.write (" Select! ") ; // print the text "Select btn" to the lcd screen
while (digitalRead (enterButton) == HIGH) { // do this while loop while enterButton is not pressed
lcd.setCursor (0 , 0) ; // Set the cursor to top row fist character position
lcd.write ("Letters ") ; // Print the text "Num of letters?" to the lcd screen
while (digitalRead(selectButton) == LOW) { // do this while loop when selectButton is pressed
charCode = charCode + 1 ; // increase the value of charCode by 1
if (charCode >= 91 ) { // if charCode reaches 88 ("Z") then
charCode = 64 ; // reset charCode to value 64
} // end of if function
lcd.setCursor (0 , 1) ; // Set the cursor to lower row fist character position
lcd.write (charCode) ; // Print the value of letter to the lcd screen
lcd.setCursor (3 , 1) ; // Set the cursor to lower row 4th character position
lcd.write (" Enter! ") ; // Print the text " Enter btn" to the lcd screen
delay (400) ;
} // repeat the selectButton while loop while selectButton is pressed
delay (500) ;
} // repeat the enterButton loop while enterButton is not pressed
letter = char (charCode);
return letter ; // return the value of letter to the calling function
}
//############################## getMorseCodeString function ###############################################
String getMorseCodeString (String myLetter) {
String str = "" ;
char letChar = myLetter.charAt (0) ; // give the character variable 'letchar' the value character value of 'myLetter' ( uses the charat function)
int index = tolower(letChar) - 'a'; // convert the character 'letchar' to lower case and subtract the code for 'a' from it
// give its resultant code number to the integer variable 'index'
str = morseString[index] ; // give the value of array 'morseString'[index] to the string variable 'str'
return str ; // return the value 'str' to the calling function
}
//############################### playAsMorse function ###############################################
void playAsMorse (String string) {
//code here
lcd.write (" ") ;
lcd.setCursor (0 , 1) ;
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
lcd.print (c) ;
switch (c) {
case '.':
// output a 'dot' sound
digitalWrite ( Speaker , HIGH ) ;
delay (dotDuration) ;
digitalWrite ( Speaker , LOW ) ;
delay(spaceBetweenPartsOfSameLetter);
break;
case '-':
// output a 'dash' sound
digitalWrite ( Speaker , HIGH ) ;
delay (dashDuration) ;
digitalWrite ( Speaker , LOW ) ;
delay(spaceBetweenPartsOfSameLetter);
break;
case ' ':
// output a space between letters or words
if (string.charAt(i - 1) == ' ') {
// output a space between words
delay(spaceBetweenWords);
} else {
// output a space between letters
delay(spaceBetweenLetters);
}
break;
}
}
}
//################################# createMorseString function ###################################
void createMorseString () {
//code here
wholeMorseResult = "" ;
lcd.clear () ; // Clear the display
numLetters = getLengthOfString () ; // call the function getLengthOfString and pass its return value to numLetters
delay (1000) ;
wholeString = "" ;
for (int x = 1 ; x <= numLetters ; x++) {
lcd.setCursor (7 , 0) ;
lcd.write (" ") ;
lcd.setCursor (8 , 0) ;
lcd.print (x) ;
lcd.write (" of ") ;
lcd.print (numLetters) ;
//Serial.print (" x =") ;
//Serial.println (x) ;
selectedLetter = getChosenCharacter () ; // call the function getChosenCharacter and pass the result to selectedLetter
morseResult = getMorseCodeString (selectedLetter) ; // call the function getMorseCodeString, passing the argument of selectedLetter and return the result to morseResult
wholeString = wholeString + selectedLetter ;
wholeMorseResult = wholeMorseResult + morseResult + " " ;
delay (1000) ;
}
lcd.clear () ; // Clear the display
lcd.setCursor (5 , 0) ;
lcd.write ("in Morse is") ;
lcd.setCursor (0 , 0) ;
lcd.print (wholeString) ;
lcd.setCursor (0 , 1) ;
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ SET UP ///////////////////////////////////////////////////
void setup() {
pinMode (selectButton , INPUT_PULLUP) ;
pinMode (enterButton , INPUT_PULLUP) ;
pinMode (Speaker , OUTPUT ) ;
// set up the LCD's number of columns and rows:
lcd.begin(16, 2); //Initialise the display
lcd.clear () ; // Clear the display
lcd.setCursor (0 , 0) ;
lcd.write ("Your initials") ;
lcd.setCursor (0 , 1) ;
lcd.write ("in Morse code!") ;
delay (5000) ;
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ LOOP ////////////////////////////////////////////////////////
void loop() {
createMorseString () ;
while (digitalRead (enterButton) == HIGH) {
// repeat this while loop until pressing enter button
lcd.setCursor (0 , 1) ;
lcd.write (" ") ;
playAsMorse (wholeMorseResult) ;
delay (700) ;
}
delay (1000) ;
}