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) {
  // We'll send debugging information via the Serial monitor
  pinMode(speaker, OUTPUT);

void loop(void) {
  psrReading = analogRead(6);
  psrReading = analogRead(6);
  Serial.print("\n psr reading = ");
  fsrReading = analogRead(10);
  fsrReading = analogRead(10);
  Serial.print("\n fsr reading : ");
  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?

  • Which board are you using? 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? Oct 20, 2016 at 13:34

2 Answers 2


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).


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


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:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.