
; Sound addresses : P2, #53 - Belch; P2, #63 - Doh!; P2, #0 - Blank-avoid at all costs.  
; The ISD chip has a defect which can erase beginning locations of chip memory.

; R0,R1,R2 reserved for time delays
; R3, R4 reserved for IR sensor routines; R5 for checking sensor values

SOUNDCLK	EQU	P0.0			; Clock pin to the ISD chip, setting this LOW plays sound on P2 Address

MOTORPWR	EQU	P1.5			; Setting this pin HIGH turns both motors on, reverse by default
LEFTMTR		EQU	P1.3			; Setting this HIGH causes H bridge to make left motor go foreward
RIGHTMTR	EQU	P1.4			; Setting this HIGH causes H bridge to make right motor go foreward
						; (To make robot go foreward, all three lines must be set HIGH)
LeftIRLine	EQU	P1.6
RightIRLine	EQU	P1.7

MOV	P0,	#255				; Set P0 high to turn off LEDs on P0, as well as set SOUNDCLK high.
MOV	P1,	#0				; Set P1 low, P1 is motor control / IR sensors
MOV	P2,	#0				; Set P2 low, ISD address &00h.
MOV	P3,	#0				; Set P3 low, not used at this time.



Begin:

	SETB	MOTORPWR			; These three lines make robot go foreward by energizing all three
	SETB	LEFTMTR				; motor relays, power and two H bridge relays, one for each wheel.
	SETB	RIGHTMTR

	MOV	A,	#0			; Set A to #0
	MOV	R3,	#255			; Set R3 to 255, the number of times to scan the Left IR Sensor
LeftIRScan:
	LCALL	Shortdelay			; Slow this routine down.  IR runs at 40Khz, not 8Mhz.
	JNB	LeftIRLine,	Addleft		; If LeftIRLine is low (its active low), jump and add #1 to the ACC,
	DJNZ	R3,	LeftIRScan		; If not, rescan.
	SJMP	LeftFinished
Addleft:
	ADD	A,	#1
	DEC	R3
	SJMP	LeftIRScan
LeftFinished:
	MOV	R3,	A			; Store final value of A in R3


	MOV	A,	#0			; Set A to #0
	MOV	R4,	#255			; Set R4 to 255, the number of times to scan the Right IR Sensor
RightIRScan:
	LCALL	Shortdelay			; Slow this routine down.  IR runs at 40Khz, not 8Mhz.
	JNB	RightIRLine,	AddRight	; If RightIRLine is low (its active low), jump and add #1 to the ACC,
	DJNZ	R4,	RightIRScan		; If not, rescan.
	SJMP	RightFinished
AddRight:
	ADD	A,	#1
	DEC	R4
	SJMP	RightIRScan
RightFinished:
	MOV	R4,	A			; Store final value of A in R4

						; At this point, we should have R3 and R4 set based on the sensor inputs.
						; Had no sensor input been detected (that is, sensors remained high), the
						; values of R3 and R4 should be 0.  The closer an object was, the higher
						; these values will be.

	MOV	R5,	#125			; We're gonna see if either value is higher than 125 by adding to them.
CheckIRLoop:					; After 125 loops, each loop adding #1, if neither hits a value 255, they
	INC	R3				; were under 125 and should not be considered a trigger.
	INC	R4
	CJNE	R3,	#255,	Trigger1	; If R3 ever reaches 255 here, it was originally higher than 125.
	CJNE	R4,	#255,	Trigger2	; If R4 ever reaches 255 here, it was originally higher than 125.
	DJNZ	R5,	CheckIRLoop

LJMP	Begin


Shortdelay:
	MOV	R0,	#25
Loop:
	DJNZ	R0,	Loop
	RET


Longdelay:
	MOV	R0,	#250
	MOV	R1,	#250
	MOV	R2,	#250
Loop2:
	DJNZ	R0,	Loop2
	DJNZ	R1,	Loop2
	DJNZ	R2,	Loop2
	RET


Trigger1:					; We've got to change direction cause we're gonna run into something.
	MOV	P2,	#63			; P2 ISD address for DOH
	LCALL	Shortdelay			; Give the ISD chip some type to see the address before we clock it.
	CLR	LEFTMTR				; Turn off one relay, so this wheel will reverse, and turn the robot.
	CLR	SOUNDCLK			; Make it say DOH while it turns :)
	LCALL	Longdelay			; Wait
	SETB	SOUNDCLK
	SETB	LEFTMTR				; Go straight again.
	MOV	R3,	#0			; Set R3 to 0; we don't want it to go back into the check loop and trigger
	RET					; each cycle.  That would take forever, and royally suck.

Trigger2:					; We've got to change direction cause we're gonna run into something.
	MOV	P2,	#63			; P2 ISD address for DOH
	LCALL	Shortdelay			; Give the ISD chip some type to see the address before we clock it.
	CLR	RIGHTMTR			; Turn off one relay, so this wheel will reverse, and turn the robot.
	CLR	SOUNDCLK			; Make it say DOH while it turns :)
	LCALL	Longdelay			; Wait
	SETB	SOUNDCLK
	SETB	RIGHTMTR			; Go straight again.
	MOV	R3,	#0			; Set R3 to 0; we don't want it to go back into the check loop and trigger
	RET					; each cycle.  That would take forever, and royally suck.

END
