Devices:
speaker, pull down resistors x2, potentiometer, wires, buttons x2 ,led x2,
———-whole piece of code here——————-
#include “pitches.h”
#define NO_SOUND 0 // make the rests in music
// note types: 1 = whole note, 2 = half note, 4 = quarter note, 8 = eighth note, etc.
//Mario melody
int mario_melody[] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0, MUSIC_STOP
};
//Mario tempo
int mario_tempo[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
// 007 melody
int d07_melody[] = {
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,
NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4,
NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5,
NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6,NO_SOUND, MUSIC_STOP
};
// 007 tempo
int d07_tempo[] = {
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,2,8,8,1,
8,4,8,4,8,8,
8,8,4,8,4,8,
4,8,4,8,3
};
// mario nderworld melody
int underworld_melody[] = {
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0, NOTE_DS4, NOTE_CS4, NOTE_D4,
NOTE_CS4, NOTE_DS4,
NOTE_DS4, NOTE_GS3,
NOTE_G3, NOTE_CS4,
NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
NOTE_GS4, NOTE_DS4, NOTE_B3,
NOTE_AS3, NOTE_A3, NOTE_GS3,
0, 0, 0, MUSIC_STOP
};
// mario underworld tempo
int underworld_tempo[] = {
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
6, 18, 18, 18,
6, 6,
6, 6,
6, 6,
18, 18, 18, 18, 18, 18,
10, 10, 10,
10, 10, 10,
3, 3, 3
};
int pace = 1450; // change pace of music(“speedy”)
int spkrPin = 8;
// these are used for later phases
int potPin = A0;
int buttonPin = 2;
int buttonPin2 = 3;
int feq;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int counter;
void play(int *music, int *timing) {
for (int Note = 0; music[Note] != MUSIC_STOP; Note++) {
setColor(music[Note]);
int duration = pace/timing[Note]; // music tempo
tone(spkrPin, music[Note]+feq, duration); // play tone
// give the note time to play
delay(duration*1.2);
}
}
void setColor(int colorchoice){
//???
}
void setup() {
// Not needed here but used as an example for a later phase
// int *melody = mario_melody; // these POINT to the beginning of the array
// int *tempo = mario_tempo; // hence called integer array pointers
Serial.begin(9600);
pinMode(buttonPin,INPUT);
noTone(spkrPin);
}
void loop() {
// if(digitalRead(buttonPin)==HIGH){
// int *melody = mario_melody; // these POINT to the beginning of the array
// int *tempo = mario_tempo; // hence called integer array pointers
// Serial.println(“Now playing Mario…”);
// play(melody, tempo);
// }
//
// else if(digitalRead(buttonPin2)==HIGH){
// int *melody = d07_melody; // these POINT to the beginning of the array
// int *tempo = d07_tempo; // hence called integer array pointers
// Serial.println(“Now playing 007…”);
// play(melody, tempo);
// }
if (Serial.available() > 0) {
if(Serial.read()==’p’){
Serial.println(“Juxbox Mode”);
freq();
}
else{
Serial.println(“Out of Juxbox Mode”);
feq=0;
}
}
if(counter==3){
counter=0;
}
switch(counter){
case 0:
song1();
break;
case 1:
song2();
break;
case 2:
song3();
}
}
void freq(){
feq=map(analogRead(0),0,1024,0,500);
Serial.println(feq);
}
void song1(){
int *melody = mario_melody;
int *tempo = mario_tempo; // hence called integer array pointers
Serial.println(“Now playing Mario…”);
play(melody, tempo);
}
void song2(){
int *melody = d07_melody; // these POINT to the beginning of the array
int *tempo = d07_tempo; // hence called integer array pointers
Serial.println(“Now playing 007…”);
play(melody, tempo);
}
void song3(){
int *melody = underworld_melody; // these POINT to the beginning of the array
int *tempo = underworld_tempo; // hence called integer array pointers
Serial.println(“Now playing 007…”);
play(melody, tempo);
}