Wednesday, March 31, 2010

March Madness Challenge - Day 31

Whew, made it. Tonights project adds three potentiometers to might brightness calibration technique. Turns out it's important to examine ones assumptions. I discovered that I had to map values in distinct steps to keep the numbers within the range of my minimum maximum. If I hadn't hooked up the potentiometers and then outputted the values to see what my eyes were experience it would have taken much longer to realize this issue. So now I can go back and fix some older code.

I'd like to do a manual calibration for brightness and compare it to the automatic version. Since the analogRead function yields a 10 bit value at 4.9mV per unit it's possible to calculate what resistor needs to be place on each color in order to calibrate them with physical resistors and then not need the Arduino to calculate the relative values.

http://www.youtube.com/watch?v=QmNnJd9drYs




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

int photoPin = 0;
int redPot = 1;
int greenPot = 2;
int bluePot = 3;

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) 
{
  //These values are the 10 bit values, they need to be mapped to minmax
    //For some reason some values were mapping negative
  red = map(red, 0, 1023, 0, minmax);
  green = map(green, 0, 1023, 0, minmax);
  blue = map(blue, 0, 1023, 0, minmax);
  sendRGB(red, green, 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);
  sendRGB(red, green, 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.print("}");
}

void sendMax()
{

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



void loop() 
{
  rr = analogRead(redPot);
  gg = analogRead(greenPot);
  bb = analogRead(bluePot);
  setRGB(rr, gg, bb);
  sendRGB(rr, gg, bb);
  Serial.print(" { \"minmax\" : ");
  Serial.print(minmax);
  Serial.println("}");
  
}

No comments: