Railwaycrossing

Hi guys, I need to make an automatic railway crossing for school. I wrote a program but for some reason it isn’t working.There are 2 light sensors 1 in front of the crossing and 1 behind the crossing so when the train rides over the first one it opens and when it rides over the 2nd one it closes.

here is the code:

#include <Servo.h>

Servo myServo;

int LDR = 0; //analog pin to which LDR is connected, here we set it to 0 so it means A0

int LDR2 = 1;
int LDRValue = 0; //that¹s a variable to store LDR values

int LDRValue2 = 0;

int light_sensitivity = 90; //This is the approx value of lightsurrounding your LDR

void setup() {

Serial.begin(9600); //start the serial monitor with 9600 buad

pinMode(13, OUTPUT); //we mostly use 13 because there is already abuilt in yellow LED in arduino which shows output when 13 pin is enabled

myServo.attach(9);

}

void loop() {

LDRValue = analogRead(LDR); //reads the ldr¹s value through LDR

LDRValue2 = analogRead(LDR); //reads the second ldr¹s value throughLDR

Serial.println(LDRValue);//prints the LDR values to serialmonitor

Serial.println(LDRValue2);

delay(50); //This is the speed by which LDR sends value to arduino

if (LDRValue < light_sensitivity || LDRValue2 < light_sensitivity)
{

myServo.write(180);

digitalWrite(13, HIGH);
}
else
{

myServo.write(90);

digitalWrite(13, LOW);
}

}

You never read LDR2.

 LDRValue = analogRead(LDR);      //reads the ldr¹s value through LDR
  
 LDRValue2 = analogRead(LDR);      //reads the second ldr¹s value throughLDR

This code is not as you intend, I think. The second value should be LDR2?

Have a look here
When posting code, we ask you to please use code tags to ensure there’s no ambiguity.