You are on page 1of 5

//Cantina Band Theme w/ Servos and Lights

#include <Servo.h> //Include the already made servo library

Servo servo1; //Create servo1 as object

const int buzzerPin = 9; //Setting pinslot 9 as buzzerPin

const int songLength = 139; //Setting the song length in characters

char notes[] = "FBFBFBFfFFfFe EeEdbFBFBFBFfFeeeEeAGFeFBFBFBFfFAAFedbbdFACBfFd


FdF FdF FdfFdb FdF FdF FdfFea FdF FdF FdfFdbgbdobdfFbbdGBfFd ";
//Telling what notes in series to play, spaces are rests

int beats[] =
{2,2,2,2,1,2,2,1,2,1,1,1,1,1,1,1,1,3,5,2,2,2,2,1,2,2,1,2,4,4,1,1,2,2,2,2,2,2,2,2,2,1,2,2,1,2,2,2,1,2,
3,5,4,4,4,4,2,2,1,2,1,2,2,4,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,4,5,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,5,
4,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,4,5,1,1,2,1,1,2,1,2,5,1,1,1,1,1,2,7,2,20,20};
//Setting the duration for each note, 1 = eigth note, 2 = quarter note, etc.

int tempo = 75; //Setting variable "tempo" in milliseconds

void setup() //Sets up all pins


{
pinMode(buzzerPin, OUTPUT); //Setting variable "buzzerPin" as an output
pinMode(2, OUTPUT); //Setting pinslot 2 as an output
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
servo1.attach(12); //Attaches servo1 to pinslot 12
}

void loop() //Repeats everything inside


{
int i, duration; //Setting i and duration as integers

for (i = 0; i < songLength; i++) //Plays through each note one by one
{
duration = beats[i] * tempo; //Length of a note/rest in milliseconds
if (notes[i] == ' ') //If detects a space (rest)
{
delay(duration); //Then pause for a moment
}

if (notes[i] == 'g') //If detects note (in quotes) then...


{
digitalWrite(2, HIGH); //Turns light in pinslot 2 on
servo1.write(0); //Moves servo to 0 degrees
tone(buzzerPin, frequency(notes[i]), duration);
//Playing note on buzzerPin, from frequency(notes[i]), for a duration
delay(duration); //Delays for the duration
digitalWrite(2, LOW); //Turns light in pinslot 2 off
}

if (notes[i] == 'f')
{
digitalWrite(2, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(2, LOW);
}

if (notes[i] == 'B')
{
digitalWrite(2, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(2, LOW);
}

if (notes[i] == 'c')
{
digitalWrite(2, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(2, LOW);
}

if (notes[i] == 'o')
{
digitalWrite(3, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(3, LOW);
}

if (notes[i] == 'C')
{
digitalWrite(3, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(3, LOW);
}

if (notes[i] == 'F')
{
digitalWrite(3, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(3, LOW);
}

if (notes[i] == 'd')
{
digitalWrite(3, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(3, LOW);
}

if (notes[i] == 'a')
{
digitalWrite(4, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(4, LOW);
}

if (notes[i] == 'G')
{
digitalWrite(4, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(4, LOW);
}

if (notes[i] == 'D')
{
digitalWrite(4, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(4, LOW);
}

if (notes[i] == 'E')
{
digitalWrite(4, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(4, LOW);
}

if (notes[i] == 'b')
{
digitalWrite(5, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(5, LOW);
}

if (notes[i] == 'A')
{
digitalWrite(5, HIGH);
servo1.write(0);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(5, LOW);
}
if (notes[i] == 'e')
{
digitalWrite(5, HIGH);
servo1.write(20);
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
digitalWrite(5, LOW);
}
else //Otherwise...
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10); //Pause between notes
}
{

//Repeats song over and over

}
}

int frequency(char note)


//Takes a note character and recieves a hertz tone for each one
{
int i;
const int numNotes = 15; //Number of notes being stored
char names[] = { 'F', 'B', 'f', 'e', 'E', 'd', 'b', 'A', 'G', 'C', 'a', 'c', 'g', 'D', 'o' };
//The character variables for each frequency
int frequencies[] = {740, 988, 698, 659, 622, 587, 494, 880, 784, 1047, 440, 554, 392, 1175,
415};
//The frequencies for each note

for (i = 0; i < numNotes; i++)


{
if (names[i] == note) //If find note
{
return(frequencies[i]); //Return the frequency
}
}
return(0); //Looked through everything, didn't find anything, play nothing, return to 0
}

You might also like