I need to put a loop to make a buzzer beep if my range finder goes out of range

#include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin = 8;
const int ledPin = 9;
const int buzzer = 8;
int buttonState = 0;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  
  if (distance >= 300 || distance <= 3){
    lcd.print("Out of range");
    delay(500);
    
  }
  else {
    lcd.print(distance);
    lcd.print(" cm");
    delay(500);
 }
  delay(500);
  lcd.clear();
}

Thank you for using code tags! What is the problem? Does the sketch work or not? If not, what is wrong?

This sketch works but I don't know how to add a loop in an if statement because I want it go beep on and off when it is our of range

That is called a 'for' statement. Consult the reference page, it's there.

I put the for statement in void loop right?

#include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin = 8;
const int ledPin = 9;
const int buzzer = 8;
int buttonState = 0;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  for (distance >= 300 || distance <= 3){
    tone(buzzer,1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  if (distance >= 300 || distance <= 3){
    lcd.print("Out of range");
    delay(500);
  
    
  }
  else {
    lcd.print(distance);
    lcd.print(" cm");
    delay(500);
 }
  delay(500);
  lcd.clear();
}

This will beep 3 times if out of range. However, I will warn you that your program completely stalls during delays.

#include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin = 8;
const int ledPin = 9;
const int buzzer = 8;
int buttonState = 0;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  if (distance >= 300 || distance <= 3) {
    lcd.print("Out of range");
    for (int i=0; i<3; i++)
    {
       tone(buzzer,1000);
       delay(500);
       noTone(buzzer);
       delay(500);
    }
  }
  else {
    lcd.print(distance);
    lcd.print(" cm");
    delay(500);
 }
  delay(500);
  lcd.clear();
}

You can put it in any function where you want to use it.

Didn't you like the answers on your other near-identical topic?

Punked again.

#include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin = 8;
const int ledPin = 9;
const int buzzer = 8;
int buttonState = 0;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  for (distance >= 300 || distance <= 3){
    tone(buzzer,1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  if (distance >= 300 || distance <= 3){
    lcd.print("Out of range");
    delay(500);
  
    
  }
  else {
    lcd.print(distance);
    lcd.print(" cm");
    delay(500);
 }
  delay(500);
  lcd.clear();
}

Do you not have Google? There are, literally, thousands of tutorials on every single aspect of c and c++. What you have is not even close, and suggests you have made no effort at all to answer your own question.

Simply Google "c for loop".

The Arduino language reference has a section concerning the for loop structure.

Not proper syntax for a for loop.

Not the right place for a for loop.

In any case, if you do not recalculate or input or sense, whatever, something must change distance or you won't get out of the loop.

A while loop is a way to go:

  while (distance >= 300 || distance <= 3) {
    tone(buzzer,1000);
    delay(500);
    noTone(buzzer);
    delay(500);

    distance = … // you must perform whatever sets distance again!

  }

a7

duplicate question here, I need to put a loop to make a buzzer beep if my range finder goes out of range

Thank you so much for helping me this is my final for my engineering class and I'm very new to the code and you have explained this the best that I've found because I really didn't understand it

Where did you look?

By final exam time in any engineering class, I would expect one would be much better at finding answers to such simple questions...

Would be interesting to know EXACTLY what engineering course is involved. One to stay away from!
Paul

@argthur42,

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

Thank you.