2

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?

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Oct 20, 2016 at 6:21
2
  • Which board are you using? Commented Oct 20, 2016 at 11:18
  • 2
    You cannot take two analog readings simultaneously, but you can take them one after the other, just like you did. That being said, I see nothing wrong in your code. Are you sure you have no wiring problems? Commented Oct 20, 2016 at 13:34

2 Answers 2

1

This is not a full code but more of an explanation of the logic of your program.

when the flex sensor is triggered, a sound is played on the piezzo buzzer. The tone it makes is dependent the values received from the proximity sensor.

Follow this logic and try to code it:

1- read the flex sensor values

int val = analogRead(flexPin);

2- if it is triggered, THEN read the proximity sensor.

if(val > minimum) {
 int distance = analogRead(proximityPin); //this line depends on which proximity sensor you are using

3- play the sound accordingly to the proximity sensor (still in the if loop).

analogWrite(distance);

This logic should work fine. If you could include more info like board, flex sensor and proximity sensor, that could help.

answered Oct 20, 2016 at 11:56
1

You can try using Interrupts instead of polling the sensor values, as interrupt will only be activated during rising-edge or falling edge. More info on interrupts:

answered Oct 20, 2016 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.