Skip to main content

Reply to "Using an optocoupler to connect insulated rail to input pin of shift register"

Ok, first, for software debouncing, or any other timing task, the delay function is never the right choice.  It can be the easier option, and in really simple programs that are only doing one thing it can be a time saver in programing, but it shouldn't be in any program that is doing anything more complex than flashing an led on and off.  I wish they never put it in the blink sketch in the first place and just taught people to use timers from the start.  Timers take a couple more lines of code, something like this for a debounce, as I posted earlier in the thread:

CurrentTime = millis();
MB1_State = digitalRead(ManualButtonOnePin);
if (MB1_State != MB1_StateLast) ManualButtonOneClock = CurrentTime; 
if (CurrentTime >= ManualButtonOneClock + DebounceTime) ManualButtonOneCurrentState = MB1_State; 
MB1_StateLast = MB1_State;

There are plenty of tutorials online on how to use simple timers like this, or how to use interrupt timers for things where timing is critical.  In most cases when dealing with train-related projects, half a millisecond one way or the other is not that big of a deal.  


Someone asked for how to hook things up, so here's how I do it:  

insulated railIns-rail-trigger SCh

Sorry for the poor drawing.  The schmatic shows an RC filter used as this circuit was stolen from another project of mine (C4).  It's not needed for the anti-derail, switch machine controller, but is there to give a steady 'ON' from AC input sources.  

I choose to use a diode and one-direction Opto-coupler because it's dirt cheap (4 cents per channel), but you could also use a bi-directional Opto if you wanted and remove the diode (D5)

A pull-up resistor is needed on the logic-level output, as the opto can only pull the line LOW.  This is built in on Arduino boards, but has to be turned on.  The MCP23017 port expander also has internal pull-ups, but I've not done the homework on how to turn them on.  

JGL

Attachments

Images (2)
  • insulated rail
  • Ins-rail-trigger SCh

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
×