Wednesday, May 9, 2012

Belated ColorPal Post

//Dr. Bogen, sorry about not having this on by the 5th, we were looking through and realized it wasn't on.  This was the version that I found, just wanted to make sure you knew we'd done it! Thanks, Jason and Theresa

/*====================================================
 / 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 <OldSoftwareSerial.h>
OldSoftwareSerial Color90(2, 3);  // rx = 2, tx = 3

float red; 
float grn; 
float blu; 
float wr = 240;
float wg = 191;
float wb = 318;
float br = 22;
float bg = 21;
float bb = 33;

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();
  float R = (red-br)/(wr-br);
  float G = (grn-bg)/(wg-bg);
  float B = (blu-bb)/(wb-bb);
  Serial.print("R");
  Serial.print(R);
  Serial.print("   G");
  Serial.print(G);
  Serial.print("   B");
  Serial.println(B);
  
  if (R < .15 && G < .15 && B < .15){
    Serial.print("Black");
    delay(50);
  }
  if((R > .85 && G > .85 && B > .85){
    Serial.print("White");
    delay(50);
  }
  if((R>.15 && R <.85) && G<.15 && B <.15){
  Serial.print("Red");
  delay(50);
  }
  if((B>.15 && B <.85) && R<.15 && B <.15){
  Serial.print("Blue");
  delay(50);
  }
  if((G>.15 && G <.85) && R<.15 && B <.15){
  Serial.print("Green");
  delay(50);
  }
  if((G>.15 && G <.85) &&(R>.15 && R <.85) && B <.15){
  Serial.print("Yellow");
  delay(50);
  }
  if((G>.15 && G <.85) &&(B>.15 && B <.85) && R <.15){
  Serial.print("Cyan");
  delay(50);
  }
  if((R>.15 && R <.85) &&(B>.15 && B <.85) && G <.15){
  Serial.print("Magenta");
  delay(50);
  }
  gotcolor = 0;
  delay(50);
}

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); 
    }
  }
}


  

No comments:

Post a Comment