If statement not working esp32

Hi ,I was doing a radar project from ESP32 with HC-SR04 Ultrasonic Sensor with Arduino IDE | Random Nerd Tutorials and I am trying to make a buzzer turn on when it is between 5-16 but if is not the buzzer has to stop but this does not work.
Here is my Code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int trigPin = 5;
const int echoPin = 18;

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
int distanceCm;
int distanceInch;


int buzzPin = 17;
int buzzTime = 1;
int buzzTime4 = 4;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buzzPin, OUTPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(500);
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distanceCm = duration * SOUND_SPEED / 2;

  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;

  // Prints the distance in the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);

  display.clearDisplay();
  display.setCursor(0, 0);
  //  Display distance in cm
  display.print(distanceCm);
  display.print(" cm");

  // Display distance in inches
  display.setCursor(0, 15);
  display.print(distanceInch);
  display.print(" in");
  display.display();

  // Display :)
  display.setCursor(0, 30);
  display.print(":)");
  display.display();

  // Display Good
  display.setCursor(0, 45);
  display.print("Good");
  display.display();


  // Display ----
  display.setCursor(0, 55);
  display.print("----");
  display.display();

  if (distanceCm > 5 and  distanceCm < 16) {
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(buzzTime);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(buzzTime);
  }

  delay(500);
}

`Please can someone help me, Thanks.`

Define, "does not work".

please, explain this

If the distance is not for 5-16 it should stop.

"it should stop" - i don't see anything similar in your code

This is the part.

if (distanceCm > 5 &&  distanceCm < 16) {

I noticed.

it doesn't matter, "and" is C++ synonym for "&&"
https://en.cppreference.com/w/cpp/keyword/and

But I agree it's better to use && to avoid confusion

Does that work? My bad.

What does the serial print outs say for distanceCm?

@foltynfan, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

How is your buzzer wired?

This code sets it HIGH for 1 millionth of a second then LOW again. Is that what you want?

Maybe you need an...

 else
    digitalWrite(buzzPin, LOW);
int buzzTime = 1; 

if (distanceCm > 5 and  distanceCm < 16) {
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(buzzTime);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(buzzTime);
  }

Is your buzzer active HIGH or LOW?
With buzzTime at 1 microsecond what do you expect to hear?

If the buzzer is active LOW than that might explain why you don't hear the buzzer shut off.

To test that your if() is working or not, sop is to put a serial print in there to say "hello" or something.

But you start with:

... then you have:

I'm pretty sure you won't notice the buzzer on for such a short time.

edit: @cattledog beat me to it.

Okay that is what I want and I will try this out.

I can explain if you quote out the part of my code.

@dead_parrot I printed out something and it shows up Microsoft (I told it to print that) if the statement is true but the buzzer does not work.

Ok that at least proves the distance sensor is working and the logic takes you where you expect.

But as both I and cattledog pointed out, the delay of 1 microsecond is almost infinitesimal.

Try making that a second or so. (Use delay() rather than delayMicroseconds() then you will type fewer 0's :slightly_smiling_face: )

And do you know for a fact that the buzzer works?

Like this:

delay(buzzTime);

Yes but at the top where you have this:

int buzzTime = 1;

... you want that to be (say):

int buzzTime = 1000;

Right now you have it as 1 microsecond, and you probably want it to be say 1 second so it's long enough to notice, so that's either 1000000 micro- or 1000 milli-seconds.