I've been using this sensor with a Diecimila and come to the same conclusion: the sample code provided by Atlas doesn't work. My current theory is that it's a timing issue with SoftwareSerial; there's data coming in but it's not ASCII.
I have the sensor working using the hardware Serial, but that means that you can't simultaneously communicate with the PC. For some applications that's fine. Code for this:
//Connections:
//Sensor green wire (sensor RX) to pin 0 (board RX)
//Sensor white wire (sensor TX) to pin 1 (board TX)
char sensorstring[30];
void setup() {
Serial.begin(38400);
strcpy(sensorstring, "");
pinMode(13, OUTPUT); //LED
}
void loop() {
while (Serial.available()) {
//read from the sensor and watch out for a carriage return indicating
//the end of the reading
char inchar = (char)Serial.read();
strncat(sensorstring, (char *) &inchar, 1);
if (inchar == '\r') {
//Sensor format: something like RR,GGG,B\r or RRR,GGG,BBB,*\r
//i.e. we don't know in advance how many ASCII characters are in
//each reading, or even how many commas.
//Assumption: the sensor is in its default mode, providing continuous
//readings in mode 1
char rstring[4];
char *rstart = strtok(sensorstring, ",");
strncpy(rstring, rstart, 4);
int red = atoi(rstring);
char gstring[4];
char *gstart = strtok(NULL, ",");
strncpy(gstring, gstart, 4);
int green = atoi(gstring);
char bstring[4];
char *bstart = strtok(NULL, ",\r");
strncpy(bstring, bstart, 4);
int blue = atoi(bstring);
// now do something interesting with the data
strcpy(sensorstring, "");
//flash the LED to show success
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
}
}
Thanks phil88 for that trick: the empty sketch allows the sensor data to pass straight through the Arduino to the PC so you can check that the sensor is working and responding to commands. Note that RX and TX have to be swapped while you do this: i.e. white wire on pin 0, green wire on pin 1. Like phil88, I found that the Serial Monitor doesn't reproduce the carriage return regardless of what you set in the drop-down, but it is being sent.