Description:
For this lab project, I created a plastic skull that reacts to its environment. The darker it is around it (or rather, the more its photocell is covered), the brighter a red LED on its inside gets. It will also play a tone of the C Major scale, depending on the brightness.
Components used:
- (1) Arduino Uno
- (1) Breadboard
- (1) Photocell
- (1) LED (red)
- (1) Piezo buzzer
- (1) 10k ohm resistor
- (1) 1k ohm resistor
- (several) cables
- (1) Plastic skull, filled with artificial spiderwebs
Code:
/* Skull alarm
* --------
*
* This project is about a plastic skull that lights up and plays a sound when you come closer
* and/or it gets dark. The sounds it plays correspond to the C Major scale.
*
* Created 15 October 2013
* Clemens Meyer <clemens.meyer@ischool.berkeley.edu>
*/
//Set input and output pins
int photoPin = 0;
int speakerPin = 7;
int redPin = 9;
// Set boundaries for playing music
int upperBoundary = 500;
int lowerBoundary = 200;
int range = upperBoundary - lowerBoundary;
// Set range of tones. Here: C Major scale
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int sound;
int val = 0;
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(redPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerPin, LOW);
// Read input from photocell
val = analogRead(photoPin);
// The darker the input, the brighter the red LED
analogWrite(redPin, 255-(val/4));
// Map different brightnesses to different tones from the scale provided in the tones array
for(int i = 0; i<sizeof(tones); i++) {
if(val < upperBoundary - i * range / sizeof(tones) ) {
sound = tones[i];
}
}
// Debugging output
Serial.print(val);
Serial.print("\t");
Serial.println(sound);
// Play tone
if(val<upperBoundary) {
for(int i=0; i<10; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(sound);
digitalWrite(speakerPin, LOW);
delayMicroseconds(sound);
}
}
}
- Login to post comments