user27536: 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?

user27157: 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.

user20158: 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:

http://playground.arduino.cc/Code/Interrupts
https://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/


27536: <p>I am trying to create a simple piano playback using a flex sensor, a proximity sensor and a piezo buzzer.</p>

<p>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.</p>

<p>Here is my code:</p>

<pre><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 &lt; 980 &amp;&amp; psrReading &gt; 599){
    tone(9, 400, 1000);
  } else if (fsrReading &lt; 980 &amp;&amp; psrReading &gt; 500){
    tone(9, 500, 1000);
  } else if (fsrReading &lt; 980 &amp;&amp; psrReading &lt; 392){
    tone(9, 600, 1000);
  }
}
</code></pre>

<p>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.</p>

<p>How can I get it so that it reads both values instead of just one?</p>
 27157: <p>This is not a full code but more of an explanation of the logic of your program.</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>Follow this logic and try to code it:</p>

<p>1- read the flex sensor values</p>

<pre><code>int val = analogRead(flexPin);
</code></pre>

<p>2- if it is triggered, THEN read the proximity sensor.</p>

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

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

<pre><code>analogWrite(distance);
</code></pre>

<p>This logic should work fine. If you could include more info like board, flex sensor and proximity sensor, that could help.</p>
 20158: <p>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:</p>

<ul>
<li><a href="http://playground.arduino.cc/Code/Interrupts" rel="nofollow">http://playground.arduino.cc/Code/Interrupts</a></li>
<li><a href="https://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/" rel="nofollow">https://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/</a></li>
</ul>
