
Wyjście czujnika podpinamy pod pin nr 6 Arduino. Komunikacja serial: 115200. Dane są wysyłane w tej postaci:
RRRR;GGGG;BBBB;
gdzie RRRR – czterocyfrowa wartość koloru czerwonego 0-1023. itd.
Kod programu dla Arduino:
/* 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>
#define sio 6 // ColorPAL connected to pin 6
#define unused 255 // Non-existant pin # for SoftwareSerial
#define sioBaud 4800
// Received RGB values from ColorPAL
int red;
int grn;
int blu;
// Set up two software serials on the same pin.
SoftwareSerial serin(sio, unused);
SoftwareSerial serout(unused, sio);
void setup() {
delay(2000);
Serial.begin(115200);
reset(); // Send reset to ColorPal
serout.begin(sioBaud);
pinMode(sio, OUTPUT);
serout.print("=(00 $ m)!"); // Loop print values, see ColorPAL documentation
serout.end(); // Discontinue serial port for transmitting
pinMode(sio, INPUT);
serin.begin(sioBaud); // Set up serial port for receiving
}
void loop() {
readData();
}
// Reset ColorPAL; see ColorPAL documentation for sequence
void reset() {
delay(200);
pinMode(sio, OUTPUT);
digitalWrite(sio, LOW);
pinMode(sio, INPUT);
while (digitalRead(sio) != HIGH);
pinMode(sio, OUTPUT);
digitalWrite(sio, LOW);
delay(80);
pinMode(sio, INPUT);
delay(200);
}
void readData() {
char buffer[32];
if (serin.available() > 0) {
// Wait for a $ character, then read three 3 digit hex numbers
buffer[0] = serin.read();
if (buffer[0] == '$') {
for(int i = 0; i < 9; i++) {
while (serin.available() == 0); // Wait for next input character
buffer[i] = serin.read();
if (buffer[i] == '$') // Return early if $ character encountered
return;
}
parseAndPrint(buffer);
delay(10);
}
}
}
// Parse the hex data into integers
void parseAndPrint(char * data) {
sscanf (data, "%3x%3x%3x", &red, &grn, &blu); // Pull the R, G, and B values from the data string
char buffer[48]; // create a buffer
sprintf(buffer, "%4.4d;%4.4d;%4.4d;", red, grn, blu); //print the values into a buffer as formatted integers
Serial.println(buffer);
}
Program Processing wyświetla okno z narysowanym prostokątem w kolorze takim, jak pochodzi z czujnika. Trzeba było zmienić współczynniki kolorów. Mimo to, nadal wymagana jest kalibracja, której jak na razie nie przeprowadziłem. Program próbuje nawiązać połączenie z pierwszym portem szeregowym dostępnym na komputerze. Jeżeli masz więcej portów, trzeba zmienić kod programu.
Kod dla processing:
/**
* Color Variables (Homage to Albers).
*
* This example creates variables for colors that may be referred to
* in the program by a name, rather than a number.
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
String myString = null;
int lf = 10;
int val, red, green, blue; // Data received from the serial port
color inside = color(204, 102, 0);
void setup()
{
size(400, 400);
noStroke();
background(0, 0, 0);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);
myPort.readStringUntil(lf);
}
void draw()
{
while (myPort.available() > 0) {
myString = myPort.readStringUntil(lf);
if (myString != null) {
//println(myString);
String[] list = split(myString, ';');
red = int(list[0])*2;
green = int(list[1])*2;
blue = int(list[2])/2;
background(0, 0, 0);
fill(255);
text(" red: "+red+"\n green: "+green+"\n blue: "+blue, 10, 20);
inside = color(red, green, blue);
fill(inside);
rect(100, 100, 200, 200);
}
}
}