Thursday, February 24, 2011

Our Code for Using ColorPal

When we began using the ColorPal we faced some challenges trying to compensate for the fact that the reading for black was non zero. After tweaking our code for a good portion of time and also remeasuring our black and white values, we were finally able to arrive at a code that allowed us to successfully sense what color (or more appropriately how many parts blue, red, and green) the object was. Below is our final code for the ColorPal device we used to detect color:

/*============================
========================
 / Connect ColorPAL SIG signal to Arduino pin 2 and 3
 / Add 2K pullup resistor from pins 2 & 3 to +5v
 / Baud Rate = 9600 kbps
 / Read signal on 9600 in HEX
 /====================================================*/

#include <SoftwareSerial.h>
SoftwareSerial Color90(2, 3);  // rx = 2, tx = 3

int red;
int grn;
int blu;
float redCorr;
float bluCorr;
float grnCorr;
int gotcolor = 0;
int letter;

void setup()
{
  Serial.begin(9600); // Start communication with serial port read value
  Color90.begin(4800); // Send signal to led to read value

  pinMode(2,INPUT); // serial pin out from color pal
  pinMode(3,INPUT); // from same serial pin, signal pulls up, sends, pulls down, reads
  digitalWrite(2,HIGH); // Enable the pull-up resistor
  digitalWrite(3,HIGH); // Enable the pull-up resistor

  pinMode(2,OUTPUT); // send signal out
  pinMode(3,OUTPUT);
  digitalWrite(2,LOW); // turn pin off so pin 3 can go high
  digitalWrite(3,LOW);

  pinMode(2,INPUT); // Input signal to print
  pinMode(3,INPUT);

  Serial.println("Pass 1");
//  delay(20);

  while( digitalRead(2) != HIGH || digitalRead(3) != HIGH ) {
    Serial.println("In the loop");
    delay(50);
  }

  Serial.println("Pass 2");

  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  delay(100);     // spec is 80, but not all ColorPAL units work with 80

  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  delay(100);

}

// This oscillates back and forth on one wire to turn off led, send signal,
// turn on led, read signal. very fast strobe read - arduino is not capable of
// one wire signal communication over digital ports, so this is a way around
// that over 2 wires communicating with 1 pin on the sensor.
//---------------------------------

void loop()
{
  readcolor();
  Serial.print("R");
  Serial.print(redCorr);
  Serial.print("   G");
  Serial.print(grnCorr);
  Serial.print("   B");
  Serial.println(bluCorr);
  gotcolor = 0;
  delay(100);

}
void readcolor() {  // Reads ColorPAL, putting results in the red,grn,blu variables

  char rByte[9];
  char dummy[4];

  delay(20);
  Color90.begin(4800);
 Color90.print("= (00 $ m) !");  // set up loop to continuously send color data
  pinMode(3,INPUT);
  gotcolor = 0;
  while (gotcolor == 0) {
    rByte[0] = Color90.read();
    if( rByte[0] == '$' ) {
      gotcolor = 1;
      for(int i=0; i<9; i++) {
        rByte[i] = Color90.read();
//        Serial.print(rByte[i]);
      }
      Serial.println("");
      dummy[0] = rByte[0];
      dummy[1] = rByte[1];
      dummy[2] = rByte[2];
      dummy[3] = 0;

      red = strtol(dummy,NULL,16);
     

      dummy[0] = rByte[3];
      dummy[1] = rByte[4];
      dummy[2] = rByte[5];

      grn = strtol(dummy,NULL,16);
    
     
      dummy[0] = rByte[6];
      dummy[1] = rByte[7];
      dummy[2] = rByte[8];

      blu = strtol(dummy,NULL,16);
     
      bluCorr= (((float)blu-37.)/(505.-37.));
      grnCorr= (((float)grn-20.)/(271.-20.));
      redCorr= (((float)red-24.)/(304.-24.));

    }
  }
}

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. It's hard to come by experienced people on this subject, but it seems like you know what you're talking about. Thank you.

    Please check out:
    Medisoft Program

    ReplyDelete