/* * A simple programme that will produce both light and sound * based on the amount of light detected on the photo resister. * This example uses 8 LED's, a Piezo speaker and * the photo resister from the ARDX KIT (oomlout.com) * You can also open the 'Serial Monitor' to view to values * of resistance that the phot resistor is reporting. * theMcMurray's (Ian, Scott and Toby) Oct 2009 */ //PhotoResistor Pin int lightPin = 0; //the analog pin the photoresistor is //connected to //the photoresistor is not calibrated to any units so //this is simply a raw sensor value (relative light) //LED Pins int ledPins[] = {2,3,4,5,6,7,8,9}; //Piezo Pin int piezoPin = 11; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(piezoPin, HIGH); delayMicroseconds(tone); digitalWrite(piezoPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'B', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 2000, 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { Serial.begin(9600); for(int i=0; i < 8; i++) { pinMode(ledPins[i], OUTPUT); } pinMode(piezoPin, OUTPUT); } /* * loop() - this function will start after setup * finishes and then repeat */ void loop() { int lightLevel = analogRead(lightPin); //Read the // lightlevel lightLevel = map(lightLevel, 0, 900, 0, 255); //adjust the value 0 to 900 to //span 0 to 255 lightLevel = constrain(lightLevel, 0, 255);//make sure the //value is betwween //0 and 255 Serial.println(lightLevel); char playMe = 'B'; if ( lightLevel > 30 ) { digitalWrite(9, HIGH); playMe = 'c'; } else { digitalWrite(9, LOW); } if ( lightLevel > 60 ) { digitalWrite(8, HIGH); playMe = 'd'; } else { digitalWrite(8, LOW); } if ( lightLevel > 90 ) { digitalWrite(7, HIGH); playMe = 'e'; } else { digitalWrite(7, LOW); } if ( lightLevel > 120 ) { digitalWrite(6, HIGH); playMe = 'f';} else { digitalWrite(6, LOW); } if ( lightLevel > 150 ) { digitalWrite(5, HIGH); playMe = 'g';} else { digitalWrite(5, LOW); } if ( lightLevel > 180 ) { digitalWrite(4, HIGH); playMe = 'a';} else { digitalWrite(4, LOW); } if ( lightLevel > 210 ) { digitalWrite(3, HIGH); playMe = 'b';} else { digitalWrite(3, LOW); } if ( lightLevel > 240 ) { digitalWrite(2, HIGH); playMe = 'C';} else { digitalWrite(2, LOW); } playNote(playMe,50); analogWrite(piezoPin, lightLevel); //write the value }