Model Train Detection

I am trying to use the blinking LED lights of the Spaceship Interface in conjunction with an IR sensor wich will turn the blinking lights on and off. So instead of a switch, the IR sensor would detect light and turn on the blinking lights. Here's my idea:

I am a model train enthusist and I want to have two IR sensors one at each end of the track which would trigger the blinking LED lights.

Does anyone know how to adapt the code of the Spaceship Interface example to make the IR sensors as an on/off switch?

Thanks.

-e

Post your code.
Which type of IR sensor? (for me is not clear this "IR sensor would detect light")
How do you want use IR sensor, like an infrared barrier?

So instead of a switch, the IR sensor would detect light and turn on the blinking lights

I agree it is not clear what you mean by the above.
Where does the IR light come from and what turns it on?

It makes more sense if the blinking lights come on when the IR sensor stops seeing an IR source e.g. when a train cuts a beam.

If all you want to do is detect the presence of a train it might be simpler just to use a Light Dependent Resistor between the sleepers. It doesn't need and special light source.

...R

sorry guys I'm new at this. How about writing code for an Ambient Light Sensors. So when the train passes over the sensor the railroad signals flash? I would put ambient light sensors on either end of the track. so when the train passes the ALS it would trigger the flashers?

That's what I suggested in the previous post to yours.

...R

EricWest73:
sorry guys I'm new at this. How about writing code for an Ambient Light Sensors. So when the train passes over the sensor the railroad signals flash? I would put ambient light sensors on either end of the track. so when the train passes the ALS it would trigger the flashers?

I am new to writing code. So I'll ask again, Can anyone suggest code that may work with an ambient light sensor to replace the switch which is used in the Arduino tutorial "spaceship Interface"? The Spaceship Interface code can be found in the menu settings in the arduino application.

EricWest73:
I am new to writing code. So I’ll ask again, Can anyone suggest code that may work with an ambient light sensor to replace the switch which is used in the Arduino tutorial “spaceship Interface”? The Spaceship Interface code can be found in the menu settings in the arduino application.

Someone already asked you to please post your code…the “spaceship” code. They are doing you a favor…for free. Don’t make them search for some code that you want them to then change for you to work for your application. Not everyone has their Arduino IDE in front of them while reading these posts.
Help them out a little…please just post the code that you would like help with.

And please…use Code Tags .

Spaceship Interface Code:

/*
Arduino Starter Kit example
Project 2 - Spaceship Interface

This sketch is written to accompany Project 2 in the
Arduino Starter Kit

Parts required:
1 green LED
2 red LEDs
pushbutton
10 kilohm resistor
3 220 ohm resistors

Created 13 September 2012
by Scott Fitzgerald

http://arduino.cc/starterKit

This example code is part of the public domain
*/

// Create a global variable to hold the
// state of the switch. This variable is persistent
// throughout the program. Whenever you refer to
// switchState, you’re talking about the number it holds
int switchstate = 0;

void setup(){
// declare the LED pins as outputs
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);

// declare the switch pin as an input
pinMode(2,INPUT);
}

void loop(){

// read the value of the switch
// digitalRead() checks to see if there is voltage
// on the pin or not
switchstate = digitalRead(2);

// if the button is not pressed
// blink the red LEDs
if (switchstate == LOW) {
digitalWrite(3, HIGH); // turn the green LED on pin 3 on
digitalWrite(4, LOW); // turn the red LED on pin 4 off
digitalWrite(5, LOW); // turn the red LED on pin 5 off
}
// this else is part of the above if() statement.
// if the switch is not LOW (the button is pressed)
// the code below will run
else {
digitalWrite(3, LOW); // turn the green LED on pin 3 off
digitalWrite(4, LOW); // turn the red LED on pin 4 off
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
// wait for a quarter second before changing the light
delay(250);
digitalWrite(4, HIGH); // turn the red LED on pin 4 on
digitalWrite(5, LOW); // turn the red LED on pin 5 off
// wait for a quarter second before changing the light
delay(250);
}
}


You seem to have ignored the request to put your code within code tags !

I think all you need to do is replace the piece that reads the switch

switchstate = digitalRead(2);

with something that reads the value of the LDR such as

byte analogPin = 0;
int ldrThreshold = 650; // substitute a value to suit your application
int ldrVal;
ldrVal = analogRead(0);
switchstate = 0;
if (ldrVal < ldrThreshold) {
    switchState = 1;
}

analogRead() will return a value between 0 and 1023 depending on how much light is falling on the LDR.
You will need to figure out by experiment what value to use for the ldrThreshold.

I’m assuming you want your switch to be ON when the light is dark (i.e. train covering the LDR).

…R

PS you might get more help if you amend the title of your topic to include “model train detection”. If you edit your first post you can change the title.