Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Train reverse switch

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5091
Location: Holland

blank.gif
PostPosted: Mon Nov 18, 2019 9:25 pm    Post subject: Train reverse switch Reply with quote

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

Waitms 1000                                                 'some start delay

Dim Direction As Byte

Fin Alias Portb.3
Rin Alias Portb.4

'fin   rin  mode
'H      L     forward
'L      H     reverse
'H      H     brake
'L      L     standby
Config Portb = Output
Config Pinb.1 = Input
Portb.1 = 1                                                 'direction pull

Gosub Forward                                               'start forward

Do
   If Pinb.1 = 0 Then
      Gosub Motor_break                                     'break
      Waitms 100
      If Direction = 0 Then
         Gosub Forward
      Else
         Gosub Reverse
      End If
      Waitms 1000                                           '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
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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