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
#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