Shop OBEX P1 Docs P2 Docs Learn Events
ColorPal -- help w/ color sensing/generating without computer — Parallax Forums

ColorPal -- help w/ color sensing/generating without computer

pinballwitchpinballwitch Posts: 5
edited 2010-04-21 15:25 in Accessories
I'm using Parallax's ColorPal sensor and what I've been trying to do with it (among other things) is to sense and generate the sensed color without being connected to the computer.

I'm pretty new to this, so I've been modifying the Parallax sample program for color mimicking. However, there's a "select case" section of the program where the computer user gives the cases, telling the sensor to take various samples and then assigning values to variables. I'm having an issue with that section...

Any help/advice is much appreciated.

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-04-16 01:39
    It would help if you would attach the code you are using then point out where in the code you are having problems.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • pinballwitchpinballwitch Posts: 5
    edited 2010-04-19 18:31
    The program is below, mostly adapted from the Parallax "mimic" program...the italicized area is where I think problems are occurring.

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

    '
    [noparse][[/noparse] 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.

    '
    [noparse][[/noparse] I/O Definitions ]

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

    '
    [noparse][[/noparse] 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

    '
    [noparse][[/noparse] 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


    '
    [noparse][[/noparse] Initialization ]

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

    '
    [noparse][[/noparse] Program Code ]

    DO
    SEROUT sio, baud, [noparse][[/noparse]"=m!"] ' Request multicolor sense.
    SERIN sio, baud, [noparse][[/noparse]HEX3 red, HEX3 grn, HEX3 blu] ' Receive RGB data back.
    GOSUB Bref
    GOSUB Wref
    GOSUB Lightup

    Bref:
    bkred = red
    bkgrn = grn
    bkblu = blu
    return

    Wref:
    whred = red
    whgrn = grn
    whblu = blu
    return

    Lightup:
    IF (whred = 0) THEN
    GOTO Wref
    ELSE

    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

    SEROUT sio, baud, [noparse][[/noparse]"=r", HEX2 red >> 3, HEX2 grn >> 3, HEX2 blu >> 5, "!"]
    ENDIF

    pause 3000


    '
    [noparse][[/noparse] 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
  • pinballwitchpinballwitch Posts: 5
    edited 2010-04-19 18:38
    Mostly, I think I'm misinterpreting something going on in the original program, which is found here as "mimic." Probably in the original SELECT CASE operation, which I changed to a series of GOSUBs (because there is no computer user to select a case)...
  • FranklinFranklin Posts: 4,747
    edited 2010-04-19 19:07
    Select and case are computer commands and don't require user input. Take a look at the manual and see how it works.
    You are assigning the value red to blkred in one spot and then checking to see if it is bigger, is that what you want?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • pinballwitchpinballwitch Posts: 5
    edited 2010-04-21 15:25
    In the original program the computer user does select the case:
    the manual said...
    Quicker Start (Color Sensing)
    1. Hook up your ColorPAL as shown below in the Installation section, using P15 for the signal.
    2. Download the BASIC Stamp program, ColorPAL_mimic.bs2, and RUN it in your BASIC Stamp.
    3. Calibrate on black and white subjects, using the “b” and “w” keys.
    4. Sample a color using the “s” key, then look at the LED to see that color being mimicked.
    My goal: sense and generate the sensed color without being connected to a computer.

    That part about the variables confuses me but it is from the original Parallax program...
    And, I think I need to re-sense after each reference (so get rid of the GOSUB thing) which might change the value for "red" after each new sensing which would be how "blkred" and "whred" and the final "red" might be different??

    (Is red = blkred retroactive, or will it keep the first value of red so that whred and blkred and the "s" case red will be different?)
Sign In or Register to comment.