Shop OBEX P1 Docs P2 Docs Learn Events
ColorPal doubt — Parallax Forums

ColorPal doubt

fbozzofbozzo Posts: 6
edited 2011-12-31 06:35 in Accessories
Hi everyone, am I new to the forum and I have a doubt about ColorPal sensor. After reading ColorPal´s datasheet I understand that the following command will perform a RGB reading (subtracting ambient light):


SEROUT sio, baud, ["= (00 $ m) !"]


But I don´t understand how should I receive data from sensor. Should I wait for character $ to arrive and then data? How many bytes will the sensor send to the microcontroler?


I will be pleased if you help me.


Regards

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-26 14:23
    fbozzo,

    Welcome to the Parallax forum! Let's break that line down into its component parts:

    = Begin a program sequence.

    (00 Start an infinite (since the count is zero) loop.

    $ Send a dollar sign character. Any character that's not a command is sent as-is.

    m Perform a multi-color acquisition, subtract the ambient light reading form each component, and transmit the data as nine hex digits in the order RRRGGGBBB

    ) End the loop.

    ! Begin execution of the program sequence. This starts the loop, which outputs $RRRGGGBBB$RRRGGGBBB$RRRGGGBBB... continuously.

    So, to answer your question, you wait for a dollar sign ($), then read three 3-digit hexadecimal numbers.

    -Phil
  • fbozzofbozzo Posts: 6
    edited 2011-11-26 14:49
    Phill, thanks for your repply. It is much more clear now. Just another question: I want to perform only one read, should I use the following command?:


    SEROUT sio, baud, ["= $ m !"] or


    SEROUT sio, baud, ["= (01 $ m) !"]


    I think the first one is more convenient because a one-time loop has not sense. But am I not sure about syntax.


    Regards,
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-26 15:07
    Either would work but, as you say, the first makes more sense.

    -Phil
  • fbozzofbozzo Posts: 6
    edited 2011-11-26 15:11
    Thanks for your help!!!!. I will test it and let you know the results.


    Regards,
  • fbozzofbozzo Posts: 6
    edited 2011-11-29 14:25
    I have tested it and I can send and receive data from sensor. Is there a way to tranform de 3-digit hexadecimal numbers into a color component that ranges from 0 to 255? I have read this post and I beleive it is posiblle, but I don´t know how to do it:


    http://forums.parallax.com/showthread.php?118435-ColorPAL-Object-and-ColorPAL-VGA-Demo&highlight=colorpal


    Regards,
  • fbozzofbozzo Posts: 6
    edited 2011-12-05 04:18
    Any ideas? Perhaps what I want to do it is impossible

    Regards,
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-12-05 12:25
    Sorry, I missed your other post. To do what you want, you need to do a "white balance." This entails sampling a white target and computing a scaling factor for each color component, such that dimmer colors will produce a value in the range 0 - 255 when multiplied by the scaling factor. Here's a program that does just that:
    ' =========================================================================
    '
    '   File...... ColorPAL_sense_wh_bal.bs2
    '   Purpose... Demonstrate white balance for ColorPAL
    '   Author.... Phil Pilgrim -- Bueno Systems, Inc.
    '   Started... 4 Dec 2011
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    ' -----[ Program Description ]---------------------------------------------
    
    ' This program drives the Parallax ColorPAL color sensor and provides
    ' white-balanced color data to the DEBUG terminal.
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    sio     PIN 15           ' Serial I/O com pin for ColorPAL.
    
    ' -----[ 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
    
    ' -----[ Variables ]-------------------------------------------------------
    
    red     VAR Word          ' Received RGB values from ColorPAL.
    grn     VAR Word
    blu     VAR Word
    
    sred    VAR Word          ' White balance scaling factors.
    sgrn    VAR Word
    sblu    VAR Word
    
    char    VAR Byte
    i       VAR Nib
    
    
    ' -----[ Initialization ]--------------------------------------------------
    
    GOSUB Reset               ' Reset the ColorPAL and enter direct command mode.
    
    ' -----[ Program Code ]----------------------------------------------------
    
    SEROUT sio, baud, ["= (00 $ m) !"]  'Program ColorPAL to send $ then color data.
    
    DEBUG "Place the ColorPAL on a white target, click in the box above, and press Enter.", 13
    DO : DEBUGIN char : LOOP UNTIL char = 13
    
    GOSUB ReadRGB             ' Read the white sample.
    FOR i = 0 TO 2            ' Compute scaling factors.
      sred(i) = $ff80 / red(i)
    NEXT
    
    DEBUG "Ready to sample other colors.", 13
    
    DO
      GOSUB ReadRGB           ' Read the color components.
      FOR i = 0 TO 2          ' Scale to white values and saturate to 255 if necessary.
        red(i) = red(i) */ sred(i) MAX 255
      NEXT
      DEBUG "R", DEC3 red, " G", DEC3 grn, " B", DEC3 blu, CR ' Output it.
    LOOP
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    ' ReadRGB: Get RGB response from ColorPAL
    
    ReadRGB:
    
      SERIN sio, baud, [WAIT("$"), HEX3 red, HEX3 grn, HEX3 blu] ' Receive RGB data back.
      RETURN
    
    ' 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
    

    -Phil
  • fbozzofbozzo Posts: 6
    edited 2011-12-31 06:35
    Thanks Phil, have a happy new year!!!!!

    Regards,
Sign In or Register to comment.