Making Music Experiments

After completing the Making Music project, test yourself by trying these experiments.

1 Modify the code so that it will play the C major scale.

The notes are: NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4 and NOTE_C5 with the given frequencies.

The durations should all be the same.

Remember to press the Upload button to upload and run the program on the Arduino board after each time you make any modifications to your program.
void setup() {
  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;

  pinMode(8, OUTPUT);
  tone(8, NOTE_C4);
  delay(250);
  tone(8, NOTE_D4);
  delay(250);
  tone(8, NOTE_E4);
  delay(250);
  tone(8, NOTE_F4);
  delay(250);
  tone(8, NOTE_G4);
  delay(250);
  tone(8, NOTE_A4);
  delay(250);
  tone(8, NOTE_B4);
  delay(250);
  tone(8, NOTE_C5);
  delay(250);
  noTone(8);
}

void loop() {
}

 
2 Create a function call CMajorScale() with the code to play the C major scale.

A function allows a programmer to create a modular piece of code that performs a specific task. The main routine (caller) calls this function to execute the task when needed, and when the function finishes, it then returns to the caller at the line after where the function was called.

The void keyword tells the computer that this function does not return a value.

The name given to this function is CMajorScale.

The function does not require any parameters to be passed in so there's nothing inside the parenthesis.

The body of the function is bracketed by the open and close { } braces.

Anytime in the main code where we want to play the C major scale, we simply call the function with the newly created function command CMajorScale();.
void CMajorScale() {
  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;

  tone(8, NOTE_C4);
  delay(250);
  tone(8, NOTE_D4);
  delay(250);
  tone(8, NOTE_E4);
  delay(250);
  tone(8, NOTE_F4);
  delay(250);
  tone(8, NOTE_G4);
  delay(250);
  tone(8, NOTE_A4);
  delay(250);
  tone(8, NOTE_B4);
  delay(250);
  tone(8, NOTE_C5);
  delay(250);
  noTone(8);
}

void setup() {
  pinMode(8, OUTPUT);
  CMajorScale();  // call the function to play the C major scale
}

void loop() {
}

 
3 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 routine like this

void setup() {
  StarWars();
}
void StarWars() {
  int buzzerPin = 8;
  pinMode(buzzerPin, OUTPUT);
  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;

  // code to play the first 16 notes of the Star Wars Theme song
  tone(buzzerPin, NOTE_C4);
  delay(1000);
  tone(buzzerPin, NOTE_G4);
  delay(1000);
  tone(buzzerPin, NOTE_F4);
  delay(167);
  tone(buzzerPin, NOTE_E4);
  delay(167);
  tone(buzzerPin, NOTE_D4);
  delay(167);
  tone(buzzerPin, NOTE_C5);
  delay(1000);
  tone(buzzerPin, NOTE_G4);
  delay(500);

  tone(buzzerPin, NOTE_F4);
  delay(167);
  tone(buzzerPin, NOTE_E4);
  delay(167);
  tone(buzzerPin, NOTE_D4);
  delay(167);
  tone(buzzerPin, NOTE_C5);
  delay(1000);
  tone(buzzerPin, NOTE_G4);
  delay(500);
  
  tone(buzzerPin, NOTE_F4);
  delay(167);
  tone(buzzerPin, NOTE_E4);
  delay(167);
  tone(buzzerPin, NOTE_F4);
  delay(167);
  tone(buzzerPin, NOTE_D4);
  delay(2000);
  noTone(8);
}

 
4 Make a police siren with sound and flashing red light.

Connect a buzzer to pin 8.
int redLed = 13;

void setup() {
  pinMode(redLed, OUTPUT);
}

void loop() {
  digitalWrite(redLed, HIGH);
  //siren going up
  for (int frequency=650; frequency<900; frequency++) {
    tone(8, frequency);
    delay(10);
  }
  digitalWrite(redLed, LOW);
  // siren going down
  for (int frequency=900; frequency>=650; frequency--) {
    tone(8, frequency);
    delay(10);
  }
}

 
5 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() {
}

 
6 Create your own piece of music using the toneMelody program from Experiment 5 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++) {
 
7 (Optional Advance)
In order to get more volume, you'll need to use a transistor to amplify the sound to make it louder. Use an NPN transistor 3904 or 2N2222.
NPN transistor






Connect the transistor and speaker as follows:

tone6 transistor circuit
tone7 transistor
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.