EXPERIMENT

Try editing the body of the song procedure so that it plays a different tune. Can you get your monster to play Mary Had a Little Lamb, Happy Birthday, Beethoven’s 9th Symphony, or your own composition?

Hint 1: Below is a version of the song procedure that plays a scale:

void song(int duration) {
    tone(speaker, C);
    delay(duration);
    tone(speaker, D);
    delay(duration);
    tone(speaker, E);
    delay(duration);
    tone(speaker, F);
    delay(duration);
    tone(speaker, G);
    delay(duration);
    tone(speaker, A);
    delay(duration);
    tone(speaker, B);
    delay(duration);
    tone(speaker, C1);
    delay(duration);
    noTone(speaker);
    delay(duration);
}

Hint 2: You might want to play different notes for different amounts of time. There are a few different ways to approach this (each approach is described in a comment in the code below):

void song(int duration) {
    tone(speaker, C);
    delay(100);              // duration is permanently set at 100ms
    tone(speaker, D);
    delay(duration*2);       // duration is twice as long as input
    tone(speaker, E);
    delay(duration/2);       // duration is half as long as input
}

<< PREVIOUS      NEXT >>