RGB Color toggle using push button

Alright, so I'm new to the whole forum thing and the whole Arduino thing, but I'll keep it brief. If you want the full spill it's on Reddit: https://redd.it/5bxva6

So I'm trying to cycle colors on an RGB LED using a push button. I have no clue what I'm doing. I have turned a led on and off using buttons, but that's about it. I want it to cycle BLUE,GREEN,RED,YELLOW,CYAN,MAGENTA,WHITE when I press the button connected to pin 2. Here is what I've got:

// RGB LED lamp progress

//RGB LED pins
int ledDigitalOne[] = {8, 9, 10}; //the three digital pins of the digital LED 
                                   //9 = redPin, 10 = greenPin, 11 = bluePin
int sensorPin = A0; //Potentiometer to adjust brightness

const int colorbuttonPin = 2; //This is my button to change colors
const int patternbuttonPin = 3; //This is my button to cycle patterns
const boolean ON = LOW; 
                            //Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;   //Define off as HIGH

//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};    
const boolean GREEN[] = {OFF, ON, OFF}; 
const boolean BLUE[] = {OFF, OFF, ON}; 
const boolean YELLOW[] = {ON, ON, OFF}; 
const boolean CYAN[] = {OFF, ON, ON}; 
const boolean MAGENTA[] = {ON, OFF, ON}; 
const boolean WHITE[] = {ON, ON, ON}; 
const boolean BLACK[] = {OFF, OFF, OFF}; 

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};

void setup () {
   for(int i = 0; i < 3; i++){
   pinMode(ledDigitalOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
  pinMode(colorbuttonPin, INPUT);
  pinMode(patternbuttonPin, INPUT;
}


void loop() {
  sensorValue = analogRead(sensorPin); // reads the potentiometers value

// I want to run this after I press the patternbutton on Pin 3
void randomColor(){
  int rand = random(0, sizeof(COLORS) / 2);  //get a random number within the range of colors
  setColor(ledDigitalOne, COLORS[rand]);  //Set the color of led one to a random color
  delay(1000);
}
// Not quite sure how to make it conditional to when i toggle the button much less
// make it so I don't have to hold the button down. I want it to kinda be a toggle
// button, but not interfere with the color change button



// turning the RGB LED on will be in this area
delay (sensorValue); //This is for my Potentiometer brightness adjuster



//I think I have to turn the LED off here to make the potentiometer work as
// a brightness adjuster from what ive gathered
delay (sensorValue); //Potentiometer
}

Also a quick question. What does putton void before something do?
And I'm using a RGB LED from the ARDX Arduino kit

Any help would be appreciated thanks!

Welcome.
Attach the full sketch.
Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]
What LEDs are you using?

.

Keep a counter that increments with every button press (see the debounce example and the state change example that come with the IDE).

Every button press increments the counter (0, 1, 2, ... 7); when it reaches 8, you reset it to 0.

This counter acts as an index in the COLOR array.

Without the debounce / state change

void loop()
{
  static byte index = 0;

  if(digitalRead(colorButtonPin) == HIGH)
  {
    index++;

    if(index == 8)
    {
      index = 0;
    }
  }

  // you can access the color in the color array
  digitalWrite(ledDigitalOne[0], COLORS[index][0]);
  digitalWrite(ledDigitalOne[1], COLORS[index][1]);
  digitalWrite(ledDigitalOne[2], COLORS[index][2]);
}

Not compiled, not tested. Just to give you the idea. It's neater to change the last part to a small for loop; I did not do that as you miight initially not understand it (not sure).

Also a quick question. What does putton void before something do?

It tells the compiler that the function does not return a value.

Update: So I found out that I don't need to use the arduino to regulate brightness. And I've switched source code for the RGB:

int ledcolor = 0;
int a = 500; //this sets how long the stays one color for
int red = 8; //this sets the red led pin
int green = 9; //this sets the green led pin
int blue = 10; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 204);
delay(a);
analogWrite(red, 0);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, HIGH);
delay(a);
digitalWrite(green, LOW);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(blue, LOW);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 160);
digitalWrite(green, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(blue, LOW);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
}

}

also I have a on/off switch wired up, so its just a matter of getting the buttons up and running.

UPDATE:
After a lot of learning and work, I'm calling it finished. here's the code:

