Sei sulla pagina 1di 5

void setup() {

// iterate over the notes of the 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() {
// no need to repeat the melody.
}
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define

NOTE_B0
NOTE_C1
NOTE_CS1
NOTE_D1
NOTE_DS1
NOTE_E1
NOTE_F1
NOTE_FS1
NOTE_G1
NOTE_GS1
NOTE_A1
NOTE_AS1
NOTE_B1
NOTE_C2
NOTE_CS2
NOTE_D2
NOTE_DS2
NOTE_E2
NOTE_F2
NOTE_FS2
NOTE_G2
NOTE_GS2
NOTE_A2
NOTE_AS2
NOTE_B2
NOTE_C3
NOTE_CS3
NOTE_D3
NOTE_DS3
NOTE_E3
NOTE_F3
NOTE_FS3
NOTE_G3
NOTE_GS3
NOTE_A3
NOTE_AS3
NOTE_B3

31
33
35
37
39
41
44
46
49
52
55
58
62
65
69
73
78
82
87
93
98
104
110
117
123
131
139
147
156
165
175
185
196
208
220
233
247

#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define

NOTE_C4
NOTE_CS4
NOTE_D4
NOTE_DS4
NOTE_E4
NOTE_F4
NOTE_FS4
NOTE_G4
NOTE_GS4
NOTE_A4
NOTE_AS4
NOTE_B4
NOTE_C5
NOTE_CS5
NOTE_D5
NOTE_DS5
NOTE_E5
NOTE_F5
NOTE_FS5
NOTE_G5
NOTE_GS5
NOTE_A5
NOTE_AS5
NOTE_B5
NOTE_C6
NOTE_CS6
NOTE_D6
NOTE_DS6
NOTE_E6
NOTE_F6
NOTE_FS6
NOTE_G6
NOTE_GS6
NOTE_A6
NOTE_AS6
NOTE_B6
NOTE_C7
NOTE_CS7
NOTE_D7
NOTE_DS7
NOTE_E7
NOTE_F7
NOTE_FS7
NOTE_G7
NOTE_GS7
NOTE_A7
NOTE_AS7
NOTE_B7
NOTE_C8
NOTE_CS8
NOTE_D8
NOTE_DS8

262
277
294
311
330
349
370
392
415
440
466
494
523
554
587
622
659
698
740
784
831
880
932
988
1047
1109
1175
1245
1319
1397
1480
1568
1661
1760
1865
1976
2093
2217
2349
2489
2637
2794
2960
3136
3322
3520
3729
3951
4186
4435
4699
4978

//this noteLibrary depicts the frequencies of each note.


z=0
b2=123
c3=131
cs31=39
d3=147
ds3=156

e3=165
f3=175
fs3=185
g3=196
gs3=208
a3=220
as3=233
b3=247
c4=262
cs4=277
d4=294
ds4311
e4=330
f4=349
fs4=370
g4=392
gs4=415
a4=440
as4=466
b4=494
c5=523
cs5=554
d5=587
ds5=622
e5=659
f5=698
fs5=740
g5=784
gs5=831
a5=880
as5=932
b5=988
c6=1047
cs6=1109
d6=1175
ds6=1245
e6=1319
f6=1397
fs6=1480
g6=1568
gs6=1661
a6=1760
as6=1865
b6=1976
c7=2093
cs7=2217
d7=2349
ds7=2489
e7=2637
f7=2794
fs7=2960
g7=3136
gs7=3322
a7=3520
as7=3729
b7=3951
c8=4186
cs8=4435
d8=4699
ds8=4978

//tone[inputPin, melody[], noteDuration[]]


//This is code which uses the lock of an arduino to play notes in a sequ
ence at particular durations
//as well as particular pitches.
//As the arduino can only play one tone at a time, this code is simply a
mockup for a more
//powerful arduino such as th arduino Mega.
//inputPin# denotes which pin# is being used as a digital output to the
circuit which leads to the speaker.
//melody plays a specific frequency
//melody has a list of notes which have been specifiied earlier
//to correspond to particular frequencies i.e. keys.
//noteDuration corresponds to a formula which is altered through tempo t
o specific particlaur length to
//each note played by tone.
//MaximumOneNote=8
//MaximumOneNote denotes the fractional length of the smallest note such
that
//MaximumOneNote's value times the smallest note is a whole note
int tempo=120
//this tempo is particular to lavender town, it is different for every s
ong.
//noteDuration[]=(1/tempo)*15*oneNote[]
//Through this equation, noteDuration plays the speed at which is desire
d without
//having to lookup how many fractions of a second each note is.
int melody[161:0]={c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, z, gs4, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
b4, gs4, b4, z, b4,
b4, gs4, b4, z, b4,
b4, gs4, b4, z, b4,
c5, gs4, z, b4, z, f4, z,
z, gs4, z, b4, z, f4, z,
z, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z,
c5, gs4, z, b4, z, f4, z}
//array of notes to be played
int oneNote[161:0]={2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,

2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 2, 1, 1, 1,
2, 2, 1, 1, 1,
2, 2, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1}
int noteDuration[161:0]=(1/tempo)*15*oneNote[161:0]
//corresponding array of durations each note is played, each denotes a m
ultiple of the smallest fraction
tone[inputPin1, melody[161:0], noteDuration[161:0]]
//this plays the tune through input 1; this tune can be played on the arduino gi
ven.
//the other tunes which add to a song found below
//not accurate in programming t the moment, numbers must be changed
int melody[57:0]={z, z, z, z,
gs4, gs4, e5, e5, gs4, f4, e5, b4, e5, c5, b4, e
5,
gs4, gs4, e5, e5, gs4, f4, e5, b4, e5, c5, b4, e
5,
gs4, gs4, e5, e5, gs4, f4, e5, b4, e5, c5, b4, e
5,
gs4, gs4, e5, e5, gs4, f4, e5, b4, e5, g4, f4, b
4,
c5, z, c5, z, z, z}
int oneNote[57:0]={8, 8, 8, 8
4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2,
4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2,
4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2,
4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2,
2, 6, 2, 6, 8, 8,}
int noteDuration[57:0]=(1/tempo)*15*oneNote[57:0]
tone[inputPin2, melody[57:0], noteDuration[57:0]]
//this is the limit of the arduino
melody[33:0]={z, z, z, z,
e5, d5, c5, cs5, cs5,
e5, z, d5, c5, z,
e5, z, d5, c5, cs5, cs5,
c5, z, c5, z, c5, z, z,
e5, d5, c5, e5, c5, b4, e5}
int oneNote[33:0]={8, 8, 8, 8,
8, 8, 8, 4, 4,
2, 6, 8, 8, 8,
2, 6, 8, 8, 4, 4,
2, 6, 2, 6, 2, 6, 8,
8, 8, 8, 2, 2, 2, 2}
itn noteDuration[33:0]=(1/tempo)*15*oneNote[33:0]
tone[inputPin3, melody[33:0], noteDuration[33:0]]
//If tune works, should play Lavender Town theme from Pokemon Red/Blue/Y
ellow

Potrebbero piacerti anche