Shop OBEX P1 Docs P2 Docs Learn Events
BASIC Stamp Programming Help — Parallax Forums

BASIC Stamp Programming Help

' =========================================================================
'
' File...... ColorPAL_mimic.bs2
' Purpose... ColorPAL color sense and mimic program.
' Author.... Phil Pilgrim -- Bueno Systems, Inc.
' E-mail.... propeller@phipi.com
' Started... 17 Apr 2009
' Updated... 1 Dec 2009
' Updated... 26 Jan 2010: Changed reset delay from 50 ms. to 80ms.
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================

'
[ Program Description ]

' This program senses colors and mimics what it sees with its RGB LED.
' It is first necessary to do a white reference before sensing any colors.
' A black reference is optional. Then, once a color has been sensed, it will
' show on the LED.

'
[ I/O Definitions ]

sio PIN 15 ' Serial I/O com pin for ColorPAL.

'
[ Constants ]

' Baudrate definitions. Serial I/O must be open-drain. The ColorPAL includes
' a pullup internally.

#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
baud CON 119 + 32768
#CASE BS2SX, BS2P
baud CON 327 + 32768
#CASE BS2PX
baud CON 536 + 32768
#ENDSELECT

'
[ Variables ]

red VAR Word ' Received RGB values from ColorPAL.
grn VAR Word
blu VAR Word

bkred VAR Word ' Black reference components.
bkgrn VAR Word
bkblu VAR Word

whred VAR Word ' White reference components.
whgrn VAR Word
whblu VAR Word

cmd VAR Word ' Command byte.

'
[ Initialization ]

GOSUB Reset ' Reset the ColorPAL and enter direct command mode.

DEBUG CLS, "Enter command:", CR
DEBUG " b: Black reference.", CR
DEBUG " w: White reference.", CR
DEBUG " s: Sample and display.", CR,CR

'
[ Program Code ]

DO
DEBUGIN cmd
DEBUG CRSRXY, 0, 5, CLRDN
SEROUT sio, baud, ["=m!"] ' Request multicolor sense.
SERIN sio, baud, [HEX3 red, HEX3 grn, HEX3 blu] ' Receive RGB data back.
SELECT cmd
CASE "b"
bkred = red
bkgrn = grn
bkblu = blu
DEBUG "Black reference is: R", DEC red, " G", DEC grn, " B", DEC blu
CASE "w"
whred = red
whgrn = grn
whblu = blu
DEBUG "White reference is: R", DEC red, " G", DEC grn, " B", DEC blu
CASE "s"
IF (whred = 0) THEN
DEBUG "Need to do a white reference first."
ELSE
DEBUG "Raw value: R", DEC red, " G", DEC grn, " B", DEC blu, CR
IF (red > bkred) THEN red = (red - bkred) << 6 / ((whred - bkred) >> 2) MAX 255 ELSE red = 0
IF (grn > bkgrn) THEN grn = (grn - bkgrn) << 6 / ((whgrn - bkgrn) >> 2) MAX 255 ELSE grn = 0
IF (blu > bkblu) THEN blu = (blu - bkblu) << 6 / ((whblu - bkblu) >> 2) MAX 255 ELSE blu = 0
DEBUG "Corrected: R", DEC red, " G", DEC grn, " B", DEC blu
SEROUT sio, baud, ["=r", HEX2 red >> 3, HEX2 grn >> 3, HEX2 blu >> 5, "!"]
ENDIF
ENDSELECT
LOOP

'
[ Subroutines ]

' Reset: Sends a long break to reset ColorPAL and enter direct command mode.

Reset:
LOW sio 'Pull sio low to eliminate any residual charge.
INPUT sio 'Return pin to input.
DO UNTIL sio : LOOP 'Wait for pin to be pulled high by ColorPAL.
LOW sio 'Pull pin low.
PAUSE 80 'Keep low for 80ms to enter Direct mode.
INPUT sio 'Return pin to input.
PAUSE 10 'Pause another 10ms
RETURN




