From 96d20d8d73f4429a679ce38e98ab1c43924587e3 Mon Sep 17 00:00:00 2001 From: josepmarti Date: Tue, 3 Dec 2024 23:46:20 +0100 Subject: [PATCH] Day 04 --- docs/index.md | 3 +- docs/solutions/04/04.md | 277 ++++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 4 +- 3 files changed, 280 insertions(+), 4 deletions(-) create mode 100644 docs/solutions/04/04.md diff --git a/docs/index.md b/docs/index.md index 3179b3f..81041a0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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)" diff --git a/docs/solutions/04/04.md b/docs/solutions/04/04.md new file mode 100644 index 0000000..47e9580 --- /dev/null +++ b/docs/solutions/04/04.md @@ -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 + + \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 2216665..797d127 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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