Can somebody explain this code to me?

Hi. I am very new to Arduino (about 1 month). My teacher wants us to make a code so all three lights start lighting up when the ultrasonic system detects something in front of it. How can I do that? I have tried for a bit now, but it seems like it doesn't work. Here is a picture of the arduino itself. 2 - Album on Imgur

I used this code and it still doesn't work :
const int trigPin = 2;
const int echoPin = 3;
const int LEDlampRed = 4;
const int LEDlampYellow = 5;
const int LEDlampGreen = 6;
const int buzzer = 7;
int sound = 500;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long durationindigit, distanceincm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit * 0.034) / 2;

if (distanceincm > 50) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else if (distanceincm <= 50 && distanceincm > 20) {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else if (distanceincm <= 20 && distanceincm > 5) {
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampRed, LOW);
tone(buzzer, 500);
}
else if (distanceincm <= 0) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, HIGH);
tone(buzzer, 1000);
digitalWrite(LEDlampRed, HIGH);
delay(300);
digitalWrite(LEDlampRed, LOW);
}

Serial.print(distanceincm);
Serial.println(" cm");

delay(300);
}

What does "it doesn't work" mean?
What do the debug prints tell you?
Please remember to use code tags when posting code

What happens when you run the sketch ?
Is distanceincm correct when you print it ?

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

First off, welcome to the forum.

Secondly, you can use code tags to post your code. You'll see a small button on top right whenever you're writing a post that looks like this: </>. There's also a guide on how to use the forum

Thirdly, this:

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

is the other way around. Trigger is the input, Echo is the output.

EDIT TRIGGER IS OUTPUT IM DUMB

linearity64:
Thirdly, this:

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);



is the other way around. Trigger is the input, Echo is the output.

Don't be silly
(Welcome to the forum)

start with verifying if the pulse duration is being measured correctly

Hi guys, I need to explain this code for school. I would really appreciate if someone could explain this code to me:D Im not sure what to say, since I know what device its for and know how it works. I just don't know what to say about it.

const int trigPin = 2;
const int echoPin = 3;
const int LEDlampRed = 4;
const int LEDlampYellow = 5;
const int LEDlampGreen = 6;
const int buzzer = 7;
int sound = 500;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long durationindigit, distanceincm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit * 0.034) / 2; <--- mixing float and long?

if (distanceincm > 50) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else if (distanceincm <= 50 && distanceincm > 20) {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else if (distanceincm <= 20 && distanceincm > 5) {
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampRed, LOW);
tone(buzzer, 500);
}
else if (distanceincm <= 0) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, HIGH);
tone(buzzer, 1000);
digitalWrite(LEDlampRed, HIGH);
delay(300);
digitalWrite(LEDlampRed, LOW);
}

Serial.print(distanceincm);Serial.println(" cm");

delay(300);}

Well, it looks like code for reading an ultra sonic sensor. It determines distance based on the time sound travels through the air.

Please edit your post to add code tags, as described in "How to use the forum".

I just don't know what to say about it.

It sounds like your assignment is to explain what each part of the program does, and why it was written that particular way.

Read this:
https://forum.arduino.cc/index.php?topic=97455.0

convince us you are not asking us to do your homework.

The Arduino reference is a valuable resource.
All of the functions and operators used in that code are explained in the reference.

The range finder looks to be a HCSR-04. There must be a million tutorials for that sensor on the web.

If you have specific questions, we will be more willing to help.

Also, you could reverse engineer a program specification out of that code, stating things like under what circumstances the different leds should switch and when the buzzer should sound, etc.

You could also do a “walk through” of its operation from sending the pulse, receiving it, calculating the distance and signaling it to the user.

You have posted this same exact code 12 hrs before this post: here

And you still haven't fixed this line:

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

On the topic of this question, documentation helps.

Duplicate topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

Thanks in advance for your cooperation.

linearity64:
And you still haven't fixed this line:

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

Can you explain what needs to be fixed?

TheMemberFormerlyKnownAsAWOL:
Can you explain what needs to be fixed?

Trigger is an input, Echo is an output

...im feeling a deja vu

EDIT: IM DUMB TRIGGER IS INDEED OUTPUT

linearity64:
Trigger is an input, Echo is an output

...im feeling a deja vu

Not usually. Of course we don't know how he would have wired it, but generally the trigger causes the pulse to be emitted, and the echo receives it.

https://www.arduinoforbeginners.com/hc-sr04/

Oh shit im dumb
Yeah, ok, trigger is indeed output.
Excuse me, I haven't been using HC-SR04 without NewPing for a while.
Damn I feel dumb, holy

That OK we all make mistakes and a good number of us are prepared to admit them.