DESCRIPTION
The goal was to combine input and output coincidences. I wanted to explore the relationship between distance and sound. Much like one of the example codes given in class, my project detects the closeness and the level of proximity determines, whether the piezo speaker spits out a certain melody. There are three ranges of light that the photocell detects. Each range plays a different tune and lights the corresponding LED light. The red LED will light up when the photocell detects the greatest amount of light. As you move your hand closer, and less light is detected, it will switch to the blue light. As it gets even further darker, the LED light will switch to the green LED. Each LED color corresponds to a different tune being played.
MATERIALS
1-Arduino Uno
1-Breadboard
1-Red LED
1-Green LED
1-Blue LED
1-USB Cable
1-Photocell
3-220 ohm resistor
1-Piezo Speaker
12-Wires
CODE
int potPin = 0; // select the input pin for the potentiometer
int speakerPin = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2c2d2f2g2a2b2c2d2f2d2a2c2d2a1f2c2d2a2a2g2p2p2p2p";
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 2;
int statePin = LOW;
int val = 0;
int redPin = 3;
int greenPin = 4;
int bluePin = 5;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerPin, LOW);
val = analogRead(potPin); // read value from the sensor
val = val*2;
Serial.println(val);
if (val <= 900) {
// red light
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
analogWrite(redPin, 250);
melody[0] = melody[12];
melody[1] = melody[13];
melody[2] = melody[14];
melody[3] = melody[15];
} else if (val <= 1200) {
// blue light
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 250);
melody[0] = melody[4];
melody[1] = melody[5];
melody[2] = melody[6];
melody[3] = melody[7];
} else if (val <= 2200) {
// green light
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
analogWrite(greenPin, 250);
melody[0] = melody[8];
melody[1] = melody[9];
melody[2] = melody[10];
melody[3] = melody[11];
}
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerPin,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count2]);
}
}
}
}
}
- Login to post comments