The following is a cobbled together version of a couple of different sketches to get to where I am now. The code is not complete for two reasons:
(1) I'm not sure I can do what I want to do
and
(2) I don't think is as clean as it could be.
I'm using a tri-color RGB LED. I want to use two buttons (inc/dec) to control the brightness level of each LED, total of 6 buttons. Basically the idea is to mix the outputs to get different colors on the fly. The code uses a Case statement to evaluate a counter. The part that I'm not sure about is if I can duplicate the red LED code two more times and replace red with green and blue. I defined all of the variables but I haven't added the green and blue code yet.
I think this could be done cleaner. The Case statement limits me to 5 steps of brightness for each LED and I think that is too coarse. Increasing the number of steps makes the code clunkier but maybe it only seems that way because I haven't written in C++ before.
I thought about using pots to vary each channel but that would make the assembly bulkier than I would like.
Secondary question. Can I drive three of the tri-color LED's in parallel with one Arduino output for each channel? By channel I mean color channel, red, green, and blue.
/* PWM Control of an RGB LED. Change RGB values for each LED with buttons
*/
//Constants
const int RedLED = 11; //Red LED
const int GreenLED = 10; //Green LED
const int BLUELED = 9; //Blue LED
const int Redinc = 2; //Increase button for red LED value
const int Reddec = 3; //Decrease button for red LED value
const int Greeninc= 4; //Increase button for green LED value
const int Greendec = 5; //Decrease button for green LED value
const int Blueinc = 6; //Increase button for blue LED value
const int Bluedec = 7; //Decrease button for blue LED value
//Variables
int lastctrRed = 3;
int ctrRed; //Red LED counter
int incRed; //Red LED value increase button reading
int decRed; //Red LED value decrease button reading
int valRed; //Output value sent to the Red LED
int lastctrGreen = 3;
int ctrGreen; //Green LED counter
int incGreen; //Green LED value increase button reading
int decGreen; //Green LED value decrease button reading
int valGreen; //Output value sent to the Green LED
int lastctrBlue = 3;
int ctrBlue; //Blue LED counter
int incBlue; //Blue LED value increase button reading
int decBlue; //Blue LED value decrease button reading
int valBlue; //Output value sent to the Blue LED
void setup()
{
pinMode(Redinc, INPUT);
pinMode(Reddec, INPUT);
pinMode(Greeninc, INPUT);
pinMode(Greendec, INPUT);
pinMode(Blueinc, INPUT);
pinMode(Bluedec, INPUT);
Serial.begin(9600);
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BLUELED, OUTPUT);
}
void loop(){
ctrRed = lastctrRed; //Red counter
incRed = digitalRead(Redinc); //Red button pins
decRed = digitalRead(Reddec);
if (incRed == HIGH) { //if the Red inc button is pressed
ctrRed = ctrRed++; //counter increases by one
Serial.println(" Red ctr");
Serial.println(ctrRed);
}
if (decRed == HIGH) { //if the red dec button is pressed
ctrRed = ctrRed--; //counter decreases by one
Serial.println("Red ctr");
Serial.println(ctrRed);
}
if (ctrRed > 5){ //limit range of counter to 5 on the high end
ctrRed = 5;
}
if (ctrRed < 2){ //limit range of counter to 1 on the low end
ctrRed = 1;
}
switch (ctrRed){ //read the counter and set the output to the red LED
case 1:
valRed = 50;
break;
case 2:
valRed = 100;
break;
case 3:
valRed = 150;
break;
case 4:
valRed = 200;
break;
case 5:
valRed = 250;
break;
}
analogWrite(RedLED, valRed);
analogWrite(GreenLED, valGreen);
analogWrite(BLUELED, valBlue);
lastctrRed = ctrRed;
delay(500);