Shop OBEX P1 Docs P2 Docs Learn Events
Please Help - How Can I Match One Color? I will do the rest, but I need help getting — Parallax Forums

Please Help - How Can I Match One Color? I will do the rest, but I need help getting

MiddleSchoolerMiddleSchooler Posts: 10
edited 2012-06-19 19:37 in Accessories
Hi,

I connected a TCS 3200-DB Color Sensor to my Board ofEducation Bs2, and got the LEDs working, thanks to the wonderful help I gothere.

I amtrying to use the sensor to distinguish among different objects, yellow, red orblue. The color of each object was calculated with the sensor and it is alreadyknown. For example, Yellow gives avalue of he color of: R041 G025 B011 when I use the code lines DEBUG "R", DEC3 RED; DEBUG "G", DEC3 GREEN; and DEBUG " B", DEC3 BLUE.

Using just one colorand one object as an example, I entered as EEPROM data this line: CYel DATA 186, 155, 058 ' yellow. But, I cannot get the values inputted by the sensor to compare with theEEPROM. Assuming RED, GREEN, and BLUEare the inputted sensor data of the object I was going to use IF RED + GREEN + BLUE = CYel THEN DEBUG"Yellow", and IF RED + GREEN + BLUE <> CYel THEN DEBUG "NoMatch".

But, all I get is a big mess. I looked through the M Sorter program and that just got me moreconfused. So that program does not have any help, in fact I tried to run it and also had total failure. Sorry, I am in grade 8 and Iseem to be getting easily confused as I try to spend most of my time doingsummer homework. I have tried to solvethis every way I can, but it just does not work. I feel like a total failure.

Can someone please help me with a few lines of code for onlyone color? I will do the rest, and theninstead of DEFRAG, I will make a pin active so I can control another devicewith a relay. I have gotten that partowrking as a module, but little good that does me, when I cannot get past thisMatching Color issue! And, I do notcare about white balance, that only further confuses this, I will learn thatand do a better and more in depth program later this summer, after I get thissimple version working. Can someoneplease help me with a few lines of code for comparing a color read by the TSCand a known color in EEPROM and how to compare them to see if that objectmatches the EEPROM color I had loaded??

Thank you for your help,
Rory

Comments

  • kwinnkwinn Posts: 8,697
    edited 2012-06-17 20:47
    Rory, I think the problem is that you will rarely get the same reading twice for an object. They will be close but not exactly the same. What you need to do is look at the difference between the current reading and the ones you have stored. The stored reading that has the lowest difference would be the closest match.
  • MiddleSchoolerMiddleSchooler Posts: 10
    edited 2012-06-17 21:39
    Thanks, I will use a range, seeking only a 85% match. Good point, thank you!

    However, I do not know how to write the code to bring the raw data up and to compare it with what I have stored. In fact, I get 3 digits when I use DEFRAG Dec 3 red, but if I use just DEFRAG red I only get a weird symbol. I need to get the data from the sensor, but how? What does it really look like (I only can see it with DEC 3)? How can I compare that with 041 025 011 , since that is really 3 signals in one DEFRAG, i.e., DEFRAG Dec 3 red, Dec 3 green, DEC blue? There must be a way to compare the relative color valuses for red, green and blue sent for each block scanned, but they are 3 values, so what do I do when my EEPROM has one line with all 3 values on the same line? I am totally lost on this - I have worked very hard, but I cannot figure this out. Can someone please give me some code lines so that I can dothis with one color?

    I am using this program to get my data, but I need to do more than display the data on a DEFRAG screen, I need to compare the data with known data that I load in EEProm. Please, help me solve this, thanks, Rory

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    [ I/O Definitions ]
    OUT PIN 2 ' C
    LED PIN 3 ' D
    S2 PIN 4 ' E
    S3 PIN 5 ' F ' Thanks Mr Phil for helping with the pins, I never could do this without you
    '
    [ Constants ]
    Define COUNT periods
    pRED CON 13 'Red reading period.
    pGREEN CON 12 'Green reading period.
    pBLUE CON 11 'Blue reading period.
    '
    [ Variables ]
    RED VAR Word 'Red color reading.
    GREEN VAR Word 'Green color reading.
    BLUE VAR Word 'Blue color reading.
    counter VAR Byte
    '
    [ Initialization ]
    HIGH LED 'Turn on LED
    '
    [ Program Code ]
    FOR counter = 1 TO 2
    GOSUB Color 'Get the color data, and output to DEBUG.
    DEBUG "R", DEC3 RED
    DEBUG " G", DEC3 GREEN
    DEBUG " B", DEC3 BLUE
    DEBUG CR
    NEXT
    ' No pause after the Dark runs
    HIGH LED
    PAUSE 100 ' want to wait before sensing starts
    FOR counter = 1 TO 4
    GOSUB Color 'Get the color data, and output to DEBUG.
    DEBUG "R", DEC3 RED
    DEBUG " G", DEC3 GREEN
    DEBUG " B", DEC3 BLUE
    DEBUG CR
    NEXT
    PAUSE 10
    'Repeat color sensing, it more accurate re-doing and use last run data
    FOR counter = 1 TO 4
    GOSUB Color 'Get the color data, and output to DEBUG.
    DEBUG "R", DEC3 RED
    DEBUG " G", DEC3 GREEN
    DEBUG " B", DEC3 BLUE
    DEBUG CR
    NEXT
    LOW LED
    ' Now Rory makes other relay flash & then END program
    LOW 13 ' Relay Off
    HIGH 13 ' Relay On
    PAUSE 300
    LOW 13 ' Relay Off
    END
    '
    [ 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
  • kwinnkwinn Posts: 8,697
    edited 2012-06-17 21:49
    Here is how I would do the comparison.
  • MiddleSchoolerMiddleSchooler Posts: 10
    edited 2012-06-18 19:24
    Kwinn,

    Thank you for the help, using an array to remove the "odd-ball" readings will help when I get this color matching code writing issue solved. Thank you, Rory
  • kwinnkwinn Posts: 8,697
    edited 2012-06-18 22:34
    You're welcome. Always nice to get feedback on our suggestions. I hate to admit this but I did make a mistake on the PDF I posted. Each of the 3 lines in the repeat loop should read Cabsdiff =.................. I left off the part. Sorry for the oversight.

    I hope you realize this array is not just to remove the “oddball readings”, but how you would determine the color.

    Here is how you would find the closest match.
  • MiddleSchoolerMiddleSchooler Posts: 10
    edited 2012-06-18 22:58
    Thank you again, I will incorporate your fantastic insight and help and then I will write my code! This all was very, very helpful. With such help, I am actually starting to understamd solving these puzzles more and more - well as a newbie, but with your help I am now a learning newbie! Thanks, Rory
  • kwinnkwinn Posts: 8,697
    edited 2012-06-19 19:37
    Always happy to give someone a push to get them started. I have corrected my mistake and combined the first two attachments along with a suggestion for calculating the percent difference and match. Once you understand this you should be able to add a routine to measure and store reference color samples instead of typing them in. I will delete the first two attachments once this is posted.
Sign In or Register to comment.