Brand new, first sketch, is there a better way to do this

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);
 case 1:
  valRed = 50;
  break;
  
  case 2:
  valRed = 100;
  break;
  
  case 3:
  valRed = 150;
  break;

Or a simple multiplication.

True but I wouldn't mind getting rid of the Case statement all together and just use the counter value as the PWM output (0-255)

So get rid of the case, and put in a multiplication.

If I use multiplication the rate of increase will be higher than I want. The Case routine is too course. Counter++ if the button is down might work but I'm concerned about contact bouncing.

I may have picked a bad project to start with. I don't want to start with blinking an LED... This seemed like a challenge. Probably not much of one but better than blinking an LED.

What range do you want the red LED to operate between (looks like 50 to 250)? What intervals do you want between button presses? (say 20).

 if (incRed == HIGH) {                 //if the Red inc button is pressed
      ctrRed = ctrRed += 20;  
      if (ctrRed > 250) {
           ctrRed = 250;
      }    

     ... 

     analogWrite(RedLED, ctrRed);

...R

0-255. The Case statement is going to go away. I'm going to use hardware debouncing on the button and cut the number of buttons down to three. One for channel selection and the other two will be for up and down. I'm also going to implement code where I can either change the value by one with a single button press or hold it down and ramp the value up or down. I'm going to use these guys to display the status of each color channel (if they ever come in):

I'll have to start over but I'm good with that.

I got the idea from: Tutorial 10 for Arduino: Interrupts + Debouncing – JeremyBlum.com

If it's a challenge you want, try fading the LED on pin 13 up and down over a period of, say, 10 seconds.

The Case routine is too course

Of coarse it is. :wink:

AWOL:
If it's a challenge you want, try fading the LED on pin 13 up and down over a period of, say, 10 seconds.

The Case routine is too course

Of coarse it is. :wink:

Lol... I caught it here...

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.

Stupid computer 8)