Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Re
Published

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.

BeginnerShowcase (no instructions)30 minutes30,823
RGB LED Button Controller

Things used in this project

Story

Read more

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).

Code

Whole code

Arduino
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]);
}

Credits

Re
1 project • 0 followers

Comments