14m2 IR sensor inputs

I'm using a 14m2 for a model railroad crossing. I am using mj5000 IR sensors, on input c.0 and c.4. c.0 does fine, senses the train quickly, starts the program. c.4 is slower, the train is gone with no signal. It passed over the sensor before the voltage on the output of the sensor changed enough to affect the program. Why is c.0 so fast and the other inputs slower? Any way to speed up another input? I also have tried other IR sensors, with trim pots, that break the signal on sensing with no improvement. Opto-couplers help somewhat, but a second fast input would solve my problem.
 

hippy

Technical Support
Staff member
Why is c.0 so fast and the other inputs slower? Any way to speed up another input?
Pin C.4 shouldn't be any slower than pin C.0. There are some small differences within the firmware but they will be in the order of microseconds, should not be detectable in the overwhelming majority of cases and should not affect a program using IR sensors.

It may be there is some other issue, hardware or software. Check the sensor does go to the C.4 pin, the circuit is that same as that for C.0, and perhaps post your code.

Details of what PICAXE board you are using would help if you are using one. It may be that pin C.4 has a pull-down or pull-up which is affecting the input while pin C.0 does not.
 

hippy

Technical Support
Staff member
I would write some test code which simply sets the LED when a sensor is activated and do the same for the other. That will help determine if you have an actual sensor problem, whether related to input pin used, or some software issue with your code.

I suspect it might be a code problem which seems to be a bit more complicated than it needs to be, particularly with respect to using the SR Latch, feeding that back into the 14M2.

Interrupts could be used to handle the two sensors but you could probably simplify things greatly by using the multi-tasking capabilities of the M2. One task to monitor the sensors and set flag variables to show status of things, a second task to flash the LED's to reflect the status set.

Only tested in simulation but this might work ...

Code:
#Picaxe 14M2
#No_Data

symbol trip1        = pinC.0
symbol trip2        = pinC.4

symbol gate         = B.3

symbol red1         = B.4
symbol red2         = B.5

Symbol ACTIVATED    = 0

Symbol trainPresent = b0

Start0:
  Do
    Do : Loop Until trip1 = ACTIVATED
    trainPresent = 1
    Do : Loop Until trip2 = ACTIVATED
    trainPresent = 0
    Do : Loop While trip1 = ACTIVATED Or trip2 = ACTIVATED
  Loop

Start1:
  Do
    low gate
    Do : Loop Until trainPresent <> 0
    high gate
    Do While trainPresent <> 0
      high red1 : low  red2 : pause 500
      low  red1 : high red2 : pause 500 
      low  red2
    Loop  
  Loop
Looking at that, the sluggishness of Trip2 may simply be that it takes up to a second after Trip2 activates for the LED's to stop flashing and the gate to go up.
 

lbenson

Senior Member
Note that using your email address (if valid) is not recommended as a forum name, since malefactors can scrape it up and use it to do what they do.
 

Dartmoor

Member
As an enhancement to your red flashing LED's, you can try changing the 'pause 500' to 'pause 400'. Then add a line with both LED's on: "high red1 : high red2 : pause 100" at each change of state.
This is in Hippy's
Start1:
Do : Loop Until trainPresent <> 0


This seems very trivial but it will look completely different. On a real railway/railroad crossing one of the lamps is always on. It ensures there is a lamp lit if the system fails at any stage &, more importantly, it is to represent a man waving a red lamp from side to side!
 
Thanks, it's almost perfect, hippy. In my program, Trip1 is the 'enter' sensor, goes high when the lead car is sensed, starts the light and gate cycle. Trip2 goes high when the lead car passes over, goes low when the last car is past. This low ends the program, and also starts the whole thing off when first energized, setting gate low. I had this working, but don't know how I did it before.
Otherwise, the sensors and inputs seem to be very quick, now.
 

hippy

Technical Support
Staff member
In my program, Trip1 is the 'enter' sensor, goes high when the lead car is sensed, starts the light and gate cycle. Trip2 goes high when the lead car passes over, goes low when the last car is past.
Just changing to "Symbol ACTIVATED = 1" should make my code match yours.
 
Thanks so much, hippy. The program works satisfactorily. I could not have done it without you. I'll put this 14m2 in a crossing and test it. I expect all to be good enough for a toy train. The sensor seems to be quick and sensitive. The program seems to do the exit steps (gates up(low) and flashing lights off(low) as the first car enters the exit sensor (trip2 high) instead of when the last car leaves the exit (trip2 low), but I'm going to leave well enough alone. Again, thanks.
 
Hippy, I hate to be a bother, but is there any way to have the program go into 'exit mode' (gates up, flashers off, sound off) as the last car passes the trip 2 sensor? Having the program end before all the train has passed the crossing may be unacceptable to purist modelers. The gates going up too soon is not realistic.
I've tried switching around different sensors and changing the 'symbol activated' (line 12) but nothing works like I want it to.
PS, I have tried to change my forum name, but I'm not sure how to send an e-mail to support, apparently.
 

hippy

Technical Support
Staff member
Hippy, I hate to be a bother, but is there any way to have the program go into 'exit mode' (gates up, flashers off, sound off) as the last car passes the trip 2 sensor? Having the program end before all the train has passed the crossing may be unacceptable to purist modelers. The gates going up too soon is not realistic.
That should be possible. It was something I had thought about when I gave my earlier code but did not want to cloud the issue. Untested ...

Code:
Start0:
  Do
    ; Wait for entry to be triggered
    Do : Loop Until trip1 = ACTIVATED
    trainPresent = 1
    ; Wait for exit to be triggered
    Do : Loop Until trip2 = ACTIVATED
    ; Wait for exit to no longer be triggered
    [b]Do : Pause 100 : Loop While trip2 = ACTIVATED[/b]
    trainPresent = 0
    Do : Loop While trip1 = ACTIVATED Or trip2 = ACTIVATED
  Loop
You might need to adjust the PAUSE value to cater for gaps between trains. Might have to make that a more complicated command if that proves insufficient.
 
Testing shows trip 1 starting fine, but ten seconds later the gates go up.....and trip 2 has no effect. By the way, 'train present' (line 14) is still b0? and 'symbol activated' (line 12) is still 1?
I'm really curious about 'train present = b0. The manual wasn't very clear on that.
 

hippy

Technical Support
Staff member
I am not sure exactly what code you are using. The issue may be in how you changed things to try and make it work as required.

I would suggest going back to the code which was working which prematurely raises the gate when the train reaches Trip2. Check that is still working then post that code. Then everyone will be on the same page.

The "Symbol trainPresent = b0" defines a variable which is named as "trainPresent".
 
Yes, thanks for the prompt, hippy. I had made a mistake in my code. Once my mistake was fixed, your new code works wonderfully! The crossing works exactly as hoped, I am a happy customer.
 
Top