Help fix code for Ultrasonic sensor

Guys I am a HUGE noob
This code I typed had no syntax errors from my compiler but the code doesn't work

This is what it is supposed to do:
If anyone of the three ultrasonic sensors go off, so should the piezo

If its the right side ultrasonic sensor the right piezo should go off
If its the left Ultrasonic sensor the Left piezo should go off
If its the middle Ultrasonic sensor both the Piezos should go off.

I know there are many mistakes but as I said I am a Huge noob.

Now you might be thinking,
How can a noob type this code

Its off of youtube. Well, most of it.

Please send the refined code if you may.
Thank you in advance.

Code:

#define RechoPin 9 
#define RtrigPin 8
#define MechoPin 3
#define MtrigPin 4
#define LechoPin 10
#define LtrigPin 11


long duration; 
int distance; 
int RPiezo (6);
int LPiezo (5);

void setup() {
  pinMode(RtrigPin, OUTPUT); 
  pinMode(RechoPin, INPUT); 
  pinMode(LtrigPin, OUTPUT); 
  pinMode(LechoPin, INPUT); 
  pinMode(MtrigPin, OUTPUT); 
  pinMode(MechoPin, INPUT); 
  Serial.begin(9600); 
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); 
  Serial.println("with Arduino UNO R3");
}
void loop() {
 
  digitalWrite(RtrigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(RtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(RtrigPin, LOW);
  
  digitalWrite(MtrigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(MtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(MtrigPin, LOW);
  
  digitalWrite(LtrigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(LtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(LtrigPin, LOW);
  
  duration = pulseIn(RechoPin, HIGH);
  
  duration = pulseIn(MechoPin, HIGH);
  
  duration = pulseIn(LechoPin, HIGH);
 
  distance = duration * 0.034 / 2; 
  
  distance = duration * 0.034 / 2; 
  
  distance = duration * 0.034 / 2; 
 
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if
    (distance < 50) {
    digitalWrite(RPiezo, HIGH);
    
    delayMicroseconds(2000000);
    digitalWrite(RPiezo, LOW);
  }
  
  if
    (distance < 50) {
    digitalWrite(LPiezo, HIGH);
    
  delayMicroseconds(2000000);
    digitalWrite(LPiezo, LOW);
}
  
  if
    (distance < 50) {
    digitalWrite(RPiezo, HIGH);
    digitalWrite(LPiezo, HIGH);
    
    delayMicroseconds(2000000);
    digitalWrite(RPiezo, LOW);
    digitalWrite(LPiezo, LOW);
}
}
  duration = pulseIn(RechoPin, HIGH);
  
  duration = pulseIn(MechoPin, HIGH);
  
  duration = pulseIn(LechoPin, HIGH);

How many different values do you expect the duration variable to hold ?

Start by using different variables for right, middle and left durations and distances and save the values after the ping for each sensor not after all 3 of them have happened

There are better ways of doing this but start simple

The 'echo' happens right after the 'trigger' so you have to put the 'pulseIn(echo, HIGH);' right after the trigger pulse. If you send trigger pulses to all three and then start looking for echoes you're likely to miss most or all of the echoes. Also, you don't want an echo from the first trigger to get picked up by the second sensor. You should trigger the sensors no closer than 30 milliseconds apart to give the echoes from the previous sensor time to fade out.

To be honest @johnwasser and @UKHeliBob ,
I don't understand a thing you are saying, that is why I asked if you could send the refined code, I know I'm asking for a lot but please do.

If you don't want help learning to program but want someone to do the programming for you, there is a separate section of the forum for that:

You will learn more if you do it yourself

The program structure will be something like this

  read right sensor
  calculate distance
  save the distance in a variable (distanceR)
  wait 30 milliseconds for reflections to settle down

  read mid sensor
  calculate distance
  save the distance in a different variable (distanceM)
  wait 30 milliseconds for reflections to settle down

  read left sensor
  calculate distance
  save the distance in another different variable (distanceL)
  wait 30 milliseconds for reflections to settle down

  if distanceL < 50 or distabceM < 50 or distanceR < 50
    make a sound
  end if

NOTE - this is not real code but you can write it using the code in your sketch arranged in a different order

1 Like

Thank you @UKHeliBob and @johnwasser !

If you couldn't already tell, I'm just a kid.

And @johnwasser , I have a project due this Friday, so I really couldn't learn the entire thing by then. Sorry :sweat_smile:

Your young age is no barrier to writing the code. In fact it might be regarded as an advantage compared to the age of some of the members here

You say that you have a project due this Friday so is it fair if someone else writes the code ? The sketch that you posted is quite close to working but you have not thought through the order in which things should be done

Take a look at my pseudo code, identify which parts of your code match the sections of mine and rearrange your code to match mine. Maybe start with just one sensor for a quick win then add a second and third

You will get plenty of help here if you show some willingness to try to do things yourself but little sympathy for just asking someone to write code you your project. As a matter of interest how long have you had to complete the project ?

To be Honest, I had 2 weeks.
But since this activity wasn't from my school, my luck,

I had exams for 1 week.

So, that's that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.