Train Crossing
The train is coming and we need to protect our cars from getting in the way! Let’s detect if the train is coming and block the road with a train crossing signal.
Goal:
– Detect train, lower crossing arm and blink lights.
Challenges:
– Construct the traffic crossing signal.
- Servo to control crossing arm.
– Alternating blinking LEDs.
– Infrared proximity sensor for train detection.
- Delay the crossing for long trains.
Components:
- Raspberry Pi (or any NodeJS host)
- Arduino (I used the Mega 2560 but any will do)
- 2x 1.8v LEDs
- servo
- breadboard
- 2x 220 Ohm resistors
– IR proximity sensor
Circuit:
There will be two LEDs connected to individual pins since they will blink independently.
The servo will need to be connected to a PWM pin so that we can “pulse” the correct position to it. I will be using a 5v hobby servo with 180 degrees of rotation.
The infrared proximity sensor will be connected to a digital input pin.
Schematic:
Code:
We will need to initialize the board as usual and then define the two LED pins for our lights and the servo for the crossing arm. Then we need to define the two states that the crossing will be in; on or off. Each state will represent the current lights and position of the servo.
Then we will define a sensor listener similar to a button, to trigger the action. The only difference is that the sensor has an adjustable sample rate.
We will then need to adjust for duplicate events before the train has finished crossing. There are gaps between the train coaches which send “change” signals every time one passes. If you don’t take this into consideration, the crossing will act crazy.
To understand what position we set the servo at will take a lot of trial and error. We will use the REPL to dial in the servo angle to get the crossing arm where we want it.
Basic Flow
- Set initial state “off”
- Listen for button sensor activation
- Toggle servo and LEDs to proper state
Filename
traincrossing.js
Now save and close your file.
npm init
npm install johnny-five --save
node traincrossing.js
root@thethingbox:~/IoL# node traincrossing.js 1442769369170 Device(s) /dev/ttyACM0 1442769369254 Connected /dev/ttyACM0 1442769372201 Repl Initialized >> crossingSensor raw: 1 crossingSensor raw: 0 crossingActivate() crossingSensor raw: 1 crossingSensor raw: 0 crossingDelayed() crossingSensor raw: 1 crossingDeactivate()
Notes:
REPL
To dial in the servo angle, type the following in the REPL command prompt where the number is the desired angle.
crossingServo.to(111);
Leave a Reply
You must be logged in to post a comment.