Go Down

Topic: Code a buzzer(DC6-24V) to beep at certain distances using arduino uno/ hc-sr04 ? (Read 3935 times) previous topic - next topic

wallman123

Can someone please tell me what I am doing wrong, and how I can fix it. Thanks
*Also pin 11 is where my buzzer is located.

Code: [Select]
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to arduino pin 7
Trig to arduino pin 8
*/

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int distalRange = 133; // Distal range needed
int proximalRange = 66; // Proximal range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

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 spped of sound.
 distance = duration/58.2;
 
 if (distance >= minimumRange and distance <= proximalRange){
 /* Continuous sound if the object is within 0-66 cm of sensor. */
 Serial.println(distance);
 digitalWrite(11,HIGH);
 }
 if (distance >= proximalRange and distance <= distalRange) {
 /* On and Off sound to idicate distance between 66-133 cm */
 Serial.println(distance);
     digitalWrite(11,HIGH);
     delayMicroseconds(100);  // time for the buzzer to be on
     digitalWrite(11,LOW);
     delayMicroseconds(100);  // time for the buzzer to be off
 /* This part of the sketch makes the buzzer pulse on and off when
 the object is within 66-133cm. */
 }
 if (distance >= distalRange and distance <= maximumRange) {  
 /* Slow On and Off sound to idicate distance between 133-200cm */  
     digitalWrite(11,HIGH);
     delayMicroseconds(200);  // time for the buzzer to be on
     digitalWrite(11,LOW);
     delayMicroseconds(300);  // time for the buzzer to be off
 }
 if (distance > maximumRange){
 /* No sound to idicate distance is further than 200cm */  
     digitalWrite(11,LOW);
 }
 //Delay 100ms before next reading.
 delay(100);
 }  
 

PaulS

Did you have a question?

I have some. Why didn't you post your code correctly? Why didn't you read the sticky at the top of the forum before posting?
The art of getting good answers lies in asking good questions.

wallman123

I'm sorry, can you please help me, I did look at it, I am just so confused, I've never programmed anything before. I was just wondering why my code wasn't working.

PaulS

Quote
I was just wondering why my code wasn't working.
\
But, it is working. It may not do what you want. But, you haven't told us what it does, or how that differs from what you want it to do.
The art of getting good answers lies in asking good questions.

wallman123

So, what I have is an ultrasonic senor hooked up to my arduino uno (pins 7 and eight) refer to code, and i have a buzzer in pin 11 and GND, my plan was to have the buzzer beep as the sensor was close to an object, and have it pulse when it was a little further away, and then not beep at all when it was far away from an object. The a. uno does show me the distances in cm on my computer, but the buzzer part of it seems to not be doing what I wanted it to. I was wondering if you could tell me how to fix my code.



Jedihermit

This is an old post but it does what I want so I made the pin 11 fixes for the buzzer.



/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino 5v
Echo to arduino pin 7
Trig to arduino pin 8
*/

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
#define BuzzerPin 11 //buzzer pin
int maximumRange = 200; // Maximum range needed
int distalRange = 133; // Distal range needed
int proximalRange = 66; // Proximal range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
  pinMode(BuzzerPin, OUTPUT);
}

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 spped of sound.
  distance = duration/58.2;
 
  if (distance >= minimumRange and distance <= proximalRange){
  /* Continuous sound if the object is within 0-66 cm of sensor. */
  Serial.println("-1");
  digitalWrite(BuzzerPin,HIGH);
  }
  if (distance >= proximalRange and distance <= distalRange) {
  /* On and Off sound to idicate distance between 66-133 cm */
  Serial.println(distance);
      digitalWrite(BuzzerPin,HIGH);
      delayMicroseconds(50);  // time for the buzzer to be on
      digitalWrite(BuzzerPin,LOW);
      delayMicroseconds(50);  // time for the buzzer to be off
  /* This part of the sketch makes the buzzer pulse on and off when
  the object is within 66-133cm. */
  }
  if (distance >= distalRange and distance <= maximumRange) { 
  /* Slow On and Off sound to idicate distance between 133-200cm */ 
      digitalWrite(BuzzerPin,HIGH);
      delayMicroseconds(100);  // time for the buzzer to be on
      digitalWrite(BuzzerPin,LOW);
      delayMicroseconds(175);  // time for the buzzer to be off
  }
  if (distance > maximumRange){
  /* No sound to idicate distance is further than 200cm */ 
      digitalWrite(BuzzerPin,LOW);
  }
  //Delay 100ms before next reading.
  delay(100);
  } 
 

Go Up