schematic, then run Sketch 1 (available at the article link).
It will work for one or all four sensors in the final
arrangement. It will allow you to vary and simulate the
ambient light level of your layout room. Then, you can
observe — on the serial monitor — the change in digital
values as you shade the sensors fully. Armed with these
values, you can set the threshold number about halfway in
between. If your sensors “clear” from partial light between
the cars, move your threshold number lower.
What We
Need from
the Arduino
I used an Arduino Uno. It has
six ADC pins, and we will use
four of them. We need to
alternate — on and off — two
flasher LEDs at a rate of about 1
Hz. So, we need two digital pins
for that job. The LCD display uses
six output pins unless you use
one with an I2C backpack. Then,
you’re down to using only the SCI
and SDA pins plus five volts and
GND.
The current demand for the
whole job is under the limit, and
we could probably even tie in
another job. You could use a
hardware interrupt to sense the
change in light levels, but the
Millis timer won’t function during
an interrupt service routine, so
let’s keep it simple. You could also do the flasher
alternations with a hardware setup such as an LM555 chip
but — there again — software is sure a lot simpler.
Two Applications in One
... How It Works
Take a look at Figure 4. Here’s the sequence the
processor will go through as it executes its continuous
“loop.” It monitors the westbound
sensor and if it changes. a train has
entered the area. If not, it checks the
eastbound sensor and then returns to
the start of the “polling” loop if
nothing is found. If sensor W1 has
been covered, a train is detected, so it
turns on the two LED flashers with a
subroutine to time them to alternate at
about one cycle per second. It waits
for the sensor across the crossing
(W2) to be covered, and does nothing
until the train has completely come
across it. The while do loop is great
for such stalling.
When the back of the train clears
the sensor (W2), it turns off the
flashers and starts timing for the
speedometer run. The intrinsic Millis
function gives us the number of
milliseconds as a number since the last
■ FIGURE 3.
Schematic for the
Arduino hookup.
■ PHOTO 1. The development mockup used to calibrate sensors and the
LCD screen display. The screen display shows the Block Clear status.
30 August 2015