v1ckery
Published © GPL3+

Control an RGB LED with one potentiometer!

Because turning a knob is more fun than buttons, right?

BeginnerFull instructions provided1,399
Control an RGB LED with one potentiometer!

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×1
Breadboard (generic)
Breadboard (generic)
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×3

Story

Read more

Schematics

Schematic

Code

The code

Arduino
int rPin = 6;
int gPin = 5;
int bPin = 3;
int buttonPin = 13;
int potPin = A0;

//an array holds the different modes
String modes[3] = {"blue" , "red" , "green"};
String currentMode;

int PWM;
int potVal;

int greenVal;
int blueVal;
int redVal;

bool greenState;
bool blueState;
bool redState;

int currentButtonState;
int oldButtonState;
int i = 0 ;


void setup() {

pinMode(rPin,OUTPUT);
pinMode(bPin,OUTPUT);
pinMode(gPin,OUTPUT);
pinMode(buttonPin,INPUT_PULLUP);
pinMode(potPin,INPUT);
}

void loop() {
  

//controls the current colour mode
currentButtonState = digitalRead(buttonPin);
if (currentButtonState == 1 && oldButtonState==0){
  i = i+1;
  delay(50);

//ensure the array loops around
  if(i == 3){
    i=0;
  }
}
oldButtonState = currentButtonState;


currentMode = modes[i];


potVal = analogRead(potPin);
PWM = (potVal * 255./1023.);

//potentiometer sens adjustment
if (PWM <= 20){
  PWM = 0;
}



if (currentMode == "red"){
  greenState=false;

   if(redState == false && PWM == redVal){
    redState = true;
   }
  
   if(redState==true){
    redVal = PWM;
    analogWrite(rPin, redVal);
   }

}


if (currentMode == "green"){
    blueState = false;
  
   if(greenState == false && PWM == greenVal){
    greenState = true;
   }
  
   if(greenState==true){
    greenVal = PWM;
    analogWrite(gPin, greenVal);
   }
}


if (currentMode == "blue"){
    redState = false;
  
    if(blueState == false && PWM == blueVal){
    blueState = true;
    }
  
   if(blueState==true){
    blueVal = PWM;
    analogWrite(bPin, blueVal);
    }
}


}

Credits

v1ckery
2 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.