You are on page 1of 5

Arduino Code

//lights
const int RED_PIN = 9; //wire of light is connected to pin 9
const int GREEN_PIN = 6; //wire of light is connected to pin 6
const int BLUE_PIN = 10; //wire of light is connected to pin 10
int DISPLAY_TIME = 100; //how long each color is shown
//music box
//note frequency
//c 262 Hz
//d 294 Hz
//e 330 Hz
//f 349 Hz
//g 392 Hz
//a 440 Hz
//b 494 Hz
//C 523 Hz
//D 311.13 Hz //D means d sharp
//B 246.94 Hz
//A 220 Hz
const int buzzerPin = 8; //wire for buzzer is connected to pin 8
const int songLength = 9; //song length
char notes[] = "eDeDeBdcA"; // notes played in order (fur elise)
int beats[] = {1,1,1,1,1,1,1,1,3}; //number of beats each note is played
int tempo = 300; //how fast each the beat is (tempo)
//word board
#include <LiquidCrystal.h> //this allows the arduino board to write things on the liquid crystal display
LiquidCrystal lcd(12,11,5,4,3,2); //the number of the pins the lcd is plugged into
void setup() //start with these codes
{
// put your setup code here, to run once:
//lights
pinMode(RED_PIN, OUTPUT); //the red pin is being used as an output
pinMode(GREEN_PIN, OUTPUT); //the green pin is being used as an output
pinMode(BLUE_PIN, OUTPUT); //the blue pin is being used as an output
//music box
pinMode(buzzerPin, OUTPUT); //the buzzer pin is being used as an output
//word board
lcd.begin(16, 2); //start typing
lcd.clear(); //clear all words
lcd.setCursor(0,0); //set the cursor
lcd.print(" Robot Art Show "); // write what you want written
lcd.setCursor(0,1); //set the cursor
lcd.print("
"); // write what you want written
}

void loop() //repeat these codes


{
//word board
delay(3000); //pause for 3 seconds
lcd.setCursor(0,0); //set the cursor
lcd.print(" Robot Art Show "); // write what you want written
lcd.setCursor(0,1); //set the cursor
lcd.print("
"); // write what you want written
delay(3000); //pause for 3 seconds
lcd.setCursor(0,0); //set the cursor
lcd.print(" Hello world! "); // write what you want written
lcd.setCursor(0,1); //set the cursor
lcd.print(" How are you? "); // write what you want written
lcd.clear(); //clear all words
lcd.setCursor(0,0); //set the cursor
lcd.print("
By:
"); // write what you want written
lcd.setCursor(0,1); //set the cursor
lcd.print("Rebecca&Marilynn"); // write what you want written
delay(3000); //pause for 3 seconds
lcd.clear(); //clear all words
lcd.setCursor(0,0); //set the cursor
lcd.print(" Hello world! "); // write what you want written
lcd.setCursor(0,1); //set the cursor
lcd.print(" How are you? "); // write what you want written
delay(1500); //pause for 1.5 seconds
//music box
int i, duration; //initial song length
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in measure
if (notes[i] == ' ')
// this is a rest
{
delay(duration);
// pause for a moment
}
else if (notes[i] == 'e')
// turn on this color with this note
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH); //turn on BLUE and RED to create purple
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
// wait for note to finish
}
else if (notes[i] == 'D')
// turn on this color with this note
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH); //turn on ALL to create WHITE
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
// wait for note to finish
}

else if (notes[i] == 'B')


// turn on this color with this note
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW); //turn on green and blue to create GREEN
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
// wait for note to finish
}
else if (notes[i] == 'd')
// turn on this color with this note
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW); //turn on red and green to create light green
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
// wait for note to finish
}
else if (notes[i] == 'c')
// turn on this color with this note
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH); //turn on blue to create blue
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
// wait for note to finish
}
else if (notes[i] == 'A')
// turn on this color with this note
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH); //turn on green and blue to create light blue
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
// wait for note to finish
}
delay(tempo/10);

// brief pause between notes

}
}
//lights
void mainColors() //the writing below in brackets controls the brightness of each bulb
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH); //turn on all lights to create white
delay(1000); //pause for a second
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH); //turn on blue to create blue
delay(1000); //pause for a second
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW); //turn on red to create red

delay(1000); //pause for a second


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH); //turn on red and blue to create purple
delay(1000); //pause for a second
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW); //turn on red and green to create light green
delay(1000); //pause for a second
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW); //turn on green to create green
delay(1000); //pause for a second
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH); //turn on green and blue to create light blue
delay(1000); //pause for a second
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH); //turn on red and blue to create purple
delay(1000); //pause for a second
}
void showSpectrum()
{
int x; // define an integer variable called "x"
{
showRGB(x); // Call RGBspectrum() with our new x
delay(10); // pause for 1/100th of a second
}
}
void showRGB(int color)
{
int redIntensity; // brightness of red
int greenIntensity; // brightness of green
int blueIntensity; // brightness of blue
if (color <= 255)
// zone 1
{
redIntensity = 255 - color; // red goes from on to off
greenIntensity = color;
// green goes from off to on
blueIntensity = 0;
// blue is always off
}
else if (color <= 511) // zone 2
{
redIntensity = 0;
// red is always off
greenIntensity = 255 - (color - 256); // green on to off

blueIntensity = (color - 256);


// blue off to on
}
else // color >= 512
// zone 3
{
redIntensity = (color - 512);
// red off to on
greenIntensity = 0;
// green is always off
blueIntensity = 255 - (color - 512); // blue on to off
}
}
//music box
int frequency(char note)
{
// This function takes a note character (a-g), and returns the corresponding frequency in Hz for the tone()
function.
int i;
const int numNotes = 11; // number of notes we're storing
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'B', 'A' }; // names of our notes
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523, 311.13, 246.94, 220}; // frequencies of each note
for (i = 0; i < numNotes; i++) // i increases by one after the number of notes stated
{
if (names[i] == note)
// the names of the notes are equal to the frequencies
{
return(frequencies[i]); // Return the frequency
}
}
return(0); // Return to 0.
}

You might also like