int ledcolor = 0;
int a = 500; //this sets how long the stays one color for
int red = 8; //this sets the red led pin
int green = 9; //this sets the green led pin
int blue = 10; //this sets the blue led pin
int cycleButton = 2; // cycle colors button pin
int buttonToggleCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() { //this sets the output pins
pinMode(cycleButton, INPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(9600);
}

void loop() {
  
   buttonState = digitalRead(cycleButton);
   
   if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonToggleCounter++;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
Serial.println(buttonToggleCounter);
 if (buttonToggleCounter == 1) {
    digitalWrite(red, LOW);
    digitalWrite(blue, HIGH);
    digitalWrite(green, HIGH);  
  } 
 if (buttonToggleCounter == 2) {
    digitalWrite(red, HIGH);
    digitalWrite(blue, HIGH);
    digitalWrite(green, LOW);
    
  } 
 if (buttonToggleCounter == 3) {
    digitalWrite(red, HIGH);
    digitalWrite(blue, LOW);
    digitalWrite(green, HIGH);
    
  } 
if (buttonToggleCounter == 4) {
    digitalWrite(red, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(green, HIGH);
    
  } 
if (buttonToggleCounter == 5) {
    digitalWrite(red, LOW);
    digitalWrite(blue, HIGH);
    digitalWrite(green, LOW);
    
  } 
if (buttonToggleCounter == 6) {
    digitalWrite(red, HIGH);
    digitalWrite(blue, LOW);
    digitalWrite(green, LOW);
    
  } 
  if (buttonToggleCounter == 7) {
    digitalWrite(red, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(green, LOW);
    
  }
if (buttonToggleCounter == 8) {
int ledcolor = random(7); //this randomly selects a number between 0 and 6
switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 204);
delay(a);
analogWrite(red, 0);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, HIGH);
delay(a);
digitalWrite(green, LOW);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(blue, LOW);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 160);
digitalWrite(green, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(blue, LOW);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
}
}

if (buttonToggleCounter == 9) {buttonToggleCounter = 0;
}

if (buttonToggleCounter == 0) {
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
}
}

The whole thing works on a on/off toggle switch and a brightness adjusting potentiometer. there is a toggle button to cycle colors and the last is random patterns. There is room for improvement like organized colors and also there is a bug where it is finnicky when cycling back through. Other than that, super satisfied! It was a great experience, and I would do it over again if I had the choice. Thanks Everyone!

1 Like

Hi,
Before you send your code , select TOOLs then Auto-Format, or just do a Ctrl-T.
This will setup you code indents and level out your { } pairs to make it easier to read.

Tom... :slight_smile:

Nerdinator:
Update: So I found out that I don't need to use the arduino to regulate brightness. And I've switched source code for the RGB:

...

...




also I have a on/off switch wired up, so its just a matter of getting the buttons up and running.

If you don't need an Arduino, why do you post code ? :smiley:

sterretje:
If you don't need an Arduino, why do you post code ? :smiley:

The code posted was the new RGB LED source code I was using for displaying random colors, NOT for adjusting the RGB LED's brightness.

Two quick questions:

  1. In the middle of my project using
digitalWrite (led, HIGH);

turned the led OFF instead of ON. Why? Is this a hardware thing or a software thing?
2. I have a small bug where the delay on the random function makes it hard to cycle back through the counter. It interferes with the button press detection. How can I fix this? I tried moving the delay around, but it screwed up the pattern. Here's my code:

