/*
The timing in Morse code is based around the length of one "dit"
(or "dot" if you like). From the dit length we can derive the length
of a "dah" (or "dash") and the various pauses:
Dit: 1 unit
Dah: 3 units
Intra-character space (the gap between dits and dahs within a character): 1 unit
Inter-character space (the gap between the characters of a word): 3 units
Word space (the gap between two words): 7 units
https://morsecode.world/international/timing.html
*/
const int UNIT_TIME = 150; // sets Morse speed
const int BUZZ_PIN = 8;
const int LED_PIN = 9;
const int DOT_TIME = UNIT_TIME;
const int DASH_TIME = UNIT_TIME * 3;
const int SYMBOL_SPACE = UNIT_TIME;
const int CHAR_SPACE = UNIT_TIME * 3;
const int WORD_SPACE = UNIT_TIME * 7;
void writeDot() {
digitalWrite(LED_PIN, HIGH);
tone(BUZZ_PIN, 1000);
delay(DOT_TIME);
digitalWrite(LED_PIN, LOW);
noTone(BUZZ_PIN);
delay(SYMBOL_SPACE);
}
void writeDash() {
digitalWrite(LED_PIN, HIGH);
tone(BUZZ_PIN, 1000);
delay(DASH_TIME);
digitalWrite(LED_PIN, LOW);
noTone(BUZZ_PIN);
delay(SYMBOL_SPACE);
}
void writeCharSpc() {
delay(CHAR_SPACE);
}
void writeWordSpc() {
delay(WORD_SPACE);
}
void setup() {
//Set pins to OUTPUT
pinMode(BUZZ_PIN, OUTPUT);
pinMode (9, OUTPUT);
}
void loop() {
// write "S"
writeDash();
writeDash();
writeDash();
// next letter
writeCharSpc();
// write "O"
writeDot();
writeDot();
writeDot();
// next letter
writeCharSpc();
// write "S"
writeDash();
writeDash();
writeDash();
// next word
writeWordSpc();
}