2 ultrasonic sensors controlling 2 lights

Hi, I need help with a school project, im new to coding so im kinda lost.

The project contains two SR04, two LEDs (and also a servo but that ill fix myself).

The project is making a train crossing where i have a traffic light,(only red and green). the lights will depend on the sensors. So one of the sensors will be placed infront of the crossing. If it detects a movement (train) the red light will light up (green off).

The second sensor will be placed after the crossing, and if it detects a movment(train) the greenlight will light up. (red off).

If you want to you can also help me with the servo/ gate. There wil also be a gate at the crossing which will close when there is a red light and be open when its a green light.

Here is the code so far. look away from the servo :slight_smile:
#include <Servo.h>
int Redlight = 3;
int Greenlight = 2;
int Sensorverdi1 = 0;
int Sensorverdi2 = 0;
int servoPin = 9;
int TriggerPIN1 = 5;
int EchoPIN1 = 10;
int TriggerPIN2 = 6;
int EchoPIN2 = 11;
Servo Servo1;

void setup() {
pinMode (Redlight, OUTPUT);
pinMode (Greenlight, OUTPUT);
Servo1.attach(servoPin);
pinMode(TriggerPIN1, OUTPUT);
pinMode(EchoPIN1, INPUT);
pinMode(TriggerPIN2, OUTPUT);
pinMode(EchoPIN2, INPUT);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TriggerPIN1, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPIN1, HIGH);
delayMicroseconds(2);
digitalWrite(TriggerPIN1, LOW);

long timedelay = pulseIn(EchoPIN1, HIGH);
int distance1 = 0.0343 * (timedelay / 2);

Serial.print("Sensor 1 : ");
Serial.println(distance1);

delayMicroseconds(2);

digitalWrite(TriggerPIN2, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPIN2, HIGH);
delayMicroseconds(2);
digitalWrite(TriggerPIN2, LOW);

long td = pulseIn(EchoPIN2, HIGH);
int distance2 = 0.0343 * (td / 2);

Serial.print("Sensor 2 : ");
Serial.println(distance2);
Servo1.write(0);
delay(500);
int Sensorverdi1 = (distance1);
int Sensorverdi2 = (distance2);
digitalWrite(Redlight, 1);

if (Sensorverdi1 <= 10)
{ Servo1.write(0);
delay(2000);
digitalWrite (Redlight, LOW);
digitalWrite(Greenlight, HIGH);
}
else if (Sensorverdi2 <= 10)
{
digitalWrite (Greenlight, LOW);
digitalWrite (Redlight, HIGH);
delay (1000);
Servo1.write (90);
}
}

I`d appreciate any kind of help,
Thanks in Andvence:))

-StundentW

What's the problem?

Oh, I see.

When delay is used the CPU does nothing. The code runs very fast but for 2.5 seconds the CPU using your code does nothing.

Did you happen to take a look at File|Examples|02.Digital|BlinkWithoutDelay?

Use millis() for timing and the CPU will not be doing nothing for 2.5 seconds.

1 Like

My problem right now is that the redlight is always on even tho none of the sensors have detected something, and the green light turns on as planned, but it does not stay on. I need it to stay in until the other sensor turn it off.

Local variables with the same name is often the source of bugs and confusion.

Please remember to use code tags when posting code.

That is to say, read the instructions, so you can go back and edit your first post - using the "pencil" icon below the post.

Thx for the help guys. I found out the problem :))))

Bye!
:roll_eyes:

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