Program

 

'{$STAMP BS2}

'{$PBASIC 2.5}

 

ir_det_pin CON 8

pause_time CON 20

active_low CON 0

 

ir_detect VAR IN8

 

ir_pulse VAR Word

counter VAR Nib

type VAR Nib

pulse_delay_time CON 2

 

debounce_time CON 20

ir_message VAR Byte

active_high CON 1

decimal_value VAR Word

 

main:

IF ir_detect = 1 THEN main

GOSUB find_and_display_start_pulse

GOSUB check_for_stop_bit

GOSUB convert_to_binary_number_display

GOSUB program_select

GOTO main

 

find_and_display_start_pulse:

 

FOR counter = 0 TO 15

PULSIN ir_det_pin,active_low,ir_pulse(0)

NEXT

 

RETURN

 

check_for_stop_bit:

PULSIN ir_det_pin,active_high,ir_pulse(0)

IF ir_pulse(0) > 1400 AND ir_pulse(0) <> 0 THEN continue

GOTO check_for_stop_bit

 

continue:

PULSIN ir_det_pin,active_low,ir_pulse(0)

PULSIN ir_det_pin,active_low,ir_pulse(1)

PULSIN ir_det_pin,active_low,ir_pulse(2)

PULSIN ir_det_pin,active_low,ir_pulse(3)

PULSIN ir_det_pin,active_low,ir_pulse(4)

PULSIN ir_det_pin,active_low,ir_pulse(5)

PULSIN ir_det_pin,active_low,ir_pulse(6)

PULSIN ir_det_pin,active_low,ir_pulse(7)

PULSIN ir_det_pin,active_low,ir_pulse(8)

PULSIN ir_det_pin,active_low,ir_pulse(9)

PULSIN ir_det_pin,active_low,ir_pulse(10)

PULSIN ir_det_pin,active_low,ir_pulse(11)

 

RETURN

 

convert_to_binary_number_display:

FOR COunter = 0 TO 10

LOOKDOWN ir_pulse(counter), < [400,800],

ir_message.LOWBIT(counter)

 

NEXT

 

RETURN

 

program_select:

 

IF (ir_message = %00000100) THEN stop0

IF (ir_message = %00000001) THEN front0

IF (ir_message = %00000011) THEN left0

IF (ir_message = %00000101) THEN right0

IF (ir_message = %00000111) THEN back0

IF (ir_message = %00000110) THEN area_cover

IF (ir_message = %00001000) THEN wall

 

RETURN

 

stop0:

DO

IF ir_detect = 0 THEN main

LOOP

 

front0:

FOR x = 1 TO 5

PULSOUT 14, 649

PULSOUT 15, 892

PAUSE 20

NEXT

GOTO main

 

back0:

FOR x = 1 TO 5

PULSOUT 14, 850

PULSOUT 15, 650

PAUSE 20

NEXT

GOTO main

 

left0:

FOR x = 1 TO 3

PULSOUT 14, 650

PULSOUT 15, 725

PAUSE 20

NEXT

GOTO main

 

right0:

FOR x = 1 TO 3

PULSOUT 14, 775

PULSOUT 15, 850

PAUSE 20

NEXT

GOTO main

 

'------Variables------

irmid VAR Bit

irleft VAR Bit

irright VAR Bit

irback VAR Bit

x VAR Word

a VAR Word

v VAR Word

w VAR Word

n VAR Byte

m VAR Byte

 

'------Constants-------

freq CON 38500

 

 

Area_Cover:

IF ir_detect = 0 THEN main

 

 

'------IR Readings-----

FREQOUT 6, 1, freq

irmid = IN4

 

'------Maneuvers------

 

IF irmid = 0 THEN turn

 

PULSOUT 14, 649

PULSOUT 15, 892

PAUSE 20

 

GOTO Area_Cover

 

turn:

x = x + 1

a = (x / 2) * 2

IF (x = a) THEN left

IF (x <> a) THEN right

 

right:

FOR n = 1 TO 220

PULSOUT 14, 743

PULSOUT 15, 850

PAUSE 20

NEXT

 

GOTO Area_Cover

 

left:

 

FOR x = 1 TO 209

PULSOUT 14, 650

PULSOUT 15, 757

PAUSE 20

NEXT

 

FOR x = 1 TO 65

PULSOUT 14, 649

PULSOUT 15, 892

PAUSE 20

NEXT

 

FOR x = 1 TO 65

PULSOUT 14, 725

PULSOUT 15, 892

PAUSE 20

NEXT

 

GOTO Area_Cover

 

'--------------------------------------------

 

wall:

IF ir_detect = 0 THEN main

FREQOUT 6, 1, freq '38500

IF IN4 = 0 THEN turn1

 

forward1:

PULSOUT 14, 649

PULSOUT 15, 892

PAUSE 20

 

GOTO wall

 

turn1:

FREQOUT 2, 1, freq '38500

irright = IN0

 

FREQOUT 10, 1, freq '38500

irleft = IN9

 

IF irright = 0 THEN left1

 

IF irleft = 0 THEN right1

 

 

GOTO wall

 

'--------Maneuvers------------

right1:

FOR m = 1 TO 140

PULSOUT 14, 743

PULSOUT 15, 850

PAUSE 20

NEXT

 

'--------straighten roller-----

FOR m = 1 TO 85

PULSOUT 14, 649

PULSOUT 15, 892

PAUSE 20

NEXT

 

FOR m = 1 TO 65

PULSOUT 14, 649

PULSOUT 15, 765

PAUSE 20

NEXT

GOTO back1

 

left1:

FOR m = 1 TO 150

PULSOUT 14, 650

PULSOUT 15, 757

PAUSE 20

NEXT

 

FOR m = 1 TO 70

PULSOUT 14, 649

PULSOUT 15, 892

PAUSE 20

NEXT

 

FOR m = 1 TO 65

PULSOUT 14, 735

PULSOUT 15, 892

PAUSE 20

NEXT

GOTO back1

 

back1:

PULSOUT 14, 850

PULSOUT 15, 650

PAUSE 20

FREQOUT 7, 1, 38500

IF 0 = IN5 THEN forward1

GOTO back1