Shop OBEX P1 Docs P2 Docs Learn Events
TCS230 Color Sensor and LEDs — Parallax Forums

TCS230 Color Sensor and LEDs

scassinscassin Posts: 4
edited 2009-04-02 16:43 in BASIC Stamp
Hello,
·
I am using a BS2e education board with a TCS230 color sensor.· I am also using a DB-Expander Daughterboard-to-SIP.· I am aiming to have the sensor not only read-in and scan through the eeprom to look for matches but to also activate external·red, green·and blue·LEDs.· I would like to have three LEDs hooked up to the sensors output (slot C on the daughterboard).· The aim is to have the sensor scan through the colors and once, for example red is matched, then this would turn on the red led.· If I then turn to another color and its match is found in the eeprom, this would turn on its representative LED.· I will using a total of three LEDs (red, blue and green).·
·
Eventually, the overall goal would be to have the external red, green, and blue LEDs vary in intensity based on the intensity of the representative colors which·are sensed.
·
·
Current Code for to read colors or references ratios (From Parallax):
·'
'·· File...... TCS230DB_demo(DB-Expander).bs2
'·· Purpose... To extract color data from the TCS230-DB via the DB-Expander
'·· Author.... Parallax, Inc.
'·· E-mail.... support@parallax.com

'·· Started... 8 November 2007
'·· Updated...
'
'·· {$STAMP BS2e}
'·· {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]

' This program is used to get and output color data from the TCS230-DB
' color sensor daughterboard when connected to a BS2 via the DB-Expander.

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

' Change the "BS2" pin designators as needed.
' Sig········ BS2·· DB-Expander
' ---········ ---··


· OUT··· PIN·· 9··· '··· C
· LED··· PIN·· 10··· '··· D
· S2···· PIN·· 6··· '··· E
· S3···· PIN·· 5··· '··· F

'
[noparse][[/noparse] Constants ]

' Define count periods for each color. Adjust these for readings just under
' 255 for a white sheet of paper.

pRED···· CON·· 12···· 'Red reading period.
pGREEN·· CON··· 8···· 'Green reading period.
pBLUE··· CON··· 6···· 'Blue reading period.

'
[noparse][[/noparse] Variables ]

RED····· VAR·· Word·· 'Red color reading.
GREEN··· VAR·· Word·· 'Green color reading.
BLUE···· VAR·· Word·· 'Blue color reading.

'
[noparse][[/noparse] Initialization ]


· HIGH· LED·········· 'Turn on LED.

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

DO
· GOSUB· Color······· 'Get the color data, and output to DEBUG.
· DEBUG· "R", DEC3 RED
· DEBUG· " G", DEC3 GREEN
· DEBUG· " B", DEC3 BLUE
· DEBUG· CR
LOOP

'
[noparse][[/noparse] Subroutines ]

' Color: Read all three color components.
Color:
· LOW· S2·················· 'Address the red output.
· LOW· S3
· COUNT· OUT, pRED, RED···· 'Read the red component.
· HIGH· S3················· 'Address the blue output.
· COUNT· OUT, pBLUE, BLUE·· 'Read the blue component.
· HIGH· S2················· 'Address the green output.
· COUNT· OUT, pGREEN, GREEN 'Read the green component.
· RETURN

·
·
Code used to Scan and Look for matches (Color Me Tickled Code):
·
'
[noparse][[/noparse] Title ]
' Buildg A Spectrometer (Sample Code) - colorselect_1.BS2
' {$STAMP BS2e}
' {$PBASIC 2.5}
'
[noparse][[/noparse]Inializations]
TcsLeds·· PIN 10 ' LEDs enable - active low
TcsFreq·· PIN 9 ' freq from TCS230
TcsS2···· PIN 6 ' color filter control
TcsS3···· PIN 5
'
[noparse][[/noparse] Declarations ]
Red·········· CON 0 ' TCS230 filter selection
Green········ CON 1
Blue········· CON 2
Clear········ CON 3
ScanTime····· CON 10 ' scan time in millisecs
ScaleMax····· CON 255 ' max for scaled values
IsOn········· CON 0 ' LED control is active low
IsOff········ CON 1
NumColors···· CON 7 ' seven M&M colors
ColorThresh·· CON 4 ' allowable variance
TermChar····· CON 0 ' terminater for strings
StrLen······· CON 12 ' max length of names
'
[noparse][[/noparse] Variables ]
filter··· VAR Nib ' filter selection
rawColor· VAR Word ' raw return from TCS230
calRed··· VAR Word ' red calibration
calGrn··· VAR Word ' green calibration
calBlu··· VAR Word ' blue calibration
redVal··· VAR Byte ' red value
grnVal··· VAR Byte ' green value
bluVal··· VAR Byte ' blue value
rgb······ VAR redVal ' colors array
inKey···· VAR Byte ' input from user
colIdx··· VAR Nib ' color index
rgbIdx··· VAR Nib ' rgb index
testVal·· VAR Byte ' test value
eePntr··· VAR Word ' data table pointer
char····· VAR inKey ' char to print
'
[noparse][[/noparse] EEPROM Data ]
' RGB data
Colors DATA 098, 064, 057 ' brown
DATA 127, 049, 054 ' red
DATA 161, 119, 084 ' orange
DATA 180, 193, 103 ' yellow
DATA 031, 089, 093 ' green
DATA 028, 068, 163 ' blue
DATA 052, 057, 124· ' violet

