3 HCSR04 Ultrasonic Sensors , buzzer output when object reaches a certain range.

Hi All,

Am new to the Arduino scene having recently obtained an Arduino UNO and have been playing around discovering the basics really am electrical Engineer and Mechanical Engineer by trade so understand the fundamentals am now trying to learn more about how programming works and learn more as it's I find it interesting learning new things.

Have managed a few little projects so far to get the hang of the basic programmes.

However on my latest little project am hitting a bit of a brick wall.....

In a brief have set up a project to alert roughly when cat is nearby and using 3 seperate sensors can identify something moving around so not having to rely on leaving window open when sitting indoors or for her to meow to come in, in this freezing cold weather, generally she likes being indoors in the cold but likes to have a little wonder still as theres usually someone in this results in the house ending up cold.

1.) First of all I experimented using the if distance = commands from 'SerialPrint' output then Digitalwrite LED = High etc which worked absolutely fine. using the basic commands.

2.) Secondly I've had a look at a similar project which uses 3 distance sensors of the same type which output data to the serial monitor. Tried this code to test my wiring and sensors and was successful at outputting onto the serial monitor included with the Arduino IDE.

3.) Have tried to apply similar methodology as point no1. to output onto 'LED's' when a specific sensor detects a certain value i.e if left sensor is more than , right sensor etc. However have hit a brickwall and the IDE gives an error when trying to verify code.

have been trying to see if a similar project has been made or is this something which is limited due to hardware limitations of the Arduino?

This is what my code looks like so far. Have tried to write program by scratch.

here is my code so far;

#define trigPin1 9  //represents digital pins used on arduino
#define echoPin1 10
#define trigPin2 12
#define echoPin2 8
#define trigPin3 7
#define echoPin3 6
#define led 11
#define led2 13
#define led3 5

;long duration, distance, RightSensor,BackSensor,FrontSensor,LeftSensor;

void setup()
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

void loop()  {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;

Serial.print(" - ");
Serial.print(" - ");

void SonarSensor(int trigPin,int echoPin)
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;

if SonarSensor 'LeftSensor' distance = <90) 
 digitalWrite(led, HIGH);


if (SonarSensor 'RightSensor' = distance <90) 
 digitalWrite(led2, HIGH);


if (SonarSensor 'FrontSensor' = distance <90) 
 digitalWrite(led3, HIGH);


Your code so far is incomplete.
And lacking code tags.

It's hard to understand your sketch when you label your pins 1, 2, and 3 and then use the names Left, Front, and Right to reference them.

See how much cleaner it can look?

const byte RightTriggerPin = 9;
const byte RightEchoPin = 10;
const byte RightLEDPin = 11;

const byte LeftTriggerPin = 12;
const byte LeftEchoPin = 8;
const byte LeftLEDPin = 13;

const byte FrontTriggerPin = 7;
const byte FrontEchoPin = 6;
const byte FrontLEDPin = 5;

void setup()
  Serial.begin (9600);

  pinMode(RightTriggerPin, OUTPUT);
  pinMode(RightEchoPin, INPUT);
  pinMode(RightLEDPin, OUTPUT);

  pinMode(LeftTriggerPin, OUTPUT);
  pinMode(LeftEchoPin, INPUT);
  pinMode(LeftLEDPin, OUTPUT);

  pinMode(FrontTriggerPin, OUTPUT);
  pinMode(FrontEchoPin, INPUT);
  pinMode(FrontLEDPin, OUTPUT);

void loop()  {
  int RightSensor = SonarSensor(RightTriggerPin, RightEchoPin);
  int LeftSensor = SonarSensor(LeftTriggerPin, LeftEchoPin);
  int FrontSensor = SonarSensor(FrontTriggerPin, FrontEchoPin);

  Serial.print(" - ");
  Serial.print(" - ");

  digitalWrite(RightLEDPin,  (RightSensor != 0 && RightSensor <= 90));

  digitalWrite(LeftLEDPin,  (LeftSensor != 0 && LeftSensor <= 90));
  digitalWrite(FrontLEDPin,  (FrontSensor != 0 && FrontSensor <= 90));


int SonarSensor(int trigPin, int echoPin)
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  unsigned long duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
if (SonarSensor 'FrontSensor' = distance <90)

Please explain what the Hell this is supposed to mean.

(The code is still incomplete)

Thanks for the help here so far :slight_smile:

Have now added the code tags (oops) . Has been a bit of a challenge (sure it will be a "that was easy moment once figured").

Have done a substantial bit of reverse engineering this evening... mainly looking through more bits to educate myself further. Will clean up my code definitely.. Yeah about that was trying to reference what I thought was a common variable and then cross reference it to a particular defined sensor... have figured my logic was not sound. Will get there I think , fingers crossed gonna keep trying.

JohnWasser, the code which you have provided seems to be doing more what I'm needing the device to do now :slight_smile: , time for me to read through and understand it more. Thanks so far really appreciate it.

One thing to remember that will help you understand some of the shortcuts:

0 == false == LOW
1 == true == HIGH

You can use true/false (boolean) values in place of the HIGH/LOW values passed to digitalWrite().

  if (x) 
    digitalWrite(Pin, HIGH);
    digitalWrite(Pin, LOW);

can be replaced with:

  digitalWrite(Pin, x);

Not been on here for a couple of days, over the next few evenings should have some time to tinker a bit more, thanks for the shortcuts btw :). What starts of as a basic tinker is beginning to turn into a slight addiction guess it's the challenge surrounding it. Will have to post some photos up when it's finally completed :slight_smile: