Skip to content

Commit

Permalink
Day 04
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepMartiElias committed Dec 3, 2024
1 parent 0d48530 commit 96d20d8
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 4 deletions.
3 changes: 1 addition & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ hide:
1. Let's see your graphic design skills

!!! example annotate "4. Talk morse to me (1)"
Day 04
[](solutions/04/04.md)
[Day 04](solutions/04/04.md)
1. -... .- .-. -.. ..- .. -. ---

!!! warning annotate "5. Jingle Bells! (1)"
Expand Down
277 changes: 277 additions & 0 deletions docs/solutions/04/04.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
# Day four:

Communicate using morse code using the Barduino.

dot dot dash... Use your resources to encode a message, and try to decode the rest of messages!

Here is my solution, using the LED and the buzzer. Try to do it yourself first, and check this one if you don't manage. :winking_face:

## Possible solution!

### Arduino code

```c++
const int led = 48;
const int buzzer = 14;
const int freq = 800;

const int wait = 200; // Duration of a unit

String message = "Bon dia Pere";

void setup() {
pinMode(led, OUTPUT);
ledcAttachPin(buzzer, 1);
//pinMode(buzzer, OUTPUT);
Serial.begin(115200);
delay(2000); //wait 2 seconds to start transmitting

for (int i = 0; i < message.length(); i++) {
char letter = toLowerCase(message[i]);
Serial.print(letter);
morse(letter);
delay(wait * 2); // wait 3 units if time after each character (1 in each letter and 2 here)

}
}

void loop() {
}

void dot() {
digitalWrite(led, HIGH);
ledcWriteTone(1, freq);
delay(wait);
digitalWrite(led, LOW);
ledcWriteTone(1, 0);
delay(wait); //space between parts of a letter
}

void dash() {
digitalWrite(led, HIGH);
ledcWriteTone(1, freq);
delay(wait * 3); // A dash are 3 dots
digitalWrite(led, LOW);
ledcWriteTone(1, 0);
delay(wait); //space between parts of a letter
}

void morse(char letter) {
switch (letter) {
case 'a':
dot();
dash();
break;
case 'b':
dash();
dot();
dot();
dot();
break;
case 'c':
dash();
dot();
dash();
dot();
break;
case 'd':
dash();
dot();
dot();
break;
case 'e':
dot();
break;
case 'f':
dot();
dot();
dash();
dot();
break;
case 'g':
dash();
dash();
dot();
break;
case 'h':
dot();
dot();
dot();
dot();
break;
case 'i':
dot();
dot();
break;
case 'j':
dot();
dash();
dash();
dash();
break;
case 'k':
dash();
dot();
dash();
break;
case 'l':
dot();
dash();
dot();
dot();
break;
case 'm':
dash();
dash();
break;
case 'n':
dash();
dot();
break;
case 'o':
dash();
dash();
dash();
break;
case 'p':
dot();
dash();
dash();
dot();
break;
case 'q':
dash();
dash();
dot();
dash();
break;
case 'r':
dot();
dash();
dot();
break;
case 's':
dot();
dot();
dot();
break;
case 't':
dash();
break;
case 'u':
dot();
dot();
dash();
break;
case 'v':
dot();
dot();
dot();
dash();
break;
case 'w':
dot();
dash();
dash();
break;
case 'x':
dash();
dot();
dot();
dash();
break;
case 'y':
dash();
dot();
dash();
dash();
break;
case 'z':
dash();
dash();
dot();
dot();
break;
case '0':
dash();
dash();
dash();
dash();
dash();
break;
case '1':
dot();
dash();
dash();
dash();
dash();
break;
case '2':
dot();
dot();
dash();
dash();
dash();
break;
case '3':
dot();
dot();
dot();
dash();
dash();
break;
case '4':
dot();
dot();
dot();
dot();
dash();
break;
case '5':
dot();
dot();
dot();
dot();
dot();
break;
case '6':
dash();
dot();
dot();
dot();
dot();
break;
case '7':
dash();
dash();
dot();
dot();
dot();
break;
case '8':
dash();
dash();
dash();
dot();
dot();
break;
case '9':
dash();
dash();
dash();
dash();
dot();
break;
case ' ':
delay(wait * 5); //wait 7 units of time if there is a space (1 from the last character, 5 here and 1 in the loop)
default:
break;
}
}
```
## Hero shot
<video controls autoplay loop style="display: block; margin: auto;">
<source src="../../../video/day04.mp4" type="video/mp4">
</video>
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ nav:
- Solutions:
- Day 1: solutions/01/01.md
- Day 2: solutions/02/02.md
#- Day 3: solutions/03/03.md
#- Day 4: solutions/04/04.md
- Day 3: solutions/03/03.md
- Day 4: solutions/04/04.md
#- Day 5: solutions/05/05.md
#- Day 6: solutions/06/06.md
#- Day 7: solutions/07/07.md
Expand Down

0 comments on commit 96d20d8

Please sign in to comment.