Shop OBEX P1 Docs P2 Docs Learn Events
Tcs230 — Parallax Forums

Tcs230

agentmcmillanagentmcmillan Posts: 4
edited 2009-05-18 00:11 in Accessories
I am using the TCS230 to ic color but instead of debugging out I want it to trigger ports high or low on the prototyping daughter board. its been a really long time since I have played with a stamp. I have found the sample code. I don't know how to have the stamp compare what it is "seeing" to what would be in a table got a specif color range and then to run a subroutine based off of that.

Comments

  • agentmcmillanagentmcmillan Posts: 4
    edited 2009-05-05 01:00
    Ok I have made some progress. but is the BASIC Stamp 2pe Motherboard able to drive multiple DB at the same time. and how do you use the Owio command when using 2 daughter boards?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-05 02:37
    Yes, you can drive two DBs. The owio pin for socket A is P10; for B, P6.

    -Phil
  • agentmcmillanagentmcmillan Posts: 4
    edited 2009-05-06 18:47
    So I have figured out the constants but now the issue I am having is comparing the current color of the object to a table and running subroutines that are based on the specif color
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-06 23:15
    Take a look at the M&M sorting program listed here. There's a subroutine called "Match_Color" that should get you well on your way.

    -Phil
  • agentmcmillanagentmcmillan Posts: 4
    edited 2009-05-18 00:11
    Ok so I have figured some of it out i just need to get this to work I know I am close

    the issue is that when the code is ran it says that all levels are at 100 and thus nothing matches

    in the final sub routine I am attempting to have it output to a set transistors

    
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
    TcsLed         PIN   12                    ' LEDs enable - active low
    TcsOut         PIN   11                    ' freq from TCS230
    S2             PIN   9                     ' color filter control
    S3             PIN   8                     ' color filter control
    Rout0          PIN   5                     'port d
    Rout1          PIN   1                     'port e
    Rout2          PIN   0                     'port f
    Rout3          PIN   2                     'port h
    Rout4          PIN   3                     'port g
    
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    Red          CON 0 ' TCS230 filter selection
    Green        CON 1
    Blue         CON 2
    Clear        CON 3
    ScanTime     CON 10 ' scan time in millisecs
    ScaleMax     CON 100 ' max for scaled values
    IsOn         CON 0 ' LED control is active low
    
    IsOff        CON 1
    NumColors    CON 7 ' seven M&M colors
    ColorThresh  CON 10 ' 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 038, 007, 005    ' red
              DATA 075, 022, 008    ' orange
              DATA 086, 060, 011    ' yellow
              DATA 019, 044, 020    ' green
              DATA 021, 017, 031    ' violet
    
    ' Color Names
    CN0 DATA "Red", 0
    CN1 DATA "Orange", 0
    CN2 DATA "Yellow", 0
    CN3 DATA "Green", 0
    CN4 DATA "Violet", 0
    
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    Setup:
    TcsLed = IsOff           ' start off
    OUTPUT TcsLed            ' 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
    GOSUB Show_Color
    DEBUG CR
    ELSE
    DEBUG "No match", CR
    ENDIF
    PAUSE 5000 ' 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 Press key to scan."
    DEBUGIN inKey
    GOSUB White_Balance
    DEBUG CR, CR, "White balance complete."
    PAUSE 1000
    DEBUG CLS
    RETURN
    
    '======================White balance==============================================
    ' 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
    
    '========== calibrate Colors======================================================
    ' 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 Color Press  key to scan."
    TcsLed = 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
    
    
    '======Read Color=========================================================
    ' Reads selected color from TCS230
    ' -- takes "filter" as input
    ' -- returns "rawColor" as output (unscaled color value)
    Read_Color:
    SELECT filter
    CASE Red
    LOW S2
    LOW S3
    CASE Green
    HIGH S2
    HIGH S3
    CASE Blue
    LOW S2
    HIGH S3
    CASE ELSE ' clear -- no filter
    HIGH S2
    LOW S3
    ENDSELECT
    TcsLed = IsOn ' light sample
    COUNT TcsOut, ScanTime, rawColor ' return unscaled value
    TcsLed = IsOff
    RETURN
    
    
    
    '====Read RGB===============================================================
    ' 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
    
    '====Match Color============================================================
    ' 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============================================================
    ' 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], eePntr
    
    '=======Print_String========================================================
    ' 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
    
    '====Show_Color=============================================================
    '
    ' Show_Color displays the name of the found color on the LCD screen.
    '
    Show_Color:                                     ' print color name on screen
     SELECT eePntr
      CASE 0
        GOSUB No_Color
      CASE 1
        GOSUB Red_Found
      CASE 2
        GOSUB Orange_Found
      CASE 3
        GOSUB Yellow_Found
      CASE 4
        GOSUB Green_Found
      CASE 5
        GOSUB Purple_Found
      ENDSELECT
       DEBUG CR
      RETURN
    
    
    '-------[noparse][[/noparse]color subroutines]------------------------
    'each sub routine is setup in a way that allows the "serial out" to the robot
    No_Color:
      LOW Rout0
      LOW Rout1
      LOW Rout2
      LOW Rout3
      LOW Rout4
      DEBUG "no color found"
    
    Red_Found:
      HIGH Rout0
      LOW Rout1
      LOW Rout2
      LOW Rout3
      LOW Rout4
      DEBUG "Found Red"
    
    Orange_Found:
      LOW Rout0
      HIGH Rout1
      LOW Rout2
      LOW Rout3
      LOW Rout4
      DEBUG "Found Orange"
    
    Yellow_Found:
      LOW Rout0
      LOW Rout1
      HIGH Rout2
      LOW Rout3
      LOW Rout4
      DEBUG "Found Yellow"
    
    Green_Found:
      LOW Rout0
      LOW Rout1
      LOW Rout2
      HIGH Rout3
      LOW Rout4
      DEBUG "Found Green"
    
    Purple_Found:
      LOW Rout0
      LOW Rout1
      LOW Rout2
      LOW Rout3
      HIGH Rout4
      DEBUG "Found Purple"
    
    
Sign In or Register to comment.