Exciting Freddy Song (Toreador March) From FNAF

Supplies needed:

*2x 220 ohm or higher ohm resistor (1 is optional)

*Arduino board

-Breadboard (optional)

*Speaker or piezo buzzer

-NPN Transistor (optional)

*USB or power source for Arduino (when finished)

*Wires (Male to Male & Female to Female, etc.)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Hardware Setup

First, connect the positive pin of the speaker or buzzer to 5V WITH a resistor ( or take out the resistor if you want the speaker to be loud). Second, connect the negative end of the speaker ( GND ) to the collector of the transistor. Third, connect the GND from the Arduino to the emitter of the transistor. Last, connect the base of the transistor to pin 8 of the Arduino (or change this if you would like to, but you'll have to change it in the code). WARNING IF YOU CONNECT THE SPEAKER WITHOUT A RESISTOR, THE SPEAKER WILL BE REALLY LOUD. THE TRANSISTOR IS USED AS AN AMPLIFIER, SO YOU DON'T HAVE TO USE IT. If you want no amplifier (not making the speaker louder), then, connect the positive end of the speaker or buzzer to Arduino I/O pin 8, then connect the GND of the speaker to the Arduino GND.

Step 2: Code Setup

Here is the code:

#include "pitches.h"

/*Credit to : Griffdith, Scott Cawthon, Toms Mucenieks, UptownKitten453, & the Arduino ToneMelody tutorial. https://www.arduino.cc/en/Tutorial/toneMelody https://gist.github.com/Griffdith/daf45583b980796... https://www.arduino.cc/en/Tutorial/toneMelody https://musescore.com/user/1234751/scores/594226 */

#define NOTE_Ab4 415.30

#define NOTE_Bb4 466.16

#define NOTE_Db5 554.37

#define NOTE_Ab5 830.61

#define NOTE_Bb5 932.33

#define NOTE_Db6 1108.73

#define NOTE_Eb6 1244.51

#define NOTE_Gb6 1479.98

#define NOTE_Ab6 1661.22

int melody[] = {

NOTE_C5, NOTE_D5, NOTE_C5, NOTE_A4,

NOTE_A4, NOTE_A4, NOTE_G4, NOTE_A4,

NOTE_Bb4, NOTE_A4, NOTE_Bb4, NOTE_G4,

NOTE_C5, NOTE_A4, NOTE_F4, NOTE_D4,

NOTE_G4, NOTE_C4, NOTE_G4, NOTE_D5,

NOTE_C5, NOTE_Bb4, NOTE_A4, NOTE_G4,

NOTE_A4, NOTE_Bb4, NOTE_A4, NOTE_E4,

NOTE_A4, NOTE_A4, NOTE_Ab4, NOTE_B4,

NOTE_E5, NOTE_D5, NOTE_Db5, NOTE_D5,

NOTE_G4, NOTE_A4, NOTE_Bb4, NOTE_A4,

NOTE_F4, NOTE_D5, NOTE_C5, NOTE_C4,

NOTE_Bb4, NOTE_A4, NOTE_D4, NOTE_F4,

NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5,

NOTE_G5, NOTE_A5, NOTE_A5, NOTE_C6,

NOTE_D6, NOTE_C6, NOTE_A5, NOTE_F5,

NOTE_A5, NOTE_F5, NOTE_A5, NOTE_F5,

NOTE_G5, NOTE_A5, NOTE_Bb5, NOTE_A5,

NOTE_F5, NOTE_G5, NOTE_Bb5, NOTE_G5,

NOTE_C6, NOTE_A5, NOTE_F5, NOTE_F5,

NOTE_D5, NOTE_D5, NOTE_G5, NOTE_C5,

NOTE_G5, NOTE_D6, NOTE_C6, NOTE_Bb5,

NOTE_A5, NOTE_G5, NOTE_A5, NOTE_Bb5,

NOTE_A5, NOTE_E5, NOTE_A5, NOTE_A5,

NOTE_Ab5, NOTE_B5, NOTE_E6, NOTE_Eb6,

NOTE_E6, NOTE_F6, NOTE_Ab6, NOTE_Gb6,

NOTE_F6, NOTE_Gb6, NOTE_B5, NOTE_Db6,

NOTE_D6, NOTE_Db6, NOTE_A5, NOTE_Gb6,

NOTE_E6, NOTE_A5, NOTE_E5, NOTE_D6,

NOTE_Db6, NOTE_B5, NOTE_A5 };

int noteDurations[] = {

4, 8, 8, 4,

4, 8, 8, 8,

8, 2, 4, 8,

8, 2, 4, 8,

8, 2, 2, 8,

8, 8, 8, 8,

8, 8, 8, 8,

2, 4, 4, 4,

8, 8, 1, 16,

16, 16, 8, 8,

8, 8, 8, 4,

16, 16, 16, 8,

8, 2, 16, 16,

16, 4, 4, 4,

8, 16, 16, 16,

16, 16, 16, 16,

16, 16, 16, 16,

16, 16, 16, 4,

8, 8, 4, 4,

8, 8, 8, 8,

2, 4, 8, 8,

2, 4, 8, 8,

2, 2, 8, 8,

8, 8, 8, 8,

8, 8, 2, 4,

4, 4, 8, 8,

1, 16, 16, 16,

8, 8, 8, 8,

4, 16, 1, 1, };

void setup () { }

void loop () {

// iterate over the notes of the melody:

for (byte thisNote = 0; thisNote < 115; 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); }

Step 3: Code Explanation

#define NOTE_Ab4 415.30

#define NOTE_Bb4 466.16

#define NOTE_Db5 554.37

#define NOTE_Ab5 830.61

#define NOTE_Bb5 932.33

#define NOTE_Db6 1108.73

#define NOTE_Eb6 1244.51

#define NOTE_Gb6 1479.98

#define NOTE_Ab6 1661.22

This ^---- (up) defines the custom notes that aren't declared in the library, such as, A-flat 4 with a certain duration. (How long the note last)

int melody[] = {

NOTE_C5, NOTE_D5, NOTE_C5, NOTE_A4,

NOTE_A4, NOTE_A4, NOTE_G4, NOTE_A4,

NOTE_Bb4, NOTE_A4, NOTE_Bb4, NOTE_G4,

NOTE_C5, NOTE_A4, NOTE_F4, NOTE_D4,

NOTE_G4, NOTE_C4, NOTE_G4, NOTE_D5,

NOTE_C5, NOTE_Bb4, NOTE_A4, NOTE_G4,

NOTE_A4, NOTE_Bb4, NOTE_A4, NOTE_E4,

NOTE_A4, NOTE_A4, NOTE_Ab4, NOTE_B4,

NOTE_E5, NOTE_D5, NOTE_Db5, NOTE_D5,

NOTE_G4, NOTE_A4, NOTE_Bb4, NOTE_A4,

NOTE_F4, NOTE_D5, NOTE_C5, NOTE_C4,

NOTE_Bb4, NOTE_A4, NOTE_D4, NOTE_F4,

NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5,

NOTE_G5, NOTE_A5, NOTE_A5, NOTE_C6,

NOTE_D6, NOTE_C6, NOTE_A5, NOTE_F5,

NOTE_A5, NOTE_F5, NOTE_A5, NOTE_F5,

NOTE_G5, NOTE_A5, NOTE_Bb5, NOTE_A5,

NOTE_F5, NOTE_G5, NOTE_Bb5, NOTE_G5,

NOTE_C6, NOTE_A5, NOTE_F5, NOTE_F5,

NOTE_D5, NOTE_D5, NOTE_G5, NOTE_C5,

NOTE_G5, NOTE_D6, NOTE_C6, NOTE_Bb5,

NOTE_A5, NOTE_G5, NOTE_A5, NOTE_Bb5,

NOTE_A5, NOTE_E5, NOTE_A5, NOTE_A5,

NOTE_Ab5, NOTE_B5, NOTE_E6, NOTE_Eb6,

NOTE_E6, NOTE_F6, NOTE_Ab6, NOTE_Gb6,

NOTE_F6, NOTE_Gb6, NOTE_B5, NOTE_Db6,

NOTE_D6, NOTE_Db6, NOTE_A5, NOTE_Gb6,

NOTE_E6, NOTE_A5, NOTE_E5, NOTE_D6,

NOTE_Db6, NOTE_B5, NOTE_A5 };

This defines the notes from the pitches library, which is included in the code, with certain durations or how long the notes last. The "pitches.h" library is defined first thing in the code.

int noteDurations[] = {
4, 8, 8, 4,

4, 8, 8, 8,

8, 2, 4, 8,

8, 2, 4, 8,

8, 2, 2, 8,

8, 8, 8, 8,

8, 8, 8, 8,

2, 4, 4, 4,

8, 8, 1, 16,

16, 16, 8, 8,

8, 8, 8, 4,

16, 16, 16, 8,

8, 2, 16, 16,

16, 4, 4, 4,

8, 16, 16, 16,

16, 16, 16, 16,

16, 16, 16, 16,

16, 16, 16, 4,

8, 8, 4, 4,

8, 8, 8, 8,

2, 4, 8, 8,

2, 4, 8, 8,

2, 2, 8, 8,

8, 8, 8, 8,

8, 8, 2, 4,

4, 4, 8, 8,

1, 16, 16, 16,

8, 8, 8, 8,

4, 16, 1, 1, };

This defines what types of notes they are (e.g. 4 = quarter note, 8 = 8th note, etc.).

void loop () {
// iterate over the notes of the melody:

for (byte thisNote = 0; thisNote < 115; 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); }

The first part is the for loop which goes over all the notes and types of notes. The "noteDuration = 1000 / noteDurations[thisNote] " part calculates the note duration (how long the note is). The "tone(8, melody[thisNote], noteDuration);" part plays the actual note and the duration of the note / how long they last. The "int pauseBetweenNotes = noteDuration * 1.30;" actually defines how long the delay between the notes are.

The last part is the "noTone(8);" part that stops the speaker from making sound / music, after the for loop ends. Then, everything repeats, and it all starts over again.

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    Discussions