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

    }
  }
}

Our Code for Using "Serial.read" in Lab 3

So as Vidya told you I would do in her previous post, today I will be recording our codes from Lab 3. This post contains the program we used in the Arduino, using the "Serial.read" function to create music. Instead of simply playing the tone we used in Lab 2, we created a program that would allow us to play the tune ourselves. We first defined each tone and then were able to command our board to play a tone by using the Serial Monitor and the "inputChar" function. The code is displayed below:

int pin = 5;
int inputChar;
unsigned int a = 440;
unsigned int b = 493.88;
unsigned int C = 523.25;
unsigned int Cs = 554.37;
unsigned int E = 659.26;
unsigned int D = 587.33;
void setup()
{
Serial.begin(9600);
pinMode(pin,OUTPUT);
}
void loop()
{
 inputChar = Serial.read();
 if (inputChar == 'a')
 {
   tone(pin,a);
 }
   if (inputChar == 's')
 {
   tone(pin,b);
 }
   if (inputChar == 'g')
 {
   tone(pin,E);
 }
     if (inputChar == 'r')
 {
   tone(pin,Cs);
 }
     if (inputChar == 'f')
 {
   tone(pin,D);
 }
       if (inputChar == 'd')
 {
   tone(pin,C);
 }
 
}

Lab 3 Circuit Pictures!

Well, now that Paul's stolen my thunder, I think that another introduction is kind of repetitious! But hello to everyone who's reading this blog =)

So, Lab 3 involved using the Serial.read() command to control the Arduino. Being as fond of music as we are, we created a musical keyboard to input characters to output different notes. Paul already shoddied posting the code, so as self-designated group photographer, I am just going to post the picture of our circuits on the Arduino. I'll try to make them more artistic as we become more regular in our blogging.


So essentially, we had the positive end of the buzzer (which is the sound emitter) connected to the digital pin and the negative end was then connected to the ground.

The second half of Lab 3 involved using the ColorPal setup to figure out the color of an object placed near it. The red, green, and blue LEDs flash in sequence and measuring the reflected light intensity for each of the three colors using a photodiode.




What is interesting about this circuit is that the ColorPal serves both as an input and output device. As such, the digital pins 2 and 3 are connected in the above diagram. The pin 2 is then connected to a 2K ohm resistor, which is then connected to the 5 V. The ColorPal also have three pins that are appropriately connected to the 5V, ground, and signal as necessary.

Well, look out for Paul's blog on the actual code and Fritzing diagrams for the circuits that we used!

Thursday, February 10, 2011

First Post!

Welcome to our blog! Sorry, Vid, but it looks like I get to make the first post. So to update you guys on what were doing right now...

Current Progress:
Finished Lab 2
Started Lab 3

Once we have some more progress with Lab 3 we will update all of you. Look forward to more posts!