LED Strip with rotary switch and pot

My end goal is to use the rotary switch to choose 1 of 4 (or 5 including off) colors. And use the Pot for brightness.

The switch choose red, green, blue or white and off in any of the unused switch settings.

The pot sets the brightness.

Its sort of working.
My problem is the strip sort of blinks. randomly. Maybe call it flickering.

When set to blue I can see the green LED flicker.. When set to green the blue LED flickers. on red theres a flicker of one of the other LEDs as well.

Can somebody check my code and give me some hints on a better way to do this?

Fritzing is also attached to this post.

I havent added the code for white or off yet.

int potPin= A0;  //Declare potPin to be analog pin A0
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int colorpinvalue; //which switch position was selected
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define BLUEswitch A1
#define REDswitch A2
#define GREENswitch A3
  
void setup() {
  pinMode(potPin, INPUT);  //set potPin to be an input
  Serial.begin(9600);      // turn on Serial Port
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
 
void loop() {
 readValue = analogRead(potPin);  //Read the voltage on the Potentiometer
 writeValue = (255./1023.) * readValue; //Calculate Write Value for LED
  if (analogRead(BLUEswitch) == 1023){
    analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, writeValue);      //Write to the LED
  }
  if (analogRead(GREENswitch) == 1023){
    analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, writeValue);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
  if (analogRead(REDswitch) == 1023){
       analogWrite(REDPIN, writeValue);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
 

 Serial.print("You are writing a value of ");  //for debugging print your values
 Serial.println(writeValue);
  Serial.println(colorpinvalue);
 
}

Im using these parts:

and Arduino Uno

OP image

You need a series LED resistor for each color.
You need a gate resistor (220R) for each transistor.

pinMode(potPin, INPUT); //set potPin to be an input
Change to:
pinMode(potPin, INPUT_PULLUP); //set potPin to be an input

OMG I hate Fritzing!

Use pulldowns (Try 10K) on the switch inputs, then use digitalRead()

int writeValue; // Use this variable for writing to LED
Change to:
long writevalue;


writeValue = (255./1023.) * readValue;
change to:
writeValue = (readValue * 255) /1023;

larryd:
You need a series LED resistor for each color.
You need a gate resistor (220R) for each transistor.

pinMode(potPin, INPUT); //set potPin to be an input
Change to:
pinMode(potPin, INPUT_PULLUP); //set potPin to be an input

OMG I hate Fritzing!

Use pulldowns (Try 10K) on the switch inputs, then use digitalRead()

int writeValue; // Use this variable for writing to LED

Change to:
long writevalue;

writeValue = (255./1023.) * readValue;
change to:
writeValue = (readValue * 255) /1023;

Thanks. By pulldown something like the new attached image?
Sorry for the fritzing. I figured it was better than me trying to explain in words :wink:
How did you do the inline image?

Capture5.PNG

Just putting a 10k resistor inline on the switch pins seems to have calmed it down a lot.

this change didnt work well on my dimmer. kind of jumped around and wasnt smooth for some reason.

writeValue = (255./1023.) * readValue;
change to:
writeValue = (readValue * 255) /1023;

Added White...

int potPin= A0;  //Declare potPin to be analog pin A0
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int colorpinvalue; //which switch position was selected
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define BLUEswitch A1
#define REDswitch A2
#define GREENswitch A3
#define WHITEswitch A4
  
void setup() {
  pinMode(potPin, INPUT);  //set potPin to be an input
  Serial.begin(9600);      // turn on Serial Port
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
 
}
 
void loop() {
 readValue = analogRead(potPin);  //Read the voltage on the Potentiometer
writeValue = (255./1023.) * readValue;
  if (analogRead(BLUEswitch) == 1023){
    analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, writeValue);      //Write to the LED
  }
  if (analogRead(GREENswitch) == 1023){
    analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, writeValue);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
  if (analogRead(REDswitch) == 1023){
       analogWrite(REDPIN, writeValue);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
    if (analogRead(WHITEswitch) == 1023){
       analogWrite(REDPIN, writeValue);      //Write to the LED
    analogWrite(GREENPIN, writeValue);      //Write to the LED
    analogWrite(BLUEPIN, writeValue);      //Write to the LED
  }
 
// analogWrite(REDPIN, 0);      //clear the LED
// analogWrite(GREENPIN, 0);      //clear the LED
// analogWrite(BLUEPIN, 0);      //clear the LED
//analogWrite(colorpinvalue, writeValue);      //Write to the LED
 Serial.print("You are writing a value of ");  //for debugging print your values
 Serial.println(writeValue);
  Serial.println(colorpinvalue);
 
}

Now I need to come up with logic if there no input on any of the switch pins turn the LEds off. Now they just stay at the last setting.

Capture5.PNG

That’s a 220Ω resistor, a 10K should suffice. One resistor per input.

Use:
long writeValue;
And:
writeValue = (readValue * 255) /1023;

#define BLUEswitch A1
Change
#define BLUEswitch 15 <———<<<< then use pinMode(BLUEswitch, input); // etc.

analogRead(BLUEswitch) == 1023)
Change
digitalRead(BLUEswitch == HIGH) // etc.

larryd:
Capture5.PNG

That’s a 220Ω resistor, a 10K should suffice. One resistor per input.

Use:
long writeValue;
And:
writeValue = (readValue * 255) /1023;

#define BLUEswitch A1
Change
#define BLUEswitch 15 <———<<<< then use pinMode(BLUEswitch, input); // etc.

analogRead(BLUEswitch) == 1023)
Change
digitalRead(BLUEswitch == HIGH) // etc.

Ah this works perfectly!!! thanks so much!
I added a disco switch to make pretty colors on one switch setting and also made all the empty switch settings to equal OFF.

int potPin= A0;  //Declare potPin to be analog pin A0
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int colorpinvalue; //which switch position was selected
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define BLUEswitch 13
#define REDswitch 12
#define GREENswitch 11
#define WHITEswitch 10
#define DISCOswitch 9
#define FADESPEED 5     // make this higher to slow down
void setup() {
  pinMode(potPin, INPUT);  //set potPin to be an input
  Serial.begin(9600);      // turn on Serial Port
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
 pinMode(BLUEswitch, INPUT);
 pinMode(REDswitch, INPUT);
 pinMode(GREENswitch, INPUT);
 pinMode(WHITEswitch, INPUT);
 pinMode(DISCOswitch, INPUT);
}
 
void loop() {
  int r, g, b;
 readValue = analogRead(potPin);  //Read the voltage on the Potentiometer
writeValue = (255./1023.) * readValue;
  if (digitalRead(BLUEswitch) == HIGH){
    analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, writeValue);      //Write to the LED
  }
  if (digitalRead(GREENswitch) == HIGH){
    analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, writeValue);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
  if (digitalRead(REDswitch) == HIGH){
       analogWrite(REDPIN, writeValue);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
    if (digitalRead(WHITEswitch) == HIGH){
       analogWrite(REDPIN, writeValue);      //Write to the LED
    analogWrite(GREENPIN, writeValue);      //Write to the LED
    analogWrite(BLUEPIN, writeValue);      //Write to the LED
  }
   if (digitalRead(WHITEswitch) == LOW & digitalRead(REDswitch) == LOW &  digitalRead(GREENswitch) == LOW & digitalRead(BLUEswitch) == LOW & digitalRead(DISCOswitch) == LOW){
       analogWrite(REDPIN, 0);      //Write to the LED
    analogWrite(GREENPIN, 0);      //Write to the LED
    analogWrite(BLUEPIN, 0);      //Write to the LED
  }
   if (digitalRead(DISCOswitch) == HIGH){
      // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  }       //Write to the LED
  }
 
// analogWrite(REDPIN, 0);      //clear the LED
// analogWrite(GREENPIN, 0);      //clear the LED
// analogWrite(BLUEPIN, 0);      //clear the LED
//analogWrite(colorpinvalue, writeValue);      //Write to the LED
 Serial.print("You are writing a value of ");  //for debugging print your values
 Serial.println(writeValue);
  Serial.println(colorpinvalue);
 
}

Congrats!