On the first day of the technical studies course: “Arduino and Electronics” we focused on learning about the arduino and how to program it. The day was spend on the assignment of displaying (with an LED) morse code of characters typed in the serial monitor of the arduino IDE.
We started to build this “machine” by first learning how to turn a LED on/off and we gradually build this out to a sketch that had all the described capabilities. Underway you were introduced to interesting programming stuff like: global variables, functions, while, if, switch, debugging using the serial monitor, etc.
The final and somewhat complete machine can be downloaded from here. This is just one of the many possible answers to the solution. I have seen some other interesting approaches, eg. this machine build by Deniz Uygur and Meltem Kanatlarli. Which does not use the array and delivers somehow more readable code…
void dot() /* Define a 'dot */
{
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}
void dash() /* Define a ‘dash’ */
{
digitalWrite(ledPin, HIGH);
delay(750);
digitalWrite(ledPin, LOW);
delay(250);
}
void letterpause() /* Insert a pause between letters */
{
delay(750);
}
void wordpause() /* Insert a pause between words */
{
delay(1500);
}
void morse_write_letter_the_ultimate_version(char k)
{
switch (k) {
case ‘a’:
dot();dash();
break;
case ‘b’:
dash(); dot(); dot(); dot();
break;
}
letterpause();
}