Here is the program I am trying to understand. The part I am having the most trouble understanding is DEBUGIN cmd does that mean to retrieve the command of putting b,w,s? Also I do not understand the if statements very well. Why does it start with whred = 0. Also how can I find out the values the ColorPAL uses for example I am trying to write my own code but I have no idea what to compare sensing color values to. The whole IF ELSE part is very confusing to me. " IF (whred = 0) THEN
DEBUG "Need to do a white reference first."
ELSE
DEBUG "Raw value: R", DEC red, " G", DEC grn, " B", DEC blu, CR
IF (red > bkred) THEN red = (red - bkred) << 6 / ((whred - bkred) >> 2) MAX 255 ELSE red = 0
IF (grn > bkgrn) THEN grn = (grn - bkgrn) << 6 / ((whgrn - bkgrn) >> 2) MAX 255 ELSE grn = 0
IF (blu > bkblu) THEN blu = (blu - bkblu) << 6 / ((whblu - bkblu) >> 2) MAX 255 ELSE blu = 0
DEBUG "Corrected: R", DEC red, " G", DEC grn, " B", DEC blu
SEROUT sio, baud, ["=r", HEX2 red >> 3, HEX2 grn >> 3, HEX2 blu >> 5, "!"]
ENDIF

Comments

  • DEBUGIN reads a character from the debug terminal. If it's a "b", it assumes the ColorPAL is looking at something black; if a "w", something white; if an "s", it reads the color that the sensor is looking at and duplicates it with the LED. The "b" and "w" are necessary to calibrate the ColorPAL so it can duplicate subsequent colors accurately.

    -Phil
  • Oh okay, I understood that from the program, but I am not sure about the cmd. Why is that variable necessary does that initiate the process?

    What I want to do is detect colors, for example if it detects red I want to make a separate LED light flash. Thus, I am trying to understand the IF statement and process necessary to do this.
  • When the program starts, it has no idea what constitutes black and white. So you have to calibrate it first using the "b" and "w" commands. Once that's done, you can use the "s" command as much as you want to duplicate the colors the ColorPAL sees.

    To detect "red" you will have to sense all three colors, R, G, and B. It will be looking at "red" when the ratios R/G and R/B are high (i.e. over a threshold that you will have to determine from experimentation) and when the overall color is bright enough (i.e. when R by itself is over some pre-determined threshold).

    -Phil

  • Jacqueline

    Have you run the program?

    Is it the complete program or just the calibration part?

    Look up DEBUGIN in Basic Stamp Help in Basic Stamp Editor.

    Have a nice day!
  • Phil Pilgrim: Thank you! I did that and understood that, but I am having trouble programming. For example I want the ColorPAL to sense the color red then for example move a motor. For example If red then move motor.

    microcontrolleruser: Okay thank you!

  • jacquelinevl03jacquelinevl03 Posts: 18
    edited 2017-02-02 09:10
    I also do not understand this.

    IF (red > bkred) THEN red = (red - bkred) << 6 / ((whred - bkred) >> 2) MAX 255 ELSE red = 0

    Okay so for the red-bkred why are the bits being shifted to the left by 6. And for the whred-bkred why are the bits being shifted to the right by 2? The bit shift is confusing me.

    For the red > bkred. That is saying if red greater than "blackred" (which would be saying the red is too dark right?) so since it's too dark THEN calibrate it.
    And for the ELSE red = 0. Okay so else would be red either being less than or equal to bkred. But if it less than or equal to why is it equal to 0? Also why not compare it to whred?

    I also still do not really understand cmd. Why SELECT cmd why not SELECT red, why is that variable necessary? I get the SELECT case needs a variable so is the cmd just because it needs a variable?


  • I also need help with this line
    SEROUT sio, baud, ["=r", HEX2 red >> 3, HEX2 grn >> 3, HEX2 blu >> 5, "!"]

    I get that it stored the new values and the ColorPAL is displaying them. But what is the "=r" for and what is the "!" doing there.

    SEROUT sio, baud, ["=m!"] Here is a multicolor sense, these two things look similar but I am having trouble deciphering the one mentioned above.
Sign In or Register to comment.