Challenge - 5 Problems
Tone Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Arduino sketch?
Consider the following Arduino code that plays a simple melody using tone(). What will be the behavior when this code runs on a board connected to a speaker on pin 8?
Arduino
const int speakerPin = 8; int melody[] = {262, 294, 330, 349}; int noteDurations[] = {4, 4, 4, 4}; void setup() { for (int thisNote = 0; thisNote < 4; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(speakerPin, melody[thisNote], noteDuration); delay(noteDuration * 1.3); } } void loop() { // no repeat }
Attempts:
2 left
💡 Hint
Think about how tone() and delay() work together to play notes sequentially.
✗ Incorrect
The code plays each note in the melody array for a duration calculated from noteDurations. The delay after tone() ensures notes don't overlap. The loop runs once in setup, so the melody plays once and stops.
🧠 Conceptual
intermediate1:30remaining
Which statement about tone() is true?
Select the correct statement about the Arduino tone() function.
Attempts:
2 left
💡 Hint
Think about what happens if you call tone() twice on the same pin.
✗ Incorrect
Calling tone() on a pin stops any tone currently playing on that pin before starting the new one. It does not support multiple simultaneous tones on the same pin.
🔧 Debug
advanced2:00remaining
Why does this melody play only one note?
This code is intended to play a melody of three notes, but only the last note sounds. What is the cause?
Arduino
const int speakerPin = 9; int melody[] = {440, 494, 523}; int noteDurations[] = {500, 500, 500}; void setup() { for (int i = 0; i < 3; i++) { tone(speakerPin, melody[i]); delay(noteDurations[i]); } } void loop() {}
Attempts:
2 left
💡 Hint
Check how tone() behaves when called without a duration parameter.
✗ Incorrect
Calling tone() without a duration starts the tone and it plays until noTone() is called or another tone() call interrupts it. The delay pauses the program but does not stop the tone, so only the last tone remains playing.
📝 Syntax
advanced1:30remaining
Which option causes a compilation error?
Identify the code snippet that will cause a compilation error when trying to play a tone on pin 7.
Attempts:
2 left
💡 Hint
Look carefully at the parameters passed to tone().
✗ Incorrect
Option A is missing a comma between frequency and duration parameters, causing a syntax error. The other options are syntactically correct.
🚀 Application
expert2:30remaining
How many notes are played by this code?
Given the code below, how many distinct notes will be played on the speaker connected to pin 10?
Arduino
const int speakerPin = 10; int melody[] = {262, 294, 330, 349, 392}; int noteDurations[] = {4, 8, 4, 8, 4}; void setup() { int i = 0; while (i < 5) { int duration = 1000 / noteDurations[i]; tone(speakerPin, melody[i], duration); delay(duration * 1.2); i++; } } void loop() {}
Attempts:
2 left
💡 Hint
Count how many times tone() is called and check the loop condition.
✗ Incorrect
The while loop runs 5 times, playing each note in the melody array with durations calculated from noteDurations. All notes play sequentially.
