The first portion of the lab, character reading, utilized the textbox on the serial monitor to enable the user to interact with the program. The backbone of this functionality is below.
inputChar = Serial.read();
The circuit used for this operation can be seen below.
What we were able to do with this program is to output audio whose frequency depended on the input. The way that we did this was to note that characters and numbers entered into the serial monitor had attached a numerical value. For example, the letter 'a' is equivalent to the number 95.
frequency = inputChar * 10;
tone(pin,frequency);
This coding enabled us to produce audio based solely on the input commands that we gave.
The second major part of the lab was reading colors with the ColorPAL device. This device can be seen below (From Dr. Bogen's lab manual).
The first task that we undertook with this device was simply implementing the read_colorPAL code that was available to the students. After putting together the circuit, we tested the device out by reading the colors of various objects in the lab. When we ran the program, the serial monitor outputted a dynamic 1x3 matrix that read the red, green, and blue values at that moment. The values seemed to run the gamut from 1 to over 300. Interestingly, black did not give a 0 value for either red, green, or blue.
The next task that we undertook with this device was calibrate the output values so that black would give a value of 0 for red, green, and blue. We also wanted the numbers to be percentages rather than raw numbers. In this way, we also wanted a white object to output a value of 100 for red, green, and blue. The way that we did this was to find the red, green, and blue readings for both white and black objects. We then implemented the formulas found in the lab manual to modify the red, green, and blue variables. These formulas can be see below.
red = 100*(red - Kr)/(Wr - Kr);
grn = 100*(grn - Kg)/(Wg - Kg);blu = 100*(blu - Kb)/(Wb - Kb);
With these changes, a black object gave RGB values very close to zero, and a white object gave RGB values very close to one.
The final task that we undertook with this ColorPAL device was to modify our program so it detected and classified objects as either red, green, blue, white, or black. The way that we did this was first to observe the RGB values that control red, green, blue, white, and black objects yielded. We then implemented five "if" and "else if" statements so that the serial monitor outputted the color of objects in front it. The coding statements can be seen below.
int RG = abs(red/grn);
int RB = abs(red/blu);
int GR = abs(grn/red);
int GB = abs(grn/blu);
int BR = abs(blu/red);
int BG = abs(blu/grn);
int difference = 100-red;
if(RG>2 && RB>2 && difference>30)
{
Serial.println("This is RED!!");
}
else if(GR>2 && GB>2 && difference>30)
{
Serial.println("This is GREEN!!");
}
else if(BR>2 && BG>2 && difference>30)
{
Serial.println("This is BLUE!!");
}
else if(difference<20)
{
Serial.println("This is WHITE!!");
}
else if(difference>80)
{
Serial.println("This is BLACK!!");
}
delay(100);
The serial monitor as a result of these alterations can be seen below. At this point, a blue object was in front of the ColorPAL device.