Lesson 29: Morse decoder
Listen to Morse code
- In lesson 15 the Arduino sent Morse code with a light. Now it reads Morse code from a key (a button).
- It measures how long each press is: a short press is a dot, a long press is a dash.
- A long pause means the letter is finished, and the Arduino turns the dots and dashes into a letter.
Words for this lesson
| Word | Meaning |
|---|---|
| decode | turn a code back into normal text |
| dot / dash | a short / long signal |
| String | a text that can grow: code = code + "." |
| pause | a time with no signal |
The timing
| What happens | Time | Meaning |
|---|---|---|
| key down | under 300 ms | dot . |
| key down | 300 ms or more | dash - |
| key up | under 800 ms | same letter, more symbols |
| key up | over 800 ms | the letter is finished |
Some letters
| Letter | Code | Letter | Code |
|---|---|---|---|
| E | . |
T | - |
| A | .- |
N | -. |
| S | ... |
O | --- |
void setup() {
Serial.begin(9600);
String code = "";
code = code + ".";
code = code + "-";
Serial.println(code);
Serial.println(code == ".-");
}
void loop() {
}
- The text grows one symbol at a time: it prints
.-, and then 1, becausecodeis now the same as".-"(the letter A).
In the exam
Morse code is a code for characters: each letter has its own pattern of dots and dashes. A computer's character set works the same way, but with binary numbers.
| Letter | Morse | ASCII |
|---|---|---|
| N | -. |
78 = 01001110 |
| S | ... |
83 = 01010011 |
- 0478/12/F/M/24 Q1(b): the ASCII denary number for 'N' is 78. (ii) Give it in binary: 01001110. (iii) Explain how the word 'RED' is represented using a character set: each character has its own unique binary number, and the codes for R, then E, then D are stored in sequence.
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
In lesson 15 the Arduino sent Morse code. Now it listens. The sketch already measures how long each press of the key (pin 2) is. Finish it: a press shorter than 300 ms is a dot, so print .; a longer press is a dash, so print -. Use Serial.print (no new line), so the symbols stand side by side.
点击“运行”查看此处输出。
难度 5/5 · 今日首次完成可获 400 金币及少量额外奖励
Now turn the symbols into letters. Add each symbol to the text code instead of printing it. When the key has been up for more than 800 ms, the letter is finished: print decode(code) and empty code again. Finish decode too: E is ., T is -, S is ..., O is ---, A is .- and N is -.. The test taps S O S.
点击“运行”查看此处输出。