(fingertones)
I made this instrument in one class. It has two LDRs, one 1k resistor, one 10k, and one 100Ω. two speakers, and one button. one speaker for each LDR, and a button to turn the sound on or off. It plays different notes according to the light. When it's surroundings are dark, it plays higher notes, and when it's surroundings are light, it will not make any noise, or the noise will be lower.
Code
#include <Tone.h>
Tone speaker;
Tone speaker2;
int btn1 = 10; // switch is connected to pin 2
int val; // variable for reading the pin status
int btnState; // variable to hold the last button state
boolean lightOn=false;
void setup() {
pinMode( btn1, INPUT); // Set the switch pin as input
speaker.begin(7);
speaker2.begin(9);
btnState = digitalRead(btn1); // read the initial state
Serial.begin(9600);
}
void loop(){
val = digitalRead( btn1); // read input value and store it in val
if (val != btnState) { // the button state has changed!
if (val == HIGH) { // check if the button is pressed
lightOn=!lightOn;
}
}
//control the light here
if(lightOn){
Serial.println("here");
playMusic();
}else{
Serial.println("there");
speaker.stop();
speaker2.stop();
}
btnState = val; // save the new state in our variable
}
void playMusic(){
int val=analogRead(A0);
int val2=analogRead(A1);
Serial.println(val);
if(val<450){
val=map(val,50,449,12,0);
speaker.play(NOTE_C4+val*20);
// speaker.play(NOTE_C5+val*64);
delay(100);
}else{
speaker.stop();
}
//Serial.println(val2);
if(val2<500){
val2=map(val2,80,499,12,0);
speaker2.play(NOTE_C4+val2*20);
delay(100);
}else{
speaker2.stop();
}
}
Breadboard
Schematics
YOUR schematics PIC HERE
Video