Posted: Mon Nov 18, 2019 9:25 pm Post subject: Train reverse switch
This is a simple project i like to share.
I have a cheap Xmas train that only drives forward. But you need a lot of space for the rails. Recently i noticed that there are also expensive trains that driver forward and then go back. This way you only need a straight piece of rails.
So i decided as a fun weekend project that i would hack my train.
I used a special motor control IC : BA6287. I used this chip in customer projects so i had them. But you can use a relay or some FETs too, to control direction.
The processor is a small 8 pin tiny22 or 90S2343. It runs at 1 Mhz internal osc.
The train has a 3V battery and both chips work perfect with this voltage.
The code is simple : a reed contact on the bottom front of the train is connected to a processor pin. And 2 magnets are placed on the rails. One on each end of the rail.
When the trains drives the contact will close when it drives over the magnet. Then the code will switch to reverse.
There is a delay because otherwise the train will halt again at the magnet. And effectively it will halt.
Code:
'---------------------------------------------------------- ' train.bas ' simple program to let a train switch direction when a ' reed contact detects a small magnet ' each detection/switch will reverse the direction ' a special motor driver is used BA6287 ' PB3. and PB.4 control the direction of BA6287 ' PB.1 is connected to a reed switch 'intended purpose is to let a model train drive forward and reverse '---------------------------------------------------------- $regfile="2343def.dat"'same as tiny22 $crystal=1000000'1 mhz ! $hwstack=32 $swstack=16 $framesize=24
Waitms1000'some start delay
Dim Direction AsByte
Fin AliasPortb.3
Rin AliasPortb.4
'fin rin mode 'H L forward 'L H reverse 'H H brake 'L L standby ConfigPortb=Output ConfigPinb.1=Input Portb.1=1'direction pull
Gosub Forward 'start forward
Do IfPinb.1=0Then Gosub Motor_break 'break Waitms100 If Direction =0Then Gosub Forward
Else Gosub Reverse
End If Waitms1000'to prevent we detect the switch again End If
Loop
Motor_break:
Fin =1'break
Rin =1 Return
Forward:
Direction =1
Fin =1'forward
Rin =0 Return
Reverse:
Direction =0
Fin =0'reverse
Rin =1 Return
The nice thing is that the train will also will keep working as it did before. when there is no magnet, it will keep driving forward. _________________ Mark
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum