ColorPal Sensor

Hi, I am trying to get readings from two ColorPal sensors connected to an Arduino Uno microcontroller. I modified the Arduino example code uploaded by Parallax but so far I am unable to get readings from the two sensors. The modified code is included below, I would appreciate very much to get help in order to get alternate readouts from each of them. Thanks, Alejandro.

/* ColorPal Sensor Example for Arduino
Author: Martin Heermance, with some assistance from Gordon McComb
This program drives the Parallax ColorPAL color sensor and provides
serial RGB data in a format compatible with the PC-hosted
TCS230_ColorPAL_match.exe color matching program.
*/

#include <SoftwareSerial.h>

const int sio1 = 2; // ColorPAL connected to pin 2
const int unused1 = 254; // Non-existant pin # for SoftwareSerial
const int sioBaud1 = 4800;
const int waitDelay1 = 200;

// Received RGB values from ColorPAL
int red1;
int grn1;
int blu1;


const int sio2 = 8; // ColorPAL connected to pin 8
const int unused2 = 255; // Non-existant pin # for SoftwareSerial
const int sioBaud2 = 4800;
const int waitDelay2 = 200;

// Received RGB values from ColorPAL
int red2;
int grn2;
int blu2;



// Set up two software serials on the same pin.
SoftwareSerial serin1(sio1, unused1);
SoftwareSerial serout1(unused1, sio1);


SoftwareSerial serin2(sio2, unused2);
SoftwareSerial serout2(unused2, sio2);


void setup() {
Serial.begin(9600);
reset1(); // Send reset to ColorPal
serout1.begin(sioBaud1);
pinMode(sio1, OUTPUT);
serout1.print("= (00 $ m) !"); // Loop print values, see ColorPAL documentation
serout1.end(); // Discontinue serial port for transmitting
serin1.begin(sioBaud1); // Set up serial port for receiving
pinMode(sio1, INPUT);

reset2();
serout2.begin(sioBaud2);
pinMode(sio2, OUTPUT);
serout2.print("= (00 $ m) !"); // Loop print values, see ColorPAL documentation
serout2.end(); // Discontinue serial port for transmitting
serin2.begin(sioBaud2); // Set up serial port for receiving
pinMode(sio2, INPUT);

}

void loop() {
readData1(); readData2();
}

// Reset ColorPAL 1; see ColorPAL documentation for sequence
void reset1() {
delay(200);
pinMode(sio1, OUTPUT);
digitalWrite(sio1, LOW);
pinMode(sio1, INPUT);
while (digitalRead(sio1) != HIGH);
pinMode(sio1, OUTPUT);
digitalWrite(sio1, LOW);
delay(80);
pinMode(sio1, INPUT);
delay(waitDelay1);
}


// Reset ColorPAL 2; see ColorPAL documentation for sequence
void reset2() {
delay(200);
pinMode(sio2, OUTPUT);
digitalWrite(sio2, LOW);
pinMode(sio2, INPUT);
while (digitalRead(sio2) != HIGH);
pinMode(sio2, OUTPUT);
digitalWrite(sio2, LOW);
delay(80);
pinMode(sio2, INPUT);
delay(waitDelay2);
}




void readData1() {
char buffer[32];

if (serin1.available() > 0) {
// Wait for a $ character, then read three 3 digit hex numbers
buffer[0] = serin1.read();
if (buffer[0] == '$') {
for(int i = 0; i < 9; i++) {
while (serin1.available() == 0); // Wait for next input character
buffer = serin1.read();
if (buffer == '$') // Return early if $ character encountered
return;
}
parseAndPrint1(buffer);
delay(1000);
}
}
}



void readData2() {
char buffer1[32];

if (serin2.available() > 0) {
// Wait for a $ character, then read three 3 digit hex numbers
buffer1[1] = serin2.read();
if (buffer1[1] == '$') {
for(int i = 0; i < 9; i++) {
while (serin2.available() == 0); // Wait for next input character
buffer1 = serin2.read();
if (buffer1 == '$') // Return early if $ character encountered
return;
}
parseAndPrint2(buffer1);
delay(1000);
}
}
}



// Parse the hex data into integers
void parseAndPrint1(char * data) {
sscanf (data, "%3x%3x%3x", &red1, &grn1, &blu1);
char buffer[32];
sprintf(buffer, "R%4.4d G%4.4d B%4.4d", red1, grn1, blu1);
Serial.println(buffer);
}


// Parse the hex data into integers
void parseAndPrint2(char * data) {
sscanf (data, "%3x%3x%3x", &red2, &grn2, &blu2);
char buffer1[32];
sprintf(buffer1, "R%4.3d G%4.3d B%4.3d", red2, grn2, blu2);
Serial.println(buffer1);
}

Comments

  • 2 Comments sorted by Date Added Votes
  • How have you connected the ColorPAL to your Arduino? See this post for the correct way:

    http://forums.parallax.com/discussion/comment/890040/#Comment_890040

    -Phil
    “Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
  • Hi Phil, thanks for the reply, I am wiring the ColorPal based on the Parallax General Description document for this sensor. I am attaching the wiring snapshot taken from it (black to Gnd, Red to 5V, White to DP2 for ColorPal 1 and DP8 for ColorPal 2). In my case one sensor is connected to Pin2 for data transmission while the other is connected to Pin8 for similar purposes. Something I didn't mention before is that by using the code I uploaded yesterday I get readings from Pin8 but not from Pin2. Thanks again, Alejandro.
    1960 x 343 - 35K
Sign In or Register to comment.