Shop OBEX P1 Docs P2 Docs Learn Events
Receiving data from Parallax Color Sensor to Digital Pin on Arduino — Parallax Forums

Receiving data from Parallax Color Sensor to Digital Pin on Arduino

luriluri Posts: 1
edited 2013-02-28 13:30 in Accessories
http://forums.parallax.com/showthread.php/138612-ColorPAL-arduino-problem-gt-.-lt?highlight=colorpal

Equipment:
ColorPAL Sensor (Rev. A) 28380
Arduino Mega 2560
2x 1k Resistors (Only for the setup using pull up resistors to 5V)


ColorPal

Hi, I've been looking around the internet trying to get this ColorPAL color sensor working for some time (3 weeks-ish).

The following things below are the methods I've already tried:

-one using the one line communication digital pin method
-using two digital pins for RX and TX (pull up resistor done via pinMode)
-using the two digital pins setup with an external pull up resistor
-tried to use the built in UART pins on the Mega (pins 0 and 1 had problems since Mega by default will not let you upload your "sketch" when you're using those pins)
-tried to use another UART (Serial1, AKA pins 19 (RX) and 18 (TX)
-tried starting from scratch and tested basic serial communication TO the sensor by changing the LED color (successful, noted changing LED color)
-tried to run the program for the Colorpal that samples one color intensity ( s ) instead of ( m )

So what I do know is that I can successfully sending commands and put the color sensor into direct mode, but I have problems reading anything on the buffer using Serial.Available and in most of the sample code online, the problem is just detecting the miscellaneous character "$" that is used when putting the color sensor in the multi-color sampling mode i.e. when doing this line of code: serout.print("= (00 $ m) !");

Can anyone point me in the right direction?

Currently, I will use the sample code that was written by Martin H. as a starting point.
* ColorPal Sensor Example * Author.... Martin Heeermance based upon Phil Pilgrim's PBASIC example
 *
 * This program drives the Parallax ColorPAL color sensor and provides
 * serial RGB data to the PC-hosted TCS230_ColorPAL_match.exe color
 * matching program.
 */


#include <SoftwareSerial.h>


// I/O Pin definitions.
const int sio = 2;
const int unused = 3;
const int sioBaud = 4800;


// Received RGB values from ColorPAL.
int red;
int grn;
int blu;


// Set up two software serials on the same pin.
// This mimic PBASIC's serin and serout function.
// A bit non-standard for the Arduino.
SoftwareSerial serin(sio, unused);
SoftwareSerial serout(unused, sio);


// -----[ Initialization ]--------------------------------------------------
void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("ColorPal Demo start"); 
  
  // Reset the ColorPAL and enter direct command mode.
  reset();
  
  // Program ColorPAL to send $ then color data.
  serout.begin(sioBaud);
  pinMode(sio, OUTPUT);
  serout.print("= (00 $ m) !"); // buffer commmands, loop print $ and data end_loop now execute
  // serout is unused from this point forwards
  serout.end();


  // Now turn the sio pin into an input to read data from the color pal.
  serin.begin(sioBaud);
  pinMode(sio, INPUT);
}


// -----[ Program Code ]----------------------------------------------------
// SERIN sio, baud, [WAIT("$"), HEX3 red, HEX3 grn, HEX3 blu] ' Receive RGB data back.
void loop()
{
  readData();
}  


// -----[ Subroutines ]-----------------------------------------------------


// reset: Sends a long break to reset ColorPAL and enter direct command mode.
void reset()
{
  Serial.println("Reset ColorPal");
  pinMode(sio, OUTPUT);
  digitalWrite(sio, LOW);  // Pull sio low to eliminate any residual charge.
  pinMode(sio, INPUT);     // Return pin to input.
  while (digitalRead(sio) != HIGH); // Wait for pin to be pulled high by ColorPAL.
  pinMode(sio, OUTPUT);
  digitalWrite(sio, LOW);  // Pull pin low.
  delay(80);               // Keep low for 80ms to enter Direct mode.
  pinMode(sio, INPUT);     // Return pin to input.
  delay(10);               // Pause another 10ms
}


void readData()
{
  char buffer[32];


  // Wait for a $ and then read three 3 digit hex numbers
  buffer[0] = serin.read();
  if (buffer[0] == '$')
  {
    for(int i = 0; i < 9; i++)
    {
      buffer[i] = serin.read();
      
      // every so often the data terminates early.  If this happen return
      if (buffer[i] == '$')
        return;
    }
    parseAndPrint(buffer);
    delay(100); 
  }
}


void parseAndPrint(char * data)
{
  // parse the hex data into integers.
  sscanf (data, "%3x%3x%3x", &red, &grn, &blu);


  // format using the format expected by the windows program and output it.
  char buffer[32];
  sprintf(buffer, "R%4.4d G%4.4d B%4.4d", red, grn, blu);
  Serial.println(buffer);
}

IMAG0500.jpg



Here is what I am currently seeing when running the code above after opening Serial Monitor:

screen.jpg



As you can see there is no output of the parsed RGB values and I am sure it's because there is no data coming in on the line as well as the fact the code to read the buffer has that "IF" statement that waits for the "$" character to be read.

Any help soon would be greatly appreciated.

If you have any parts that need clarification please let me know in a post below.

Thank you very much!
1024 x 576 - 130K
1024 x 576 - 50K
Sign In or Register to comment.