Hello all! Thanks to anyone with some input!
Trying to make device that fades between two different LEDs controlled with a pot.
Everything is working nearly as expected, but I've run into a snag. I'd like the one of the LEDs to be as bright as possible during the crossfade. Right now if I'm in the middle of the crossfade than the LEDs are at half brightness instead of both being at full brightness.
Hopefully this all makes sense, I'll try to be more clear if not.
Serial output:
EX:255.00 R:127 B:128
// INPUT: Potentiometer should be connected to 5V and GND
int potWB = A0; // Potentiometer output connected to analog pin 0
float wbVal = 0; // Variable to store the input from the potentiometer
int potEX = A3; // Potentiometer output connected to analog pin 3
float exVal = 0; // Variable to store the input from the potentiometer
// PWM OUTPUT
int redPin = 11; // Red LED, connected to digital pin 9
int bluPin = 10; // Blue LED, connected to digital pin 11
// Program variables
float redVal = 0; // variables to store the values to send to the pins
float bluVal = 0;
int redEx = 0;
int bluEx = 0;
int DEBUG = 1; // set to 1 to turn on debugging output
extern const uint8_t gamma8[]; // led gamma correction table
void setup() {
pinMode(redPin, OUTPUT); // set pins as output
pinMode(bluPin, OUTPUT);
if (DEBUG) { // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput in 0004 format
}
}
void loop() {
//wbVal = analogRead(potPin);
//exVal = analogRead(potEX) / 4;
wbVal = smoothWB(); // smooth function for white balance
exVal = smoothEX() / 4; // smooth function for exposure compensation
wbVal = map(wbVal, 0, 1023, 0, exVal); // reduce high value
redVal = exVal - wbVal; // crossfade between red & blue
bluVal = wbVal;
redEx = constrain(redVal, 10, 255); // stop led from turning off
bluEx = constrain(bluVal, 10 , 255);
//redEx = pgm_read_byte(&gamma8[(int)redVal]); // led gamma correction test
//bluEx = pgm_read_byte(&gamma8[(int)bluVal]);
analogWrite(redPin, redEx); // write values to LED pins
analogWrite(bluPin, bluEx);
if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 100) // Print every hundred loops
{
DEBUG = 1; // Reset the counter
// Serial output using 0004-style functions
Serial.print("EX:"); // exposure compensation value
Serial.print(exVal); // print exposure value
Serial.print("\t"); // print a tab
Serial.print("R:"); // print for red and blu...
Serial.print(redEx);
Serial.print("\t");
Serial.print("B:");
Serial.println(bluEx); // println, to end with a carriage return
}
}
}
int smoothWB(){
int i;
int value = 0;
int numReadings = 8;
for (i = 0; i < numReadings; i++){
// read white balance pot data.
value = value + analogRead(potWB);
// 1ms pause adds more stability between reads.
//delay(1);
}
// Take an average of all the readings.
value = value / numReadings;
// Scale to 8 bits (0 - 255).
//value = value / 4;
return value;
}
int smoothEX(){
int i;
int value = 0;
int numReadings = 8;
for (i = 0; i < numReadings; i++){
// read exposure pot data.
value = value + analogRead(potEX);
// 1ms pause adds more stability between reads.
//delay(1);
}
// Take an average of all the readings.
value = value / numReadings;
// Scale to 8 bits (0 - 255).
//value = value / 4;
return value;
}
const uint8_t PROGMEM gamma8[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255
};