Hello Arduino friends,
I wanted to expand on the given task in the tutorial, and this is what I came up with (code below).
You enter a string into a char array and let the Arduino parse it as morse code via the status LED.
But my code seems unnecessarily long and redundant.
I have an idea to shorten it intelligently, but I don't know if thats possible:
If you look at the switch expression and the corresponding morseX() functions, a simple way to cut down the repetitive text scheme would be to insert the currently selected char from the for loop into the actual code.
So for example, if my for loop is iterating through the char array "PARIS", it would start with 'P' and then insert this letter into all the code snippets automatically to create the correct switch response.
Do you understand what I mean? I basically want to cut down the source code, because I feel like there would be a way more compact and maybe even efficient way of coding this.
Any suggestions? ![]()
Thanks!!
//Assigning the status LED a variable
const unsigned int LED_PIN = 13;
//Declaring the variables, such as WpM (Words per Minute) and based on that, dits and dahs (which are the basic symbols of Morse Code).
const unsigned int WpM = 5;
const unsigned int DIT = 1200/WpM;
const unsigned int DAH = 3*DIT;
//This is the char array that stores the String to be morsed (IMPORTANT: String must be UPPERCASE).
char sen[] = "PARIS";
void setup() {
//Setting the Status LED
pinMode(LED_PIN, OUTPUT);
}
void loop() {
//Main function to morse the whole char array one by one.
morseAll();
}
void morseAll() {
//Looping through the whole array.
for (int i = 0; i < sizeof(sen); i++) {
/*For each letter (A-Z) there is a corresponding function.
For SPACE (' ') the pause is longer 'halt()', as specified in Morse Code standards.
If there is an undefined symbol in the array, the LED will hyperventilate().
After each letter, the LED will pause() as specified in the Morse Code standards.
It's important, that the letters are uppercase.
*/
switch (sen[i]) {
case 'A':
morseA();
break;
case 'B':
morseB();
break;
case 'C':
morseC();
break;
case 'D':
morseD();
break;
case 'E':
morseE();
break;
case 'F':
morseF();
break;
case 'G':
morseG();
break;
case 'H':
morseH();
break;
case 'I':
morseI();
break;
case 'J':
morseJ();
break;
case 'K':
morseK();
break;
case 'L':
morseL();
break;
case 'M':
morseM();
break;
case 'N':
morseN();
break;
case 'O':
morseO();
break;
case 'P':
morseP();
break;
case 'Q':
morseQ();
break;
case 'R':
morseR();
break;
case 'S':
morseS();
break;
case 'T':
morseT();
break;
case 'U':
morseU();
break;
case 'V':
morseV();
break;
case 'W':
morseW();
break;
case 'X':
morseX();
break;
case 'Y':
morseY();
break;
case 'Z':
morseZ();
break;
//For SPACE (' ') the pause is longer 'halt()', as specified in Morse Code standards.
case ' ':
halt();
break;
//If there is an undefined symbol in the array, the LED will hyperventilate().
default:
hyperventilate();
}
//After each letter, the LED will pause() as specified in the Morse Code standards.
pause();
}
}
//Morse functions for each letter (A-Z).
void morseA() {
dit();
dah();
}
void morseB() {
dah();
dit();
dit();
dit();
}
void morseC() {
dah();
dit();
dah();
dit();
}
void morseD() {
dah();
dit();
dit();
}
void morseE() {
dit();
}
void morseF() {
dit();
dit();
dah();
dit();
}
void morseG() {
dah();
dah();
dit();
}
void morseH() {
dit();
dit();
dit();
dit();
}
void morseI() {
dit();
dit();
}
void morseJ() {
dit();
dah();
dah();
dah();
}
void morseK() {
dah();
dit();
dah();
}
void morseL() {
dit();
dah();
dit();
dit();
}
void morseM() {
dah();
dah();
}
void morseN() {
dah();
dit();
}
void morseO() {
dah();
dah();
dah();
}
void morseP() {
dit();
dah();
dah();
dit();
}
void morseQ() {
dah();
dah();
dit();
dah();
}
void morseR() {
dit();
dah();
dit();
}
void morseS() {
dit();
dit();
dit();
}
void morseT() {
dah();
}
void morseU() {
dit();
dit();
dah();
}
void morseV() {
dit();
dit();
dit();
dah();
}
void morseW() {
dit();
dah();
dah();
}
void morseX() {
dah();
dit();
dit();
dah();
}
void morseY() {
dah();
dit();
dah();
dah();
}
void morseZ() {
dah();
dah();
dit();
dit();
}
//Morse functions for dits and dahs, as well as pause(), halt() and hyperventilate().
void dit () {
digitalWrite(LED_PIN, HIGH);
delay(DIT);
digitalWrite(LED_PIN, LOW);
delay(DIT);
}
void dah () {
digitalWrite(LED_PIN, HIGH);
delay(DAH);
digitalWrite(LED_PIN, LOW);
delay(DIT);
}
void pause() {
delay(DAH);
}
void halt() {
delay(7*DIT);
}
void hyperventilate() {
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH);
delay(80);
digitalWrite(LED_PIN, LOW);
delay(80);
}
}