(code problem) Looking for advice on how to make this code work

Hi, I'm starting to learn how to code and overall create circuits. I'm trying to make something but it doesn't work as I'm imagining it. It's an Ultrasonic Distance Sensor connected to an Arduino UNO, and I'm trying to add some lights so that the closer it gets to the sensor the more lights light up. There's also a buzzer that should turn on at a certain threshold.

What happens when I run the code is that every light as well as the buzzer turn on, regardless of the distance where the object is at.

I can't figure out why it won't work. Here's the code I have:

/*

*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int LEDred = 4;
const int LEDblue = 3;
const int LEDyellow = 2;
const int Buzzer = 7;

// defines variables
long duration;
int distance;

void setup() 
{
 
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(LEDred, OUTPUT);
  pinMode(LEDblue, OUTPUT);
  pinMode(LEDyellow, OUTPUT);
  pinMode(Buzzer, OUTPUT);
 
  Serial.begin(9600); // Starts the serial communication
      
}


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);

  // Calculating the distance
  distance = duration * 0.034 / 2;
  
  
  if(distance > 400);
  {
    digitalWrite (LEDred, LOW);
    digitalWrite (LEDblue, LOW);
    digitalWrite (LEDyellow, LOW);
    digitalWrite (Buzzer, LOW);
  }
  
  if((distance > 200) && (distance < 400));
    {  
      digitalWrite (LEDred, HIGH); //red
      digitalWrite (LEDblue, LOW); //blue
      digitalWrite (LEDyellow, LOW); //yellow
      digitalWrite (Buzzer, LOW); //Buzzer  
      delay(1000);
    }
  
  if((distance > 50) && (distance < 200));
    {
      digitalWrite (LEDred, HIGH); //red
      digitalWrite (LEDblue, HIGH); //blue
      digitalWrite (LEDyellow, LOW); //yellow
      digitalWrite (Buzzer, LOW); //Buzzer
      delay(1000);
    }
    
  if((distance > 0) && (distance < 50));
    {
      digitalWrite (LEDred, HIGH); //red
      digitalWrite (LEDblue, HIGH); //blue
      digitalWrite (LEDyellow, HIGH); //yellow
      digitalWrite (Buzzer, HIGH); //Buzzer
      delay(1000);
 	}
   
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  Serial.println(" cm ");
  delay(100);
}

And also an image of how the circuit looks if it helps, since maybe I connected something wrong there.

Welcome to the forum

Every if clause in a sketch has a block of code associated with it that is executed if the test returns true. Looking at one of your tests I see

  if(distance > 400);
  {
    digitalWrite (LEDred, LOW);
    digitalWrite (LEDblue, LOW);
    digitalWrite (LEDyellow, LOW);
    digitalWrite (Buzzer, LOW);
  }

The block of dependent code in this snippet is the semicolon after the test. The rest of the code is executed unconditionally

Remove the semicolon and the others like it and the dependent code block in curly brackets will only be executed when the test returns true

1 Like

I have had success with these using the NewPing library. In Arduino IDE, click sketch > include Libraries > manage libraries and type newping into the search box. Install the newest version of NewPing by Tim Eckel and then after it's installed, in the Arduino IDE go to examples > NewPing > NewPingEventTimer. It's very well commented and you should easily be able to adapt it or one of the other examples to your needs.

1 Like

That did the trick, thank you! I'll have to keep that in mind.

The library NewPing has nothing to do with OP's issue.

I'll check that out, thank you!

1 Like

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