ColorPal -- help w/ color sensing/generating without computer
pinballwitch
Posts: 5
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.
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$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
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
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?)