Shop OBEX P1 Docs P2 Docs Learn Events
IR Line Following Sensors and ColorPal Sensors. — Parallax Forums

IR Line Following Sensors and ColorPal Sensors.

I am programming a robot to follow a black line with four IR Sensors and also have two ColorPal Light Sensors connected to the robot as well. The sensors will detect green, blue, and yellow. If they detect one of these colors, then a yellow, blue, or green light will be flashed on the breadboard. I have my line following and light sensing code completed but I do not know how to combine them for the robot to constantly be checking the light sensors for detection and also keep moving with the line following. Help.

' {$STAMP BS2}
' {$PBASIC 2.5}
' light

baud CON 119 + 32768
LeftCp CON 15
RightCp CON 14

red VAR Word
grn VAR Word
blu VAR Word
sio VAR Word

sio = LeftCp
GOSUB Reset
sio = RightCp
GOSUB reset

DO
sio = LeftCp
SEROUT sio, baud, ["= (00 $ m) !"]
SERIN sio, baud, [WAIT("$"), HEX3 red, HEX3 grn, HEX3 blu] ' Receive RGB data back.
'turn on light for yellow
IF (red <= 255 AND red >=100) AND (grn <= 255 AND grn >= 100) AND (blu >= 0 AND blu <=100) THEN
HIGH 10
PAUSE 2000
LOW 10
ENDIF
' turn on light for blue
IF (red <= 50 AND red >= 0) AND (grn <=50 AND grn >= 0) AND (blu <= 255 AND blu >= 100) THEN
HIGH 12
PAUSE 2000
LOW 12
ENDIF
' turn on light for green
IF (red <= 50 AND red >= 0) AND (grn <= 255 AND grn >= 50) AND (blu <= 100 AND blu >= 0) THEN
HIGH 11
PAUSE 2000
LOW 11
ENDIF

sio = RightCp
SEROUT sio, baud, ["= (00 $ m) !"]
SERIN sio, baud, [WAIT("$"), HEX3 red, HEX3 grn, HEX3 blu] ' Receive RGB data back.
'turn on light for yellow
IF (red <= 255 AND red >=100) AND (grn <= 255 AND grn >= 100) AND (blu >= 0 AND blu <=100) THEN
HIGH 10
PAUSE 2000
LOW 10
ENDIF
' turn on light for blue
IF (red <= 50 AND red >= 0) AND (grn <=50 AND grn >= 0) AND (blu <= 255 AND blu >= 100) THEN
HIGH 12
PAUSE 2000
LOW 12
ENDIF
' turn on light for green
IF (red <= 50 AND red >= 0) AND (grn <= 255 AND grn >= 50) AND (blu <= 100 AND blu >= 0) THEN
HIGH 11
PAUSE 2000
LOW 11
ENDIF
LOOP

counter VAR Word
qtis VAR Nib

OUTB = %1111

DO
GOSUB Check_Qtis

SELECT qtis

CASE %1000
GOSUB turnright
CASE %1100
GOSUB turnright
CASE %1110
GOSUB backleft
CASE %0100
GOSUB turnright
CASE %0110
GOSUB forward
CASE %0010
GOSUB turnleft
CASE %0111
GOSUB backleft
CASE %0011
GOSUB turnleft
CASE %0001
GOSUB turnleft
CASE %0000
GOSUB forward
CASE %1111
GOSUB backleft
CASE %1001
GOSUB backleft
CASE %1011
GOSUB backleft
CASE %1101
GOSUB backleft
CASE ELSE
PAUSE 3
ENDSELECT
LOOP


forward:
PULSOUT 12, 650
PULSOUT 13, 850
PAUSE 20
RETURN

right90:
FOR counter = 1 TO 18
PULSOUT 12, 650
PULSOUT 13, 650
PAUSE 20
NEXT
RETURN

veryleft:
PULSOUT 12, 650
PULSOUT 13, 700
PAUSE 20
RETURN

veryright:
PULSOUT 12, 800
PULSOUT 13, 850
PAUSE 20
RETURN

turnright:
PULSOUT 12, 750
PULSOUT 13, 850
PAUSE 20
RETURN

turnleft:
PULSOUT 12, 650
PULSOUT 13, 750
PAUSE 20
RETURN

backleft:
FOR counter = 1 TO 65
PULSOUT 12, 850
PULSOUT 13, 750
PAUSE 20
NEXT
RETURN

Check_Qtis:
DIRB = %1111
PAUSE 0
DIRB = %0000
PAUSE 0
qtis = INB
RETURN

Reset:
LOW sio
INPUT sio
DO UNTIL sio : LOOP
LOW sio
PAUSE 80
INPUT sio
PAUSE 10
RETURN

Comments

  • tomcrawfordtomcrawford Posts: 1,129
    edited 2016-02-05 18:20
    You gotta put both functions (read colorPAL/fiddlelights and followALine) inside the same Do/LOOP.
    DO
       readColorPAL/fiddleLights
       followALine
    LOOP
    

    As an aside, you have both colorPALs fiddling the same lights ;-)
  • Which part is the line following code? I'm new to programming
  • It starts here
    counter VAR Word
    qtis VAR Nib
    
    OUTB = %1111
    
    DO
    GOSUB Check_Qtis
    

    and goes on pretty much to the end. If you want to learn about programming robots with BS-2, I would suggest this link.
Sign In or Register to comment.