After completing the Making Music project, test yourself by trying these experiments.
1
| Replace the number 440 in the tone command with 523.
Upload and run the program again.
What do you hear? Experiment with other numbers.
|
2
| Modify the program to make it play two notes, one after the other.
|
3
| Modify the program to make it play three notes, one after the other with the first note faster than the last two notes.
|
4
| This next program will play several notes of a song.
The frequencies for the four notes (NOTE_G3, NOTE_A3, NOTE_B3, and NOTE_C4) are assigned to variable names using the int command.
For example, the frequency 196 is assigned to the variable name NOTE_G3.
Later in the code, we can use the name instead of the number, which is easier to remember.
A variable name must start with a letter and cannot have any spaces.
Upload and run the program. You should hear a song.
|
int NOTE_G3 = 196;
int NOTE_A3 = 220;
int NOTE_B3 = 247;
int NOTE_C4 = 262;
void setup() {
pinMode(8, OUTPUT);
tone(8, NOTE_C4);
delay(250);
tone(8, NOTE_G3);
delay(125);
noTone(8);
delay(75);
tone(8, NOTE_G3);
delay(125);
tone(8, NOTE_A3);
delay(250);
tone(8, NOTE_G3);
delay(250);
noTone(8);
delay(250);
tone(8, NOTE_B3);
delay(250);
tone(8, NOTE_C4);
delay(400);
noTone(8);
}
void loop() {
}
|
5
| Create a function call StarWars() with the code shown on the right to play the first 16 notes of the Star Wars Theme song.
The notes are: NOTE_C4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4 and NOTE_D4 with the given frequencies and durations.
Call the StarWars() function whenever you want to execute the code in the function like this
void setup() {
StarWars();
}
|
int NOTE_C4 = 262;
int NOTE_D4 = 294;
int NOTE_E4 = 330;
int NOTE_F4 = 349;
int NOTE_G4 = 392;
int NOTE_A4 = 440;
int NOTE_B4 = 494;
int NOTE_C5 = 523;
// This is the StarWars function containing the code to
// play the first 16 notes of the Star Wars Theme song
void StarWars() {
tone(8, NOTE_C4);
delay(1000);
tone(8, NOTE_G4);
delay(1000);
tone(8, NOTE_F4);
delay(167);
tone(8, NOTE_E4);
delay(167);
tone(8, NOTE_D4);
delay(167);
tone(8, NOTE_C5);
delay(1000);
tone(8, NOTE_G4);
delay(500);
tone(8, NOTE_F4);
delay(167);
tone(8, NOTE_E4);
delay(167);
tone(8, NOTE_D4);
delay(167);
tone(8, NOTE_C5);
delay(1000);
tone(8, NOTE_G4);
delay(500);
tone(8, NOTE_F4);
delay(167);
tone(8, NOTE_E4);
delay(167);
tone(8, NOTE_F4);
delay(167);
tone(8, NOTE_D4);
delay(2000);
noTone(8);
}
void setup() {
pinMode(8, OUTPUT);
StarWars(); // this will execute the StarWars() function
}
void loop() {
}
|
6
| Make a police siren with sound and flashing red light.
Connect a buzzer to pin 8.
|
int red_led = 13;
int buzzer = 8;
void setup() {
pinMode(red_led, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
digitalWrite(red_led, HIGH);
// increment the siren frequency from 650 going up to 900
for (int frequency=650; frequency<=900; frequency++) {
tone(buzzer, frequency);
delay(10);
}
digitalWrite(red_led, LOW);
// decrement the siren frequency from 900 going down to 650
for (int frequency=900; frequency>=650; frequency--) {
tone(buzzer, frequency);
delay(10);
}
}
|
7
| If you want to experiment more, this next program makes it easier to write your own music.
Select File from the menu
-
then select Examples
-
then select Digital
-
then select toneMelody
Upload and run the program.
|
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// Change the number 8 to match the number of notes in your melody
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
}
|
8
| Create your own piece of music using the toneMelody program from Experiment 7 above.
Three things you need to change in the program:
i. The music notes for your song in the line int melody[ ] = { ...
ii. The durations for each note in the line int noteDurations[ ] = { ...
iii. The number of notes in your song in the line for (int thisNote = 0; thisNote < 8; thisNote++) {
|
9
| (Optional Advance)
In order to make the sound louder, you'll need to use a transistor to amplify the sound.
Use an NPN transistor 3904 or 2N2222.
You can also use a speaker instead of the buzzer to make it sound better.
|
|
|
Connect the transistor and speaker as follows:
|
|
|
NOTE: Use of the tone command will interfere with the analogWrite command on pins 3 and 11.
This means that you cannot use the analogWrite command on pins 3 and 11, and use the tone command at the same time.
|
|