' Color Names
CN0 DATA "Brown", 0
CN1 DATA "Red", 0
CN2 DATA "Orange", 0
CN3 DATA "Yellow", 0
CN4 DATA "Green", 0
CN5 DATA "Blue", 0
CN6 DATA "Violet", 0
'

[noparse][[/noparse] Initialization ]
Setup:
TcsLeds = IsOff ' start off
OUTPUT TcSLeds ' allow direct control
GOSUB Calibrate_White ' white balance sensor
GOSUB Calibrate_Colors ' calibrate color table
'
[noparse][[/noparse] Program Code ]
Main:
DO
GOSUB Read_RGB ' scan color
DEBUG "RGB = ", ' display components
DEC3 redVal, ", ",
DEC3 grnVal, ", ",
DEC3 bluVal, " "
GOSUB Match_Color ' compare scan to table
IF (colIdx < NumColors) THEN ' match was found
GOSUB Print_Color
DEBUG CR
ELSE
DEBUG "No match", CR
ENDIF
PAUSE 1000 ' delay between scans
LOOP
END
'
[noparse][[/noparse] Subroutines ]
' Calibrates "white" to ambient conditions
Calibrate_White:
DEBUG CLS, "TCS230 White Balance"
DEBUG CR, CR, "Insert white sample. Press a key to scan..."
DEBUGIN inKey
GOSUB White_Balance
DEBUG CR, CR, "White balance complete."
PAUSE 1000
DEBUG CLS
RETURN
' Reads "white" and calculates calibration values
White_Balance:
filter = Red
GOSUB Read_Color ' read raw red
calRed = ScaleMax * 256 / rawColor ' calculate red cal
filter = Green
GOSUB Read_Color ' read raw green
calGrn = ScaleMax * 256 / rawColor ' calculate green scale
filter = Blue
GOSUB Read_Color ' read raw blue
calBlu = ScaleMax * 256 / rawColor ' calculate blue scale
RETURN
' Calibrates color table to ambient conditions
Calibrate_Colors:
FOR colIdx = 0 TO (NumColors - 1) ' loop through all colors
DEBUG CLS, "TCS230 Color Calibration: "
GOSUB Print_Color
DEBUG CR, CR, "Insert sample. Press a key to scan..."
TcsLeds = IsOn ' light up scan area
DEBUGIN inKey
GOSUB Read_RGB ' scan sample item
eePntr = Colors + (3 * colIdx) ' point to table entry
WRITE eePntr, redVal, grnVal, bluVal ' save new data
NEXT
DEBUG CLS
RETURN
' Reads selected color from TCS230
' -- takes "filter" as input
' -- returns "rawColor" as output (unscaled color value)
Read_Color:
SELECT filter
CASE Red
LOW TcsS2
LOW TcsS3
CASE Green
HIGH TcsS2
HIGH TcsS3
CASE Blue
LOW TcsS2
HIGH TcsS3
CASE ELSE ' clear -- no filter
HIGH TcsS2
LOW TcsS3

ENDSELECT
TcsLeds = IsOn ' light sample
COUNT TcsFreq, ScanTime, rawColor ' return unscaled value
TcsLeds = IsOff
RETURN
' Reads and scales RGB colors
Read_RGB:
filter = Red
GOSUB Read_Color
redVal = rawColor */ calRed MAX ScaleMax
filter = Green
GOSUB Read_Color
grnVal = rawColor */ calGrn MAX ScaleMax
filter = Blue
GOSUB Read_Color
bluVal = rawColor */ calBlu MAX ScaleMax
RETURN
' Compares current color scan with known values in
' table. If match is found, the value of "colIdx"
' will be less than "NumColors"

Match_Color:
colIdx = 0
DO WHILE (colIdx < NumColors) ' check known colors
rgbIdx = 0
DO WHILE (rgbIdx < 3) ' compare rgb components
eePntr = Colors + (colIdx * 3) + rgbIdx ' point to color table
READ eePntr, testVal ' read known r, g or b
testVal = ABS(testVal - rgb(rgbIdx)) ' calculate variance
IF (testVal > ColorThresh) THEN EXIT ' if out-of-range, next
rgbIdx = rgbIdx + 1 ' test next component
LOOP
IF (rgbIdx = 3) THEN EXIT ' match found
colIdx = colIdx + 1 ' try next color
LOOP
RETURN
' Print color name
' -- takes "colIdx" as input
' -- allow this to fall through to Print_String
Print_Color:
LOOKUP colIdx, [noparse][[/noparse]CN0, CN1, CN2,
CN3, CN4, CN5, CN6], eePntr

' Print a string stored in DATA table
' -- point to first character with "eePntr"
Print_String:
DO
READ eePntr, char ' reach character
IF (char = TermChar) THEN EXIT ' end of string?
DEBUG char ' no -- print char
eePntr = eePntr + 1 ' point to next
LOOP
RETURN

·
·
I would really appreciate some help on this project.·
·
Thanks in advance,
steve

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-04-02 16:30
    Hello, your duplicate post has been removed. Please post messages on a subject only once in the forums.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • scassinscassin Posts: 4
    edited 2009-04-02 16:34
    sorry about that, I forgot to post the subject title in the original posting.

    Thanks,
    steve
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-04-02 16:43
    In the future you can edit your subject/message by clicking the pencil icon located to the top-right of the message. Also, it is a good idea to post full programs as an attachment rather that pasting them into the message. It makes following the message/code more difficult. As an attachment code can easily be loaded into the BASIC Stamp Editor with formatting intact. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.