Stop the sound when there's no ultrasonic input

Good afternoon,

I’ve already done the code to create a progressive tone when it changes the distance input from ultrasonic sensor.
So, I want to know how to make to stop the tone (sound) when there’s no interference in the distance input from the ultrasonic sensor.

Thanks :slight_smile:

#define MIN_FREQ 30
#define MAX_FREQ 80

int echoPin =  7;
int trigPin =  8;
int soundPin = 12; // Piezo

int maximumRange = 100 ;
int minimumRange = 0;
long duration, distance;



void setup() {
Serial.begin (9600); // Init communications to serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
  }

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
// Yellow led indicated out of maximumRange. Prints "Out Of Range" to serial if target is outside maximumRange.
}

else {
// When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED.

Serial.print(distance);
int freq = map(distance, 0, 50, MIN_FREQ, MAX_FREQ);// al cuadrat log expo-
    freq = constrain(freq, MIN_FREQ, MAX_FREQ);
    tone(12, freq);
delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones.
noTone(0);



}

// Mandatory delay
delay(50);
}

Use noTone()

if (distance >= maximumRange)
noTone(0);

i try that but doesnt stop when go maximum

OK, keep trying thinking.

Good luck.

Perhaps only call noTone:

if (distance >= maximumRange)
{ 
  noTone(0);
  return;   // don't try to do anything else
}

in your code,

you start tone on pin12, right?

tone(12, freq);

but

noTone(0); //????? you are disabling tone on pin 0 here!!!!!

Thanks for your reply,

but it still not working… :o
As yous see I create an if that disable the tone as you said, but it’s not working :frowning:

#define MIN_FREQ 30
#define MAX_FREQ 80

int echoPin =  7;
int trigPin =  8;
int soundPin = 12; // Piezo

int maximumRange = 100 ;
int minimumRange = 0;
long duration, distance;



void setup() {
Serial.begin (9600); // Init communications to serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
  }

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;


if (distance >= maximumRange || distance <= minimumRange){
// Yellow led indicated out of maximumRange. Prints "Out Of Range" to serial if target is outside maximumRange.

}

else {
// When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED.

Serial.print(distance);
int freq = map(distance, 0, 50, MIN_FREQ, MAX_FREQ);// al cuadrat log expo-
    freq = constrain(freq, MIN_FREQ, MAX_FREQ);
    tone(12, freq);
delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones.

}

if (distance = maximumRange){
noTone(12);
return;
}

// Mandatory delay
delay(50);
}
if (distance = maximumRange){

Please, put this down, and go off and work through some basic examples

Why are you wasting your time looking at my profile?