tonePitchFollower How to stop tone when input reaches zero

Hi and thank you for reading. I modified the "tonePitchFollower" project (in the IDE examples under "digital" tab) to use a softpot as a ribbon controller to play melodies. Its working great but I'm wondering if there's a way to get no tone until the softpot is pressed. In other words, I am playing in one octave, 440-880Hz, so I could make the range that plays those pitches 10-1023 and make 0-9 silent somehow but how? I've tried a few ways to do it but no success. I'd appreciate any help!

This sketch is what I'm using so far (on an Uno):

/*
  Pitch follower

  Plays a pitch that changes based on a changing analog input

  circuit:
  - 8 ohm speaker on digital pin 9
  - photoresistor on analog 0 to 5V
  - 4.7 kilohm resistor on analog 0 to ground

  created 21 Jan 2010
  modified 31 May 2012
  by Tom Igoe, with suggestion from Michael Flynn

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone2
*/

void setup() {
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below depending on the range
  // your sensor's giving:
  int thisPitch = map(sensorReading, 0, 1023, 440, 880);

  // play the pitch:
  tone(9, thisPitch, 10);
  delay(1);        // delay in between reads for stability
}

noTone()?

Thanks so much for the reply! noTone() sounds promising! I tried a few ways of implementing it but im a beginner with syntax. The code below didnt work but am I on the right track?

/*
Pitch follower

Plays a pitch that changes based on a changing analog input

circuit:

  • 8 ohm speaker on digital pin 9
  • photoresistor on analog 0 to 5V
  • 4.7 kilohm resistor on analog 0 to ground

created 21 Jan 2010
modified 31 May 2012
by Tom Igoe, with suggestion from Michael Flynn

This example code is in the public domain.

*/

void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
}

void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
// to the output pitch range (120 - 1500Hz)
// change the minimum and maximum input numbers below depending on the range
// your sensor's giving:
int thisPitch = map(sensorReading, 10, 1023, 440, 880);

// play the pitch:
tone(9, thisPitch, 10);
delay(1); // delay in between reads for stability
noTone(sensorReading, 0-9);
}

Thank you for the links, I read them and I'm getting closer (I think). This code returned error:

/var/folders/xg/kmsx7p8j0j1gl5wl80sgt86w0000gn/T/arduino_modified_sketch_993701/tonePitchFollower.ino: In function 'void loop()':
tonePitchFollower:39:21: error: expected ')' before numeric constant
if (sensorReading 0-9)
^
exit status 1
expected ')' before numeric constant

/*
  Pitch follower

  Plays a pitch that changes based on a changing analog input

  circuit:
  - 8 ohm speaker on digital pin 9
  - photoresistor on analog 0 to 5V
  - 4.7 kilohm resistor on analog 0 to ground

  created 21 Jan 2010
  modified 31 May 2012
  by Tom Igoe, with suggestion from Michael Flynn

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone2
*/

void setup() {
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below depending on the range
  // your sensor's giving:
  int thisPitch = map(sensorReading, 10, 1023, 440, 880);

  // play the pitch:
  tone(9, thisPitch, 10);
  delay(1);        // delay in between reads for stability
  if (sensorReading 0-9)
  {noTone(9);}
}

This time I didn't get an error! But I also didnt get "no tone". The serial monitor is reading 0 but there is a tone. Would any of you be able to explain how to fix that? Thanks so much!

/*
  Pitch follower

  Plays a pitch that changes based on a changing analog input

  circuit:
  - 8 ohm speaker on digital pin 9
  - photoresistor on analog 0 to 5V
  - 4.7 kilohm resistor on analog 0 to ground

  created 21 Jan 2010
  modified 31 May 2012
  by Tom Igoe, with suggestion from Michael Flynn

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone2
*/

void setup() {
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below depending on the range
  // your sensor's giving:
  int thisPitch = map(sensorReading, 10, 1023, 440, 880);

  // play the pitch:
  tone(9, thisPitch, 10);
  delay(1);        // delay in between reads for stability
  if (sensorReading <10)
  noTone(9);
}

Yes, your are getting the idea. I don't know where you got this from though

heyjudemw:
if (sensorReading 0-9)
{noTone(9);}
}

So here is another link C if...else Statement

Edit: you already got it :slight_smile:

Now read your own code line by line and figure out what exactly it does

I got it working! There is no tone until the pot is touched. Thank you so much for pointing me in the right direction, guys! The working code is below. Does it look right to you all?

/*
  Pitch follower

  Plays a pitch that changes based on a changing analog input

  circuit:
  - 8 ohm speaker on digital pin 9
  - photoresistor on analog 0 to 5V
  - 4.7 kilohm resistor on analog 0 to ground

  created 21 Jan 2010
  modified 31 May 2012
  by Tom Igoe, with suggestion from Michael Flynn

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone2
*/

void setup() {
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below depending on the range
  // your sensor's giving:
  int thisPitch = map(sensorReading, 0, 1023, 440, 880);

  // play the pitch:
  if (sensorReading >9){
  tone(9, thisPitch, 10);
    delay(1);  }
  if (sensorReading <10){
    noTone(9);}
  }