Servo with IRIN

krogerssolar

Senior Member
Hi all I need some help with my code I basicly don't know where to start I have a Railroad code the is using an LDR I'm looking to do a IRIN Cunrtly the code has two LDR inputs i would like to swap to IRIN inputs basically if the beam is broken the code should be triggered.

this is what i have
Code:
start0:
#PICAXE 14M2

;Define Outputs use portB pins
SYMBOL Light1 = b.1
SYMBOL Light2 = b.2
SYMBOL Light3 = b.3
SYMBOL Gong	  = b.4
SYMBOL boom1  = b.5

;Define inputs use portC pins
SYMBOL LDR_1 = c.0
SYMBOL LDR_2 = c.4

'Define variables
SYMBOL boompositn  = b1
SYMBOL currentpos  = b2
SYMBOL Flasherflag = b3
SYMBOL Lightlevel1 = b5 ; select a spare variable b5 here
SYMBOL LightLevel2 = b6

;define constants
SYMBOL BoomSpDelay = 60 'ms
SYMBOL FlashDelay = 500 'ms
SYMBOL Setpoint1 = 60 ; 0 to 255 is ADC input range - this is the trigger level for lights to flash
SYMBOL Setpoint2 = 60 ; 0 to 255 is ADC input range - this is the trigger level for lights to flash
SYMBOL BoomIsUp  = 120
SYMBOL BoomIsDown = 218
; first code section is for boom control 
init:
	servo boom1,120 ; initialise servo
	currentpos = BoomIsUp

BoomCtrl:
	READADC LDR_1, LightLevel1 ; check light level at side 1
	READADC LDR_2, LightLevel2 ; check light level at side 2
 

	IF currentpos = BoomIsUp THEN
	    IF LightLevel1 > Setpoint1 OR LightLevel2 > Setpoint2 THEN ; if train is detected at either sensor
		PAUSE FlashDelay  ; wait a while (say 2 flash periods) so lights flash before the boom starts to lower
		PAUSE FlashDelay
		FOR boompositn = BoomIsUp TO BoomIsDown	; lower the boom in increments
		    SERVOPOS boom1, boompositn		; move to next incremental position
		    PAUSE BoomSpDelay				; delay to give slower boom travel speed
		NEXT boompositn
		currentpos = BoomIsDown				; set the current boom position as down
	    ENDIF
	ENDIF
 
	IF currentpos = BoomIsDown THEN
	    IF LightLevel1 < Setpoint1 AND LightLevel2 < Setpoint2 THEN ; if train is clear of both sensors

		FOR boompositn = BoomIsDown to BoomIsUp STEP - 1 ; raise the boom in increments
		    SERVOPOS boom1, boompositn
		    PAUSE BoomSpDelay
		NEXT boompositn
		currentpos = BoomIsUp				; set the current boom position as up
	    ENDIF
	ENDIF

	GOTO BoomCtrl
;==========================================================
; run the flashing lights as a separate process
Start1:
	LOW Light1 ; turn off all lights and bell/gong
	LOW Light2
	low Light3
	LOW Gong
LightCtrl:
      IF LightLevel1 > Setpoint1 OR LightLevel2 > Setpoint2 THEN ; start flashing the lights as boom about to be lowered
	    DO
		IF Flasherflag = 0  THEN ; set for first time only in each boom down - up cycle
	  	  HIGH Light1  
		  LOW  Light2
		  HIGH Light3
                  Flasherflag = 4 ; any non zero value - to flag not to repeat
		ENDIF
		PAUSE FlashDelay
		HIGH Gong		
		TOGGLE Light1	; flash each light alternately
		TOGGLE Light2
		PAUSE FlashDelay
		LOW Gong

            LOOP UNTIL LightLevel1 < Setpoint1 AND LightLevel2 < Setpoint2 AND currentpos = BoomIsUp
	ENDIF	
	
	IF Flasherflag = 4 THEN  ; when the boom is fully raised
	    FOR Flasherflag = 4 to 1 step -1 ; flash lights for a few more cycles after the boom is down
		PAUSE FlashDelay         ; don't change the sequence 3 to 1 or will need to add b3 = 0 after the NEXT command
		HIGH Gong		
		TOGGLE Light1	; flash each light alternately
		TOGGLE Light2
		PAUSE FlashDelay
		LOW Gong
	    NEXT Flasherflag ; on exit b3 = 0 ready for the next cycle 

	    LOW Light1 ; turn off all lights
	    LOW Light2
	    LOW Light3
	ENDIF		
	GOTO LightCtrl
I Already have a IRIN working with just leds but don't know what to do for the servo or how to incorporate it with IR

Working LED IR code

Code:
#PICAXE 08M
main:

symbol Light1 = c.1
symbol Light2 = c.2
SYMBOL IRIN1     = pinc.3 ; The IR input to detect train presence
SYMBOL IRIN2     = pinc.4 ; The IR input to detect train presence
symbol Delay = 900 'ms


DO 
     IF IRIN1 =1 or IRIN2 =1 THEN  ; IR beam is broken
    low Light1
    pause Delay
    high Light1
    low Light2
    pause Delay
    high Light2   ; Red Lights Flashing
  ELSE ; IR beam is received
  ENDIF
LOOP
Anyone have an Idea.

Thanks
 

rossko57

Senior Member
IRIN command is usually used to receive a coded command e.g. like a TV remote button press. It's not really clear why you'd want to use that instead of a simple go/nogo break-beam arrangement, nor what might be gained from that.

In your 'working' code, it is just looking at input pin levels. It doesn't use the IRIN command at all, maybe there is confusion here? You've chosen to call the input pins IRINx, but they could just well be called Rhubarbx or whatever, and the code would work in the same way.
 

erco

Senior Member
Sounds like you're only interested in sensing DC light levels, whether visible or IR (the PICAXE neither knows nor cares). Per rossko57, you won't use IRIN. That's Sony SIRC tv remote protocol. You'll probably use a phototransistor, although you might get away with a photocell in some instances. And you'll continue to use simple IF checks to see if the pin connected to your sensor is high or low.
 
Top