Arduino train crossing with IR sensors

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 :slight_smile:

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

Can anyone give advice on this?

this is how i connected all the parts to the arduino board: http://puu.sh/ec6ZB/07e176a3d7.jpg

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);
  }
}

The first thing I see wrong is that you are driving the relay directly from the output pin. It should be driven by a transistor.

I then see that you appear to be using a 38khz IR receiver for detection. The usual way is to use a IR transistor to receive the IR.

You appear to be using two,different types of sensing.

Please give more information on how and why you are doing things.

Weedpharma

Ok, i will replace it with transistors.

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.

thanks

If you have a sensor on each side of the level crossing I don't see any need for a delay()

The logic I would use is something like this

read the 2 sensors
if both sensors are "off", open the gates to road traffic
if either sensor is "on", close the gates to road traffic

If there is some other reason why you need a time period see how it is done without using delay() in several things at a time.

...R

Needed to tweak some minor issues but at last i got my code working.

Thanks guys!

Thread closed

Link supplied by Groundfungus in another thread should show you how to do the beam breaker.

http://www.me.umn.edu/courses/me2011/arduino/technotes/irbeam/irbeam.html

Weedpharma