Photoresistor scale Video
For this demo I set up a string of photoresistors to act as switches for a ‘keyboard’ of lights corresponding to notes in a scale. When played individually the speaker produces the same tone, but when multiple are covered at a time you can play a scale, and thirds play a rhythm!
Code a combination and alteration of different pieces from the Arduino Example library.
____________________________________________________________________________________
#define C 262
#define D 294
#define E 330
#define F 349
#define G 392
int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
int sensorPin1 = A1;
int ledPin1 = 12;
int sensorValue1 = 0;
int sensorPin2 = A2;
int ledPin2 = 11;
int sensorValue2 = 0;
int sensorPin3 = A3;
int ledPin3 = 10;
int sensorValue3 = 0;
int sensorPin4 = A4;
int ledPin4 = 9;
int sensorValue4 = 0;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
sensorValue4 = analogRead(sensorPin4);
if (sensorValue <= 20){
digitalWrite(ledPin, LOW);
tone(6, C, 200);
}
else {
digitalWrite(ledPin, HIGH);
noTone(6);
}
//light2
if (sensorValue1 <= 20){
digitalWrite(ledPin1, LOW);
tone(6, D, 150);
}
else {
digitalWrite(ledPin1, HIGH);
noTone(6);
}
//light3
if (sensorValue2 <= 20){
digitalWrite(ledPin2, LOW);
tone(6, E, 100);
}
else {
digitalWrite(ledPin2, HIGH);
noTone(6);
}
//light4
if (sensorValue3 <= 10){
digitalWrite(ledPin3, LOW);
tone(6, F, 80);
}
else {
digitalWrite(ledPin3, HIGH);
noTone(6);
}
//light 5
if (sensorValue4 <= 20){
digitalWrite(ledPin4, LOW);
tone(6, G, 100);
//delay(200);
}
else {
digitalWrite(ledPin4, HIGH);
noTone(6);
}
//beat 1
if ((sensorValue4 <= 20) && (sensorValue2 <=20)&& (sensorValue > 20)){
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin4, LOW);
tone(6, G, 100);
delay(200);
}
else {
digitalWrite(ledPin4, HIGH);
noTone(6);
}
//beat 2
if ((sensorValue3 <= 10) && (sensorValue1 <=20) && (sensorValue > 20)){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
tone(6, F, 100);
delay(200);
}
else {
digitalWrite(ledPin3, HIGH);
noTone(6);
}
//beat 3
if ((sensorValue2 <= 20) && (sensorValue <=20) && (sensorValue1 > 20)){
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin, LOW);
tone(6, E, 100);
delay(200);
}
else {
digitalWrite(ledPin2, HIGH);
noTone(6);
}
}
_______________________________________________________________________________________
Leave a Reply
You must be logged in to post a comment.