# Arduino morse code and functional programming
###### tags: `IB` `Computer Science` `Arduino`
In the previous lesson [Starting with Arduino. Installing and first blink](/-TGF1lqLROSs9UcvU0BuBQ) we made a blink. Now we can do a more complex program.
## First code "A"
The first code is after the blink. We need to consider that we have a dot and a dash. And they have durations that _depend_ on each other.
The idea is that we're going to blink in morse code.
For this I need to understand the proportions of the morse code. We can find it in [wikipedia](https://en.wikipedia.org/wiki/Morse_code).

{%youtube jPTS-IiYG8Y%}
Each student is going to use their own names, and we're going to start by the first letter. In this example I'm going to use A.
First we get rid of the comments of the blink so I have only the pure code. You can use this as an example
:::warning
:warning: Remember that if you copy and paste the code from here, you need to delete all the previous code from your sketch. :warning:
If you don't you might find some errors like "I have setup written twice and I don't know which one execute"
:::
```cpp=
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
```
Now we need to think. If we want to have an A, we need to do a dot, then a dash, then a space between letters.
Here you have a reminder

Now these would be the steps that we need to do

But since we don't know how to do a dot we need to lower the level of abstraction a little bit. For a dot we need to light up for a lenth of a dot and then we need to light down for a length of a dot because we're in the letter yet. Then we need to light up for three dots to do the dash and then light down for the duration of three dots. If we do this in a shape of a diagram it would be like this:

We can use for example 600 milliseconds as a length for a dot (use another in your sketch). So a dash will be 1800.
```cpp=
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
//dot
digitalWrite(LED_BUILTIN, HIGH);
delay(600);
digitalWrite(LED_BUILTIN, LOW);
//space inside a letter
delay(600);
//dash
digitalWrite(LED_BUILTIN, HIGH);
delay(1800);
digitalWrite(LED_BUILTIN, LOW);
//space between letters
delay(1800);
}
```
:::info
I used here some comments to know what I'm doing using `//`
:::
The problem of this is that if we want to change the value of the length of the dot, we would have to change 4 lines if we have one letter, but many letters, then... oh boy we have to change a lot. So we're going to **refactor** to add a variable.
## Refactoring and adding a variable
//talk about refactoring and manteinability (maintenance) of code
We're going to use a variable. dotDuration (that can be changed to any other name). Since it's going to be read in all the code, we're going to define it as a global variable. (outside of the setup and the loop)
Let's say that our name is Wenceslao. So the first letter is W. So we need to state a a dot and two dashes.
You have the code in the details.
:::info
There are naming conventions for the variables that depend on the programming language and the context where you are. Probably at some point you will have a small note on this.
:::
:::spoiler
```cpp=
//we write the variable
int dotDuration = 500;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
//W
//dot
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dotDuration);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(dotDuration);
//dash
digitalWrite(LED_BUILTIN, 1); // turn the LED on (HIGH is the voltage level)
delay(dotDuration*3);
digitalWrite(LED_BUILTIN, 0); // turn the LED off by making the voltage LOW
delay(dotDuration);
//dash
digitalWrite(LED_BUILTIN, 1); // turn the LED on (HIGH is the voltage level)
delay(dotDuration*3);
digitalWrite(LED_BUILTIN, 0); // turn the LED off by making the voltage LOW
delay(dotDuration*3);
//wenceslao
}
```
:::
:::info
If you see in detail in some `digitalWrite` the code uses `HIGH`, `LOW`, `0` and `1` and `HIGH` is the same as writing `1` and `LOW` is the same as writing `0`
:::
## Functions
If we would need to add all this to all the name of **Wenceslao** would need to write _a lot_ of time. To save this, we're going to define **functions**.
To create a function in C++ we need to state out of the setup and loop curly braces a line that is "void $NameOfTheFunction()". (This is a simplification)
In our case we're going to use "morseDot()" and "morseDash()".
MorseDot will do the dot and morse dash will do the, ehem, the dash. So if we have a W we will have to **call** the function
The code is this in the details
:::spoiler
```cpp=
int dotDuration = 500;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void morseDot() {
digitalWrite(LED_BUILTIN, 1); // turn the LED on (HIGH is the voltage level)
delay(dotDuration);
digitalWrite(LED_BUILTIN, 0); // turn the LED off by making the voltage LOW
delay(dotDuration);
}
void morseDash() {
digitalWrite(LED_BUILTIN, 1); // turn the LED on (HIGH is the voltage level)
delay(dotDuration*3);
digitalWrite(LED_BUILTIN, 0); // turn the LED off by making the voltage LOW
delay(dotDuration);
}
// the loop function runs over and over again forever
void loop() {
//W
//dot
morseDot();
//dash
morseDash();
//dash
morseDash();
delay(dotDuration*2);
}
```
:::
### Exercise:
Write your own name in morse code using these functions.
## Grouping functions
If we want to have a more complex diagram we are going to repeat the same thing again, we're going to have a lot of morseDot() and morseDash() calls all over. So we are going create a function that calls the other functions. In the case of A, we're going to create morseA
:::info
Other implementations are possible
:::
```cpp=
void morseW() {
//W
//dot
morseDot();
//dash
morseDash();
//dash
morseDash();
delay(dotDuration*2);
}
```
Now the loop will look something like this:
```cpp=
void loop() {
morseD();
morseA();
morseV();
morseI();
morseD();
delay(dotDuration*4);
}
```
## Your first library
We want to reuse this code for everybody (people do it )

## Reference code
https://github.com/DavidMenCam/Arduino/blob/main/morse_3/morse_3.ino