int ledcolor = 0;
int a = 500; //this sets how long the stays one color for
int red = 8; //this sets the red led pin
int green = 9; //this sets the green led pin
int blue = 10; //this sets the blue led pin
int cycleButton = 2; // cycle colors button pin
int buttonToggleCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() { //this sets the output pins
  pinMode(cycleButton, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  buttonState = digitalRead(cycleButton);

  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonToggleCounter++;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  Serial.println(buttonToggleCounter);
  if (buttonToggleCounter == 1) {
    digitalWrite(red, LOW);
    digitalWrite(blue, HIGH);
    digitalWrite(green, HIGH);  
  } 
  if (buttonToggleCounter == 2) {
    digitalWrite(red, HIGH);
    digitalWrite(blue, HIGH);
    digitalWrite(green, LOW);

  } 
  if (buttonToggleCounter == 3) {
    digitalWrite(red, HIGH);
    digitalWrite(blue, LOW);
    digitalWrite(green, HIGH);

  } 
  if (buttonToggleCounter == 4) {
    digitalWrite(red, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(green, HIGH);

  } 
  if (buttonToggleCounter == 5) {
    digitalWrite(red, LOW);
    digitalWrite(blue, HIGH);
    digitalWrite(green, LOW);

  } 
  if (buttonToggleCounter == 6) {
    digitalWrite(red, HIGH);
    digitalWrite(blue, LOW);
    digitalWrite(green, LOW);

  } 
  if (buttonToggleCounter == 7) {
    digitalWrite(red, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(green, LOW);

  }
  if (buttonToggleCounter == 8) {
    int ledcolor = random(7); //this randomly selects a number between 0 and 6
    switch (ledcolor) {
    case 0: //if ledcolor equals 0 then the led will turn red
      analogWrite(red, 204);
      delay(a);
      analogWrite(red, 0);
      break;
    case 1: //if ledcolor equals 1 then the led will turn green
      digitalWrite(green, HIGH);
      delay(a);
      digitalWrite(green, LOW);
      break;
    case 2: //if ledcolor equals 2 then the led will turn blue
      digitalWrite(blue, HIGH);
      delay(a);
      digitalWrite(blue, LOW);
      break;
    case 3: //if ledcolor equals 3 then the led will turn yellow
      analogWrite(red, 160);
      digitalWrite(green, HIGH);
      delay(a);
      analogWrite(red, 0);
      digitalWrite(green, LOW);
      break;
    case 4: //if ledcolor equals 4 then the led will turn cyan
      analogWrite(red, 168);
      digitalWrite(blue, HIGH);
      delay(a);
      analogWrite(red, 0);
      digitalWrite(blue, LOW);
      break;
    case 5: //if ledcolor equals 5 then the led will turn magenta
      digitalWrite(green, HIGH);
      digitalWrite(blue, HIGH);
      delay(a);
      digitalWrite(green, LOW);
      digitalWrite(blue, LOW);
      break;
    case 6: //if ledcolor equals 6 then the led will turn white
      analogWrite(red, 100);
      digitalWrite(green, HIGH);
      digitalWrite(blue, HIGH);
      delay(a);
      analogWrite(red, 0);
      digitalWrite(green, LOW);
      digitalWrite(blue, LOW);
      break;
    }
  }

  if (buttonToggleCounter == 9) {
    buttonToggleCounter = 0;
  }

  if (buttonToggleCounter == 0) {
    digitalWrite(red, HIGH);
    digitalWrite(green, HIGH);
    digitalWrite(blue, HIGH);
  }
}

Thanks for the help!

If your o/p pin is connected to the anode of a led you need a HIGH to turn on the led.
If the cathode of a led is connected to the pin you need a LOW to turn the led on.

Using delay() in a sketch stops code execution for that amount of time.

.

LarryD:
If your o/p pin is connected to the anode of a led you need a HIGH to turn on the led.
If the cathode of a led is connected to the pin you need a LOW to turn the led on.

Using delay() in a sketch stops code execution for that amount of time.

.

My wiring is +5V going to a toggle switch, then to breadboard + and a potentiometer is connected the breadboard +. The RGB then has its common connected to the potentiometer, and the red, green, and blue have 560 ohm resistors on them going to pins 8, 9, 10. So how could I fix this?would connecting my common to ground make it so HIGH would turn it on?

Always best to draw a schematic of your circuit and show it to us.
A good picture of the wiring helps us see what you have.

.

Quite right. I have just spent five minuets trying to visualise what you had. And I failed.

BTW
digitalWrite (led, HIGH); if this turns off a led then you can define a variable to make things make more sense.
#define Off HIGH
#define On LOW

Now a digitalWrite (led,On); will turn the LED on.
Often flipping things in software can make up for hardware situations.

.

Hi,
Sorry guys, i should have asked back at post #6

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

ahhh.. I feel better now.. Tom... :slight_smile:

LarryD:
BTW
digitalWrite (led, HIGH); if this turns off a led then you can define a variable to make things make more sense.
#define Off HIGH
#define On LOW

Now a digitalWrite (led,On); will turn the LED on.
Often flipping things in software can make up for hardware situations.

.

Good point, the problem with that is that I'm planning on adding an analogWrite fading pattern, so reversing the definition of digitalWrite won't help then.

In that case, subtract the pwm value thàt you need from 255 and write that calculated value to the pwm pin.

TomGeorge:
Hi,
Sorry guys, i should have asked back at post #6

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

ahhh.. I feel better now.. Tom... :slight_smile:

https://circuits.io/circuits/3203850-rgb-lamp for the project. I found what it is I think. My RGB's common is the anode, not the cathode.

So now the conclusion is that is that I have a common anode RGB, so I'll need to rethink my program. I need some help with crossfading, as I'm only less than a week into this whole Arduino coding thing. I know I need to use analogWrite to crossfade as it gives you complete control over the RGB's values. I thought about making a few more counters and having it crossfade based off of them. It wouldn't be complicated but that is just a lot of work. Another option would be using math to crossfade it (which I have no idea how to do.) here is my sequence to crossfade it. (keep in mind I only want this to happen when I have my button counter at a certain stage:
0, 255, 255 - Red
0, 255 ,0 - Yellow
255, 255, 0 - Green
255, 0, 0 - Cyan
255, 0, 255 - Blue
0, 0, 255 - Magenta
0, 255, 255 - Red