I am trying to create a simple piano playback using a flex sensor, a proximity sensor and a piezo buzzer.
I want to have it so that when the flex sensor is triggered, a sound is played on the piezo buzzer. The tone it makes is dependent on the values received from the proximity sensor.
Here is my code:
int psrAnalogPin = 6;
int fsrAnalogPin = 10;
int speaker = 9;
int fsrReading;
int psrReading;
void setup(void) {
Serial.begin(9600);
// We'll send debugging information via the Serial monitor
pinMode(speaker, OUTPUT);
}
void loop(void) {
psrReading = analogRead(6);
delay(10);
psrReading = analogRead(6);
delay(10);
Serial.print("\n psr reading = ");
Serial.println(psrReading);
fsrReading = analogRead(10);
delay(10);
fsrReading = analogRead(10);
delay(10);
Serial.print("\n fsr reading : ");
Serial.println(fsrReading);
if(fsrReading < 980 && psrReading > 599){
tone(9, 400, 1000);
} else if (fsrReading < 980 && psrReading > 500){
tone(9, 500, 1000);
} else if (fsrReading < 980 && psrReading < 392){
tone(9, 600, 1000);
}
}
When I look at the serial monitor's output, the values output seem to be only reading from my flex sensor(fsrReading) and not my proximity sensor. If I only read the proximity sensor's value it works, but not both at the same time.
How can I get it so that it reads both values instead of just one?