If you recall, Winslow used the Serial.read() command before in Lab 2 to control our LED brightness...
int pin = 5;
int input = 0;
int bright = 1;
int counter = 1;
void setup(){
Serial.begin(9600);
Serial.println("start");
pinMode(pin,OUTPUT);
}
void loop(){
if(Serial.available()>0){
input = Serial.read();
Serial.print("Brightness Level ");
if(input == '1'){
Serial.println("1");
bright = 10;
}
else if(input == '2'){
Serial.println("2");
bright = 20;
}
else if(input == '3'){
Serial.println("3");
bright = 40;
}
else if(input == '4'){
Serial.println("4");
bright = 80;
}
else if(input == '5'){
Serial.println("5");
bright = 160;
}
else if(input == '6'){
Serial.println("6");
bright = 320;
}
else if(input == '7'){
Serial.println("7");
bright = 640;
}
else if(input == '8'){
Serial.println("8");
bright = 1280;
}
else if(input == '9'){
Serial.println("9");
bright = 2560;
}
else{
Serial.println("undefined");
}
}
if(bright>=counter){
digitalWrite(pin,HIGH);
}
if(counter>bright){
digitalWrite(pin,LOW);
}
if(counter>2600){
counter = 1;
}
counter++;
}
And he also used this to control the Arduino in the custom sounds part of the last lab. Hooray!
COLORPAL
#include <SoftwareSerial.h>
SoftwareSerial Color90(2, 3); // rx = 2, tx = 3
int red;
int grn;
int blu;
float red_corrected;
float grn_corrected;
float blu_corrected;
int Kr = 10;
int Kg = 6;
int Kb = 14;
int Wr = 244;
int Wg = 172;
int Wb = 348;
char objectColor;
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(red);
Serial.print(" G");
Serial.print(grn);
Serial.print(" B");
Serial.println(blu);
Serial.print("Rc");
Serial.print(red_corrected);
Serial.print(" Gc");
Serial.print(grn_corrected);
Serial.print(" Bc");
Serial.println(blu_corrected);
Serial.println(objectColor);
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);
red_corrected = (float)255*(red-Kr)/(Wr-Kr);
dummy[0] = rByte[3];
dummy[1] = rByte[4];
dummy[2] = rByte[5];
grn = strtol(dummy,NULL,16);
grn_corrected = (float)255*(grn-Kg)/(Wg-Kg);
dummy[0] = rByte[6];
dummy[1] = rByte[7];
dummy[2] = rByte[8];
blu = strtol(dummy,NULL,16);
blu_corrected = (float)255*(blu-Kb)/(Wb-Kb);
if(red_corrected > 25+grn_corrected && red_corrected > 25+blu_corrected){
objectColor = 'R';
}
else if(grn_corrected > 25+red_corrected && grn_corrected > 25+blu_corrected){
objectColor = 'G';
}
else if(blu_corrected > 25+grn_corrected && blu_corrected > 25+red_corrected){
objectColor = 'B';
}
else if(red_corrected > 240 && grn_corrected > 240 && blu_corrected > 240){
objectColor = 'W';
}
else if(red_corrected < 15 && grn_corrected < 15 && blu_corrected < 15){
objectColor = 'K';
}
else{
objectColor = '?';
}
}
}
}
No comments:
Post a Comment