Bird
0
0
Arduinoprogramming~20 mins

Playing melodies with tone() in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tone Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
}
AThe speaker plays a continuous tone at 262Hz without pauses.
BThe speaker plays four notes (C4, D4, E4, F4) each for 250ms with short pauses, then stops.
CThe code causes a compilation error due to missing semicolons.
DThe speaker plays the notes but all at the same time, creating a chord.
Attempts:
2 left
💡 Hint
Think about how tone() and delay() work together to play notes sequentially.
🧠 Conceptual
intermediate
1:30remaining
Which statement about tone() is true?
Select the correct statement about the Arduino tone() function.
Atone() can play multiple tones simultaneously on different pins without interference.
Btone() automatically stops after 1 second if no duration is specified.
Ctone() requires an external library to work.
Dtone() stops any previous tone on the same pin when called again.
Attempts:
2 left
💡 Hint
Think about what happens if you call tone() twice on the same pin.
🔧 Debug
advanced
2: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() {}
AThe speakerPin is not set as output, so only the last note plays.
BThe delay() function is too short to hear the notes properly.
Ctone() without duration plays indefinitely until stopped, so only the last note remains audible.
DThe melody array has invalid frequencies causing the first notes to be skipped.
Attempts:
2 left
💡 Hint
Check how tone() behaves when called without a duration parameter.
📝 Syntax
advanced
1: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.
Atone(7, 440 500); delay(500);
Btone(7, 440); delay(500); noTone(7);
Ctone(7, 440, 500); delay(500); noTone(7);
Dtone(7, 440, 500); delay(500);
Attempts:
2 left
💡 Hint
Look carefully at the parameters passed to tone().
🚀 Application
expert
2: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() {}
A5 notes are played sequentially with varying durations.
BOnly 4 notes are played because the last note is skipped.
CThe code plays 5 notes but all with the same duration.
DNo notes are played because tone() is called inside a while loop.
Attempts:
2 left
💡 Hint
Count how many times tone() is called and check the loop condition.