A problem with the code

Hey,
I’m a little bit stressed because I’ve a problem with my project. The circuit that I’ve done includes a LDR with 3 yellow LEDs, and another part with HC-SR04 connected with a red LED an a buzzer (both circuits with resistors). The problem is that the part with HC-SR04 doesn’t work but the other part with LDR, yes. I’m new in this world so I don’t now how to fix the problem. This is the code:

//set pin numbers
//const won’t change
const int ledPin = 13; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {

int ldrStatus = analogRead(ldrPin); //read the status of the LDR value

//check if the LDR status is <= 300
//if it is, the LED is HIGH

if (ldrStatus <=50) {

digitalWrite(ledPin, HIGH); //turn LED on
Serial.println(“LDR is DARK, LED is ON”);

}
else {

digitalWrite(ledPin, LOW); //turn LED off
Serial.println("---------------");
}
{
// 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;

safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
}

If someone could help me I would be very happy, because it’s a school project and it’s important for me. Thanks!!

I've forgotten to mention the model of my arduino, it's an Arduino UNO SMD edition, and I'm using 12V.

But we don't know how you have things connected, need a wiring diagram, also please modify your post and put your code in code tags, Here's how

Does the sonar/buzzer part work in a sketch of its own without the ldr/led part?

If someone could help me I would be very happy, because it’s a school project and it’s important for me.

The code DOES work. That it might not do what you expect means that your expectations are wrong (or the hardware is not connected correctly).

If you want help, you have to tell us what the code ACTUALLY does, and how that differs from what you expect it to do.

“It doesn’t work” is wrong, and useless.

Sorry I didn't know how to express well. You are right, it was a wrong connection. Actually the code functions are:

1.When I approach my hand (10cm) to HC-SR04 the buzzer sounds, and the 3 yellow LEDs that I mentioned before, lights up.

2.The other action that the code does is, when it's dark the 3 yellow LEDs light up because of the LDR.

But now I've another problem, the LEDs that light up in the first function aren't the ones that I want to light up. As I said before, I would like to light up the red LED conjountly the buzzer when the HC-SR04 detect something. I don't know it well, but I think that the part of LDR is good, but I don't know surely.

Thank you again.

I'm a bit confused now. I just noticed that you only pinMode()-ed one pin as an output, ledPin, yet you talk of 3 leds and another led.

So I think the first thing you should do is declare some pin names say yellowLed-1 etc and redLed and pinMode them as outputs, and digitalWrite() them high and low in the codeas appropriate. Then post that code if you can't fix it.

Hi!
I’ve renamed what you said before and this is the result:

//set pin numbers
//const won’t change
const int ledPinLDR = 13; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPinHCSR04;
// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {

Serial.begin(9600);
pinMode(ledPinLDR, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPinHCSR04, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {

int ldrStatus = analogRead(ldrPin); //read the status of the LDR value

//check if the LDR status is <= 300
//if it is, the LED is HIGH

if (ldrStatus <=50) {

digitalWrite(ledPinLDR, HIGH); //turn LED on
Serial.println(“LDR is DARK, LED is ON”);

}
else {

digitalWrite(ledPinLDR, LOW); //turn LED off
Serial.println("---------------");
}
{
// 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;

safetyDistance = distance;
if (safetyDistance <= 10){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPinHCSR04, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPinHCSR04, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
}

Now, when the sensor detects something in a rank of 10cm the buzzer sounds, and the 3 yellow LEDs don’t light up. But the red LED still not light up.
Thanks for your attention.

Welcome to the Forum. Please read the post: How to use this forum - please read.

For the ones that helped me I want to make now you that I've solved the problem. The mistake was in the IDE code, I didn't define the number of the output so it was normally that the Arduino couldn't read in which output was the red LED connected. We learn from errors! Thank you for all your attention.

gere: We learn from errors!

Not everyone does. As one can see here where you were asked to read how to post on this site and to wrap your code in code tags. Guess you couldn't be bothered.