Sunteți pe pagina 1din 6

const int buzzerPin = 8;

const int SongA = 4; //Tetris pin


const int SongB = 5; //Mario pin
const int SongC = 6; //DOOM pin //This section defines all our pins/variables.
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int i, duration;

const int songLengthA = 38; //This are the notes, rythmes, total
length, and tempo for song A, B, and C
char notesA[] = "EbCDCbaaCEDCbCDECaa DFAGFECEDCbbCDECaa ";
int beatsA[] = {2,1,1,2,1,1,2,1,1,2,1,1,3,1,2,2,2,2,2,3,2,1,2,1,1,3,1,2,1,1,2,1,1,2,2,2,2,2};
int tempoA = 150;

const int songLengthB= 106;


char notesB[] = "EE CE G g C g e a b sagEGAFG ECDb C g e a b sagEGAFG ECDb GlFw E
gaC aCD GlFw E A AA GlFw E gaC aCD w D C ";
float beatsB[] = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
1, 0.66, 0.66, 0.66, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 2, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
0.66, 0.66, 0.66, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 2, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 2, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 1, 1,1,1};
int tempoB = 250;

const int songLengthC = 54;


char notesC[] = "eeEeeDeeCeesbC eeEeeDeeCes eeEeeDeeCeesbC eeEeeDeeCes ";
float beatsC[] =
{0.5,1,1.5,0.5,1,1.5,0.5,1,1.5,0.5,1,1.5,1,1.5,1,1,1,1.5,0.5,1,1.5,0.5,1,1.5,1,4,1,1,1,1.5,0.5,1,1.5,
0.5,1,1.5,0.5,1,1.5,1,1.5,1,1,1,1.5,0.5,1,1.5,0.5,1,1.5,1,4,1};
int tempoC = 125;

void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(SongA, INPUT);
pinMode(SongB, INPUT);
pinMode(SongC, INPUT);
pinMode(RED_PIN, OUTPUT); //This section sets our pinmodes. Output pins
give power, whereas input pins recieve power.
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
digitalWrite(buzzerPin, LOW);
};

void loop() {
digitalWrite(buzzerPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW); //Sets all output pins to LOW, basically a reset.
digitalWrite(BLUE_PIN, LOW);
if ((digitalRead(SongA) == HIGH)) {

playSongA (); //Detects if Song A (Tetris)'s pin has power, if so, play said
song
digitalWrite(buzzerPin, LOW);

}
if ((digitalRead(SongB) == HIGH)) {
playSongB (); //Detects if Song B (Mario)'s pin has power, if so, play said
song
digitalWrite(buzzerPin, LOW);
};
if ((digitalRead(SongC) == HIGH)) {
red(); //
digitalWrite(buzzerPin, LOW); //Detects if Song C (Doom theme)'s pin has power, if
so, play said song
playSongC ();
digitalWrite(buzzerPin, LOW);

}
else (digitalWrite(buzzerPin, LOW));

//Otherwise, don't play anything.


} //Then loop

void playSongA () { //Play Song A, Tetris


for (i = 0; i < songLengthA; i++) //Go through all the notes, until the song length is
reached
{
duration = beatsA[i] * tempoA; // length of note/rest in ms
int x;
x = random(5);
if (notesA[i] == ' ') //check for a rest
{
if (x == 0) {
cyan();
}
if (x == 1) {
purple();
}
if (x == 2) {
blue(); //this section will make the LED play either cyan, purle, blue, red,
or green during rests
}
if (x == 3) {
red();
}
if (x == 4) {
green();
}
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
if (x == 0) {
cyan();
}
if (x == 1) {
purple();
}
if (x == 2) {
blue();
} //this section will make the LED play either cyan, purle, blue, red,
or green everytime a note is played
if (x == 3) {
red();
}
if (x == 4) {
green();
}
tone(buzzerPin, frequency(notesA[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempoA/10);
digitalWrite(buzzerPin, LOW); //turn of buzzer/stop song
}
}
void playSongC () { //Play song C, Doom
for (i = 0; i < songLengthC; i++) //Go through all the notes, until the song length is
reached
{
duration = beatsC[i] * tempoC; // length of note/rest in ms

if (notesC[i] == ' ') // is this a rest?


{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(buzzerPin, frequency(notesC[i]), duration); //play note
delay(duration); // wait for tone to finish
}
delay(tempoC/10);
digitalWrite(buzzerPin, LOW); // stops song
}
}

void playSongB () { //play song B, Mario


int i = 0;
for (i = 0; i < songLengthB; i++) //Go through all the notes, until the song length is
reached
{
int x;
x = random(3);
duration = beatsB[i] * tempoB; // length of note/rest in ms

if (notesB[i] == ' ') // is this a rest?


{
if (x == 0) {
red();
} //LED will either light red or blue during a rest
if (x == 1) {
blue();
}
if (x == 2) {
green();
}

delay(duration); // then pause for a moment


}
else // otherwise, play the note
{
if (x == 0) {
red();
} //Light the LED to either red or blue when a note is played
if (x == 1) {
blue();
}
if (x == 2) {
green();
}

tone(buzzerPin, frequency(notesB[i]), duration); //play note


delay(duration); // wait for tone to finish
}
delay(tempoB/10);
digitalWrite(buzzerPin, LOW); //stop song

}
}

int frequency(char note) //This function will set notes to their appropriate frequency
{

int i;
const int numNotes = 17; //The number of notes being stored

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 's', 'l', 'w', 't'}; //Names of
notes being stored
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523, 587, 660, 670, 784, 880, 466, 740,
622, 415}; //Frequencies of the notes being stored

for (i = 0; i < numNotes; i++) // Step through the notes


{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); //If nothing is found, return no sound
}

void red() { //Make the color red


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}

void green() { //Make the color green


digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}

void blue() { //Make the color blue


digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}

void yellow() { //Make the color yellow(red/green)


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}

void purple() { //Make the color purple(red/blue)


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}

void cyan() { //Make the color cyan(green/blue)


digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
}

S-ar putea să vă placă și