ultrasonic sensor and piezo buzzer

Hi,

I’ve got a simple code I’m trying to develop. I’ve got the ultrasonic hc-sr04 ranging, and when it goes below a threshold, say, 10cm, the piezo is supposed to sound.

I’m getting some garbage values of 0.00 and it still seems to pass through the filter I made, specifically to make sure that it doesn’t (e.g. above 0.00 for the ultrasonic sensor readings).

What’s occurring is the buzzer is going off each time the sensor pings, regardless of the reading.

Any ideas?

/* Ping sonar.  If below or within threshold, play buzzer.
 *  
 *  source of buzzer code:  https://bitbucket.org/teckel12/arduino-new-tone/wiki/Home
 *  
 * WIRING
 * 
 * Sonar:
 *   Trigger_pin on 2
 *   Echo_pin    on 3
 *
 * Buzzer:
   buzzer power (red wire) on 8
 *   
 */  

// LIBRARIES
#include <NewTone.h>
#include <NewPing.h>  // sonar library

// Sonar setup
#define TRIGGER_PIN  2  // Arduino trigger pin on sonar
#define ECHO_PIN     3  // Arduino echo pin on sonar
#define MAX_DISTANCE 200 /* Maximum distance we want to ping for (in centimeters). 
                            Maximum sensor distance is rated at 400-500cm.
                            default setting = 200
                         */

// Buzzer setup
#define tone_pin 8 // Pin you have speaker/piezo connected to (be sure to include a 100 ohm resistor)  


// SONAR
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

int sonar_delay = 40; // Wait 50ms between pings (~ 20 pings/sec). 29ms should be min delay between pings
int MIN_DISTANCE = 15;     // (cm) sets min sonar distance, which is used in threshold checks for spinning motors
int uS;                  // send ping, get returned time in microseconds
double sonar_distance;    // 

// BUZZER
int tone_frequency = 100;  // frequency of tone, from low to high about 50-15000 Hz
int tone_time = 300;      // time in ms that tone should play for

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{

 // SONAR
  delay(sonar_delay);    
  
  uS = sonar.ping(); // Send ping, get ping time in microseconds (uS)
  sonar_distance = (uS / US_ROUNDTRIP_CM) / 2.54; 


// discard garbage value of 0.00
   if (1.00 < sonar_distance < 10.00)
     {
       // print sonar value
       Serial.print("sonar distance = ");
       Serial.println(sonar_distance);

       // play buzzer 
       NewTone(tone_pin, tone_frequency); // Play the frequency (125 Hz to 15 kHz sweep in 10 Hz steps).
       delay(tone_time); // Wait 1 ms so you can hear it.
       noNewTone(tone_pin); // Turn off the tone.
       delay(100);
     }


  
}

if (1.00 < sonar_distance < 10.00)

Are you sure this does what you want it to do?

I’m not a great C++ programmer but I have no idea what the compiler would make of this line.

  if (1.00 < sonar_distance < 10.00)

Maybe split up the conditional distance comparison into a check for zero and then a check for less than 10cm.

My best guess is that it always evaluates to true.

1.00 < sonar_distance < 10.00
(1.00 < sonar_distance) < 10.00
true < 10.00 --> true
false < 10.00 --> true

Thanks for the tip off to the probable error line!

knut_ny:
Are you sure this does what you want it to do?

I needed the ‘AND’ condition in the ‘if’ statement. This cleared it up quickly:

 if ((0.00 < sonar_distance) && (sonar_distance < 10.00))