Project showcase
RGB LED Button Controller

RGB LED Button Controller

Extremely simple project, my first even. Four buttons. Three to choose RGB values, one to choose whether to subtract or to add.

  • 11,311 views
  • 1 comment
  • 16 respects

Components and supplies

About this project

Just wanted to check out how publishing something here was like.

(Editor's note: The code has a smart way of using arrays and it is worth studying)

Code

Whole codeArduino
All the code needed. Just make sure to update the pins pointing to the corresponding led/button colour, these being named btnsRGB & ledsRGB.
// Which pins points to the buttons, order of Red Green Blue
const int btnsRGB[3] = {5, 6, 7};

// Which pins points to the leds, order of Red Green Blue
const int ledsRGB[3] = {10, 9, 11};

// Button & variable to control wether to subtract or add to valueRGB
const int btnMode = 13;
bool modeAdd = true;

// To save current strength to display on RGB led
int valueRGB[3] = {0, 0, 0};

void setup() {
	Serial.begin(9600);

	// Initialize buttons
	for(int i = 0; i < 2; i++){
		pinMode(btnsRGB[i], INPUT);
		analogWrite(btnsRGB[i], 0);
	}

	// Initialize led outputs
	for(int i = 0; i < 2; i++){
		pinMode(ledsRGB[i], INPUT);
	}

	// Initialize mode button
	pinMode(btnMode, INPUT);
}

void loop() {
  	// If red button was pressed
  	if(digitalRead(btnsRGB[0]) == HIGH){
  		updateValue(0);
	}
	
	// If green button was pressed
	if(digitalRead(btnsRGB[1]) == HIGH){
		updateValue(1);
	}

	// If blue button was pressed
  	if(digitalRead(btnsRGB[2]) == HIGH){
  		updateValue(2);
	}

	// If switch mode button was pressed
	if(digitalRead(btnMode) == HIGH){
		if(modeAdd == true){
			modeAdd = false;
		}else{
			modeAdd = true;
		}

		// Ugly way to stop button from unintentional press
		delay(1000);
	}

	// no chill
	delay(30);
}


// Update valueRGB and write change
void updateValue(int colour){

	// Add or subtract depending on mode
	if(modeAdd == true){

		// Don't let the value be more than 255
		if(valueRGB[colour] >= 255){
			valueRGB[colour] = 255;
		} else {
			valueRGB[colour]++;
		}
  	}else{

  		// Don't let the value be less than 0
  		if(valueRGB[colour] <= 0){
  			valueRGB[colour] = 0;
  		} else {
  			valueRGB[colour]--;
  		}
  	}

  	// Actually update the value of corresponding pin
  	analogWrite(ledsRGB[colour], valueRGB[colour]);
}

Schematics

Circuit
The RGB led is of common cathode, meaning the long leg gets grounded. The 10K resistors by the buttons are used as pull-down resistors. Make sure the cables from the led gets put into PWM pins (the ones with a ~ in front of their number).
Michiddelkl2ktcofx98

Comments

Similar projects you might like

Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

  • 2,953 views
  • 8 comments
  • 24 respects

LED Emergency Lights using WS2812 RGB LED Module

Project tutorial by bigboystoys13

  • 10,030 views
  • 5 comments
  • 20 respects

Arduino RGB Color Mixer

Project tutorial by FIELDING

  • 3,195 views
  • 1 comment
  • 11 respects

Multicolor Knight Rider with RGB PL9823 LED + Arduino UNO

Project tutorial by Giovanni Gentile

  • 3,959 views
  • 0 comments
  • 10 respects

RGB LED Strips Controller

Project tutorial by Philippe Libioulle

  • 16,647 views
  • 2 comments
  • 18 respects
Add projectSign up / Login