Thursday, March 31, 2011

Lab 3

 Use Serial.Read to Control the Arduino

Serial.read() allows the user to input information in the serial monitor so that the Arduino can receive signals or commands.  Basically, Serial.read allows  the user to send characters from the input line of the serial monitor to the Arduino.

Specific case: Using Serial.read to play tones on a buzzer on the arduino.  The buzzer was connected to pin 8 on the micro-controller.  Only two tones were set with the characters 'a' and 'b.'  Character 'a' was set at a frequency of 200 Hz and played for 2000 milliseconds.  On the other hand, character 'b' was played at 600 Hz and lasted 1000 milliseconds.  Basically, characters can be set for a series of tones and any song can be played as desired.  This would be especially useful if the letters represented the actual notes on a standard keyboard. Basically, just the frequencies of the notes have to be looked up for each character.  The code can be seen below:


Code:

int inputChar;

void setup()
{
  Serial.begin(9600);
  pinMode(8, OUTPUT);
}





void loop() {
  int inputChar;
  if (Serial.available()>0){
    inputChar = Serial.read();
    if (inputChar == 'a') {
      tone(8,200,2000);
    }
    if (inputChar == 'b') {
     tone(8,600,1000);
    }
  }
}


ColorPAL

First, we downloaded the example code for ColorPAL on blackboard to understand how the sensor functioned:

We noticed that the serial monitor read a specific color with 3 different number values for red, green, and blue.  A higher number for any of these specific values meant that there was more of that color.  For instance, a purely red object would have a high red value and low green and blue values.  Additionally, black objects would have very low values for all 3 numbers in the set while white had very high (300+) values.  After testing the device, we created a black and white reference in order to calibrate the ColorPAL.  We recorded the set of 3 values that the sensor read when putting a black sheet of paper directly in front of the sensor.  We named these values Kb, Kg, and Kr to designate the blue, green, and red reference values for the "black" background.  Similarly, we did the same for a "white" background and named these values Wb, Wg, and Wr.  After initializing these values, we inserted them into a code provided in the lab manual, which calibrated the ColorPAL for each color.  For instance, the green color was calibrated as follows:

Cg=(grn-Kg)/(Wg-Kg)

where
grn= green value read by device
Kg=black background value for green
Wg= white background value for green
Cg= final calibrated value for green

This procedure was repeated for the other two colors (blue and red).

The entire code is shown below:

/*====================================================
 / 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

float red;
float grn;
float blu;
float Kr=6;
float Kb=8;
float Kg=6;
float Wr=312;
float Wg=315;
float Wb=420;
float Cr;
float Cg;
float Cb;


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");
   Cr=((float)red-(float)Kr)/((float)Wr-(float)Kr);

  Serial.print(Cr);
  Serial.print("   G");
  Cg=(grn-Kg)/(Wg-Kg);
  Serial.print(Cg);
  Serial.print("   B");
  Cb=(blu-Kb)/(Wb-Kb);
  Serial.println(Cb);
  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);
 

 

    }
  }
}

No comments:

Post a Comment