You are on page 1of 6

int ledPins[] = {1,2,3,4,5,6,7,8,9};

//Used to keep track of how many leds are being used in the circuit

const int buzzerPin = 10;

//Tells what pin the buzzer is connected to.

const int songLength = 20;

// The songlength tells the board how long the song will last, including notes and spaces

char notes[] = "eedgeefdbbeefd bcdcb";

//Tells the board what notes to play on the Piezo Buzzer

int beats[] = {3,2,2,1,1,1,1,3,1,1,1,1,2,2,1,1,1,1,1,3};

//This includes what kind of note each above listed note is

int tempo = 200;

//The speed at which the song goes

void setup()

//Runs the stektch

{
pinMode(buzzerPin, OUTPUT);
int index;

//Says that the buzzer is the output

for(index = 0; index <= 8; index++)


{
//This command steps the lights to go from light 0 to 8.

pinMode(ledPins[index],OUTPUT);

//
}

void loop()
{
// Runs the programa and commands into a loop

int i, duration;

for (i = 0; i < songLength; i++) // Goes through the various arrays of the song
{
duration = beats[i] * tempo; // Duration of the song accounting for the notes, rests, and tempo

if (notes[i] == ' ') // Tells the board that a space is equal to a rest
{
delay(duration); // Tells the board to execute the rest
}
else // If there isn't a space just play the note like normal
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // Allow the tone to complete
}
delay(tempo/10); // Makes a pause between notes
}

// We leave the music commands and start the led commands

oneAfterAnotherNoLoop();

// Leds lights are not in a loop and go one after another

oneOnAtATime(); // Tells it to light up one led at a time and to go to the next one

pingPong(); // Next has the leds light up back and forth like a ball in ping pong

marquee(); // Lights up LEDs in an acarde like series

// Enter music commands again


int frequency(char note) //takes notes and matches them to a frequency
{

int i;
const int numNotes = 7; // Number of different notes in the song

char names[] = { 'e', 'd', 'g', 'f', 'd', 'b', 'c'};

//All the unique notes in the song

int frequencies[] = {330, 294, 392, 369, 293, 494, 277};


// Frequences of the notes being used in the song

for (i = 0; i < numNotes; i++) // Goes through each note


{
if (names[i] == note) // If the note is played use the frequency that corresponds
{
return(frequencies[i]); // Return with the frequency that corresponds to the note
}
}
return(0); // If there is nothing to return put 0
}
// Now returning to led commands

void oneAfterAnotherNoLoop() // This function will light one LED, after another with a pause in between with a
delay time stated in the command. The no loop indicates this function will not repeat itself.
{
int delayTime = 100; // Time inbetween lights turning on in miliseconds
//Each of these lines of code turns on and LED and tells it to wait a certain amount of time.
digitalWrite(ledPins[0], HIGH);
delay(delayTime);
digitalWrite(ledPins[1], HIGH);
delay(delayTime);
digitalWrite(ledPins[2], HIGH);
delay(delayTime);
digitalWrite(ledPins[3], HIGH);
delay(delayTime);
digitalWrite(ledPins[4], HIGH);
delay(delayTime);
digitalWrite(ledPins[5], HIGH);
delay(delayTime);
digitalWrite(ledPins[6], HIGH);
delay(delayTime);
digitalWrite(ledPins[7], HIGH);
delay(delayTime);
digitalWrite(ledPins[8], HIGH);
delay(delayTime);
digitalWrite(ledPins[9], HIGH);
delay(delayTime);

// This turns off all the LED after a certain amount of time as definied by the delay time, this applies to the
above code.
digitalWrite(ledPins[8], LOW);
delay(delayTime);
digitalWrite(ledPins[7], LOW);
delay(delayTime);
digitalWrite(ledPins[6], LOW);
delay(delayTime);
digitalWrite(ledPins[5], LOW);
delay(delayTime);
digitalWrite(ledPins[4], LOW);
delay(delayTime);
digitalWrite(ledPins[3], LOW);
delay(delayTime);
digitalWrite(ledPins[2], LOW);
delay(delayTime);
digitalWrite(ledPins[1], LOW);
delay(delayTime);
digitalWrite(ledPins[0], LOW);
delay(delayTime);
}

void oneAfterAnotherLoop() // Commands the LEDs to follow the above orders


{
int index;
int delayTime = 100; // Time of wait between LED's turning on and off in milliseconds

for(index = 0; index <=8 ; index++) // Steps through each LED and turns it on
{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
}

for(index = 8; index >= 0; index--) //Steps through each LED turning it on


{
digitalWrite(ledPins[index], LOW);
delay(delayTime);
}
}

void oneOnAtATime() // Lights each LED one at a time


{
int index;
int delayTime = 100;

for(index = 0; index <= 8; index++)


{
digitalWrite(ledPins[index], HIGH); // Activates LED
delay(delayTime); // Time inbetween
digitalWrite(ledPins[index], LOW); // Deactivates LED
}
}

void pingPong() //Turns on the pingpong command


{
int index;
int delayTime = 100;

for(index = 0; index <= 8; index++)


{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
digitalWrite(ledPins[index], LOW);
}

for(index = 8; index >= 0; index--)


{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
digitalWrite(ledPins[index], LOW);
}
}

void marquee() // Will activvate the previously mentioned marquee


{
int index;
int delayTime = 200;

for(index = 0; index <= 5; index++) // Lights up

{
digitalWrite(ledPins[index], HIGH); // Makes an LED turn on
digitalWrite(ledPins[index+4], HIGH); // Skips 4 of the LED's and turns the next one on
delay(delayTime); // Pause to slow down the sequence
digitalWrite(ledPins[index], LOW); // Makes the LED turn on
digitalWrite(ledPins[index+4], LOW); // Skips 4 of the LED's and turns the next one off
}
}

void randomLED()
{
int index;
int delayTime;

index = random(8); // pick a random LED to light up


delayTime = 100;

digitalWrite(ledPins[index], HIGH); // Makes the led go on


delay(delayTime); // Creates a pause between the comands
digitalWrite(ledPins[index], LOW); // Makes the led go off
}

You might also like