/*Play a Morse Code message on the speaker
Rules of Morse Code:
- Dots are 1 'unit' of sound
- Dashes are 3 'units' of sound (3 times as long as a dot)
- Spaces between symbols within the same letter are 1 'unit' of silence
- Spaces between letters are 3 'units' of silence
- Spaces between words are 7 'units' of silence
*/
void setup(){
pinMode(A5,OUTPUT); //Set the speaker as an 'OUTPUT'
}
void loop(){
//S
//dot
tone(A5,500); //play a tone of 500 hertz on the speaker (A5)
delay(300); //wait one 'dot' with the speaker on
noTone(A5); //turn off the sound
delay(300); //wait before the next symbol in a letter
//dot
tone(A5,500); //play a tone of 500 hertz on the speaker (A5)
delay(300); //wait one 'dot' with speaker on
noTone(A5); //turn off the sound
delay(300); //wait before the next symbol in a letter
//dot
tone(A5,500); //play a tone of 500 hertz on the speaker (A5)
delay(300); //wait one 'dot' with speaker on
noTone(A5); //turn off the sound
delay(900); //wait before the next letter in a word
//O
//dash
tone(A5,500); //play a tone of 500 hertz
delay(900); //wait one 'dash' with speaker on
noTone(A5); //turn off the sound
delay(300); //wait before the next symbol in a letter
//dash
tone(A5,500); //play a tone of 500 hertz
delay(900); //wait one 'dash' with the speaker on
noTone(A5); //turn off the sound
delay(300); //wait before the next symbol in a letter
//dash
tone(A5,500); //play a tone of 500 hertz
delay(900); //wait one 'dash' with speaker on
noTone(A5); //turn off the sound
delay(900); //wait before the letter in a word
//S
//dot
tone(A5,500); //play a tone of 500 hertz on the speaker (A5)
delay(300); //wait one 'dot' with the speaker on
noTone(A5); //turn off the sound
delay(300); //wait before the next symbol in a letter
//dot
tone(A5,500); //play a tone of 500 hertz on the speaker (A5)
delay(300); //wait one 'dot' with the speaker on
noTone(A5); //turn off the sound
delay(300); //wait before the next symbol in a letter
//dot
tone(A5,500); //play a tone of 500 hertz on the speaker (A5)
delay(300); //wait one 'dot' with the speaker on
noTone(A5); //turn off the sound
delay(2100); //wait before the next word
}
// (c) 2024 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
It looks like your computer isn't set up to code yet.
Success!
Compile errors:
Serial port monitor:
Input:
Walkthrough Video
Watch the video for a line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!
Challenges
Can you complete the challenges? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!