Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Assignment: Input / Output Coincidence Lab Assignment
Collaborators: jin0305
Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Description
For this assignment, Jin and I collaborated to design a garden weather chime that 'chimes' nursery rhymes based on the weather. If the weather is sunny, it chimes a happy "The Mulberry Bush". When the weather is windy or it is raining, it chimes "London Bridge" and "Rain, Rain go away" respectively. On a cloudy day, it chimes "I hear thunder." The chime also has built-in LEDs that flash as it chimes. In the night, it dosen't sing anymore but continues flashing lights. The chime is also painted to camouflage it in the garden foliage.
For the circuit, we used an FSR and a Photocell to detect the 'light' of the day and the 'pressure' (esp. for windy and rainy days) and play the appropriate song. We also used LEDs for the flashing lights.
Components Used
Arduino
FSR, Photocell
Peizo Speaker
LEDs
220 Ohm Resistors
Wires
Card Stock
Plant
Arduino Code
/*
*
* 4 song Piezo Speaker toggle, for use inside a garden chime.
*
* Photocell and FSR input toggle songs to play
*Created by Jin Baik and Janani Vasudev, October 2009
*/
int speakerPin = 7;
//byte names[] = {'G', 'A', 'B', 'c', 'd', 'e', 'f', 'g', 'a'};
//int tones[] = {2560, 2311, 2090, 1915, 1700, 1519, 1432, 1275, 1136};
int windyLength = 25; // the number of windy notes
char windyNotes[] = "gagfefgdefefggagfefgdgec "; // a space represents a rest
int windyBeats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 4 };
int windyTempo = 300;
int rainyLength = 32; // the number of rainy notes
char rainyNotes[] = "geggegeggeggeaggegfedcdegfedcde ";
int rainyBeats[] = { 2, 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int rainyTempo = 250;
int sunnyLength = 27; // the number of sunny notes
char sunnyNotes[] = "cccegecdddBAGcccegecddGABcc ";
int sunnyBeats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 4 };
int sunnyTempo = 150;
int cloudyLength = 33; // the number of cloudy notes
char cloudyNotes[] = "cdeccdecefgefggagfecgagfecdGcdGc "; // a space represents a rest
int cloudyBeats[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 4, 2, 2, 4, 4 };
int cloudyTempo = 200;
// LED Output
int bluePin = 12; // Blue LED connected to digital pin 12
int greenPin = 11; //Green LED connected to digital pin 11
int redPin = 13; // LED LED connected to digital pin 13
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24
;
int statePin = LOW;
// Analog pin settings
int photocellIn = 0;
int fsrIn = 1;
// Values
int photocellVal = 0;
int fsrVal = 0;
int checkSum = 0; // Aggregate pot values
int prevCheckSum = 0;
int sens = 3; // Sensitivity theshold, to prevent small changes in
// pot values from triggering false reporting
// FLAGS
int PRINT = 1; // Set to 1 to output values
int DEBUG = 0; // Set to 1 to turn on debugging output
void setup()
{
pinMode(speakerPin , OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = {'G', 'A', 'B', 'c', 'd', 'e', 'f', 'g', 'a'};
int tones[] = {2560, 2311, 2090, 1915, 1700, 1519, 1432, 1275, 1136};
//char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
//int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i <= 9; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void loop()
{
digitalWrite(bluePin, HIGH); // set the Blue LED on
delay(100); // wait for a second
digitalWrite(greenPin, HIGH); // set the Green LED on
delay(100); // wait for a second
digitalWrite(redPin, HIGH); // set the Red LED on
delay(100); // wait for a second
digitalWrite(bluePin, LOW); // set the Blue LED off
delay(100); // wait for a second
digitalWrite(greenPin, LOW); // set the Green LED off
delay(100);
digitalWrite(redPin, LOW); // set the Red LED off
delay(100);
photocellVal = analogRead(photocellIn); // read input pins
fsrVal = analogRead(fsrIn);
Serial.print("Photocell:"); // ...then print the values.
Serial.print(photocellVal);
Serial.print("\t");
Serial.print("FSR: ");
Serial.print(fsrVal);
Serial.println();
if (photocellVal > 70 && fsrVal ==0) { //If the weather is sunny
Serial.println("Play the Sunny tune!");
// Play Start
for (int i = 0; i < sunnyLength; i++) {
if (sunnyNotes[i] == ' ') {
delay(sunnyBeats[i] * sunnyTempo); // rest
} else {
playNote(sunnyNotes[i], sunnyBeats[i] * sunnyTempo);
}
// pause between notes
delay(sunnyTempo / 2);
}
}
else if (photocellVal>0 && photocellVal<70 && fsrVal==0) { // If the weather is cloudy...
Serial.println("Play the Cloudy tune!");
// Play Start
for (int i = 0; i < cloudyLength; i++) {
if (cloudyNotes[i] == ' ') {
delay(cloudyBeats[i] * cloudyTempo); // rest
} else {
playNote(cloudyNotes[i], cloudyBeats[i] * cloudyTempo);
}
// pause between notes
delay(cloudyTempo / 2);
}
//Play End
}
else if (photocellVal >20 && photocellVal <70 && fsrVal > 0 && fsrVal < 200) { // If the weather is windy...
Serial.println("Play the Windy tune!");
// Play Start
for (int i = 0; i < windyLength; i++) {
if (windyNotes[i] == ' ') {
delay(windyBeats[i] * windyTempo); // rest
} else {
playNote(windyNotes[i], windyBeats[i] * windyTempo);
}
// pause between notes
delay(windyTempo / 2);
}
//Play End
}
else if (photocellVal >20 && photocellVal <70 && fsrVal > 200) { // If the weather is rainy...
Serial.println("Play the Rainy tune!");
// Play Start
for (int i = 0; i < rainyLength; i++) {
if (rainyNotes[i] == ' ') {
delay(rainyBeats[i] * rainyTempo); // rest
} else {
playNote(rainyNotes[i], rainyBeats[i] * rainyTempo);
}
// pause between notes
delay(rainyTempo / 2);
}
} else { //If it is nighttime
Serial.println("Play No tune!");
}
}