I have a little project running and i cant get it work correctly. I recently started programming with arduino and im still a noob at this so i need a bit of help of you guys
Train crossing:
The idea is when a train arrives at a crossing the barrier closes and when the train is over the crossing the barrier opens up again. The train is detected with IR sensors and the barrier is controlled with 2 relais to control the barrier going up and down. So when IR sensor 1 dives below a specific value relais 1 switches on and when the value rises back over the specific value relais 1 is switches off. Then the exact same thing needs to be happening with sensor 2 and relais 2.
I programmed the code for each sensor individually and they work but when i try to implement both sensors in one program it stops working
const int sensorpin1 = A0;Â
const int sensorpin2 = A1;
const int relais1 = 8;
const int relais2 = 7;
const int led1 = 12;
const int led2 = 13;
int sensor1 = 0;
int sensor2 = 0;
void setup()
{
 Serial.begin(9600);
Â
 Serial.begin(9600);
 pinMode(led1,OUTPUT);
 pinMode(led2,OUTPUT);
 pinMode(sensorpin1,INPUT);
 pinMode(sensorpin2,INPUT);
 pinMode(relais1,OUTPUT);
 pinMode(relais2,OUTPUT);
}
void loop()
{
 sensor1 = analogRead(sensorpin1);  Â
 Serial.print(sensor1);       Â
 delay(100);
 if (sensor1 < 50)
 {
  digitalWrite(led1,LOW);
  digitalWrite(relais1,LOW);
 }
 else
 {
  digitalWrite(relais1,HIGH); Â
  digitalWrite(led1,HIGH);
  delay(4000);
 }
Â
 sensor2 = analogRead(sensorpin2);   Â
Â
 Serial.print(sensor2);       Â
Â
 delay(100);
Â
 if (sensor2 > 50)
 {
  digitalWrite(led2,LOW);
  digitalWrite(relais2,LOW);
 }
 else
 {
  digitalWrite(relais2,HIGH); Â
  digitalWrite(led2,HIGH);
  delay(4000);
 }
}
Yea they are 2 different kinds of IR sensors, one is for seeing if the signal is interrupted (sensor 1) by the incoming train and IR sensor 2 is laid down flat on the traintracks to detect if the trains passes over it. I have to use as many different components as possible to use in my project because its for school otherwise i would have used same ones.
So my idea on how the program should work is:
-train arrives
-IR sensor 1 detects it ( IR beam is interrupted( falls below a certain value Y))
-relay 1 should be activated for X seconds
-IR sensor 1 back to normal
-train goes over IR sensor 2
-sensor 2 detects the train( falls below a certain value Y)
-relay 2 has to be activated for X seconds
-sensor 2 back to normal
I still have to measure the values and seconds but i dont think the numbers have an inpact on the code.
If im missing something of information, please tell me.