Friday, March 26, 2010

March Madness Challenge - Day 26

Took me a bit to get a strategy that passed most tests. I'll have to look at this one again in the morning. It has some nice features, and some mystery features. Using seriality API I could use QUnit to test the string parsing. Ok, so what this thing does is take a string of 3 numbers separated by commas, and feed it over the serial port to the Arduino. The code looks for commas and then asks is this the first, or second comma. There is no third comma so it simply checks nn value to say is this the third value. If so it sets it.

It only set's the values if it understands and leaves the values with the previous value. This should be a nice feature if used with sliders because there would be no off and on flickering it just stays on at the last combination it had.

This program also just echoes back the setting sent to it. Nice thing to look for for debugging. This is also a tool to be used for testing since, I know what went in should match what come out.

Finally, I added a map function that reverses HIGH, LOW and makes to analog numbers feel more natural. Also, I need to find a way to test that using the map function the way I'm using it really results in balance colors.


/*
* Day 26 RGB calibrated fading code with serial controls
*/
char str[11];

int redPin = 9; // LED connected to digital pin 9
int greenPin = 11;
int bluePin = 10;
int pins[] =
{
redPin, greenPin, bluePin
};

int photoPin = 0;
int photoVal = -1;
int rr, gg, bb = -1;
int minmax, rmax, gmax, bmax = 0;

void setup() {
Serial.begin(9600);
Serial.println("Start");

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);

digitalWrite(redPin, LOW);
delay(500);
rmax = analogRead(photoPin);
digitalWrite(redPin, HIGH);

digitalWrite(greenPin, LOW);
delay(500);
gmax = analogRead(photoPin);
digitalWrite(greenPin, HIGH);

digitalWrite(bluePin, LOW);
delay(500);
bmax = analogRead(photoPin);
digitalWrite(bluePin, HIGH);

sendMax();
minmax = min(rmax, min(gmax, bmax));
setRGB(255, 255, 255);
}

void setRGB(int red, int green, int blue)
{
red = map(red, 0, minmax, 255, 0);
green = map(green, 0, minmax, 255, 0);
blue = map(blue, 0, minmax, 255, 0);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

void sendRGB(int red, int green, int blue)
{
Serial.print(" { \"red\" : ");
Serial.print(red);
Serial.print(", \"green\" : ");
Serial.print(green);
Serial.print(", \"blue\" : ");
Serial.print(blue);
Serial.println("}");
}

void sendMax()
{

Serial.print(" { \"rmax\" : ");
Serial.print(rmax);
Serial.print(", \"gmax\" : ");
Serial.print(gmax);
Serial.print(", \"bmax\" : ");
Serial.print(bmax);
Serial.println("}");
}



void loop()
{
int ii = 0;
int nn = 0;
char num[2];
int rgb = 0;
boolean keepgoing = true;

while (keepgoing)
{
while (Serial.available() )
{
{
str[ii] = Serial.read();
num[nn] = str[ii];
//Echo the character as you type
//Serial.write(str[ii]);

if (str[ii] == 44)
{
if (rgb == 0)
{
rr = atoi(num);
nn = -1;
rgb++;
}
else if (rgb == 1)
{
gg = atoi(num);
nn = -1;
rgb++;
}
}
if (str[ii] == 0x0d )
{
keepgoing = false;
if (rgb == 2)
{
bb = atoi(num);
nn = -1;
rgb = 0;
}
sendRGB(rr,gg, bb);
setRGB(rr, gg, bb);
}
ii++;
nn++;
}
}
}
str[ii] = 0;
Serial.println();
Serial.println(str);
setRGB(rr, gg, bb);

}




No comments: