Hey everyone. I created an RGB color picker that uses three potentiometers to control the amount of R, G, and B in an RGB LED. This circuit and code worked a while ago, but now, when I rotate one potentiometer, it somehow changes the others. The R, G, and B values seem to be jumping as well. I have each potentiometer connected to a different analog pin on the arduino. I have no idea what to do and I have to demonstrate this circuit soon in an interview.
Any help would be greatly appreciated.
Here is a video of what I'm talking about if it is still hard to visualize: https://drive.google.com/file/d/1DuxETy9RCZuzVLbNPQTu1U9NFUKBQAEl/view?usp=sharing
Here is a picture of my circuit: IMG_1647.JPG - Google Drive
Here is my code:
#include <LiquidCrystal.h> //Include the LCD library
LiquidCrystal lcd(7, 8, 10, 11, 12, 13); //Set up LCD
#define rPot 2 //Initialize Pot ports
#define gPot 1
#define bPot 0
#define rLED 6 //Initialize RGB LED ports
#define gLED 5
#define bLED 3
void setup() {
pinMode(rPot, INPUT); //Set Pots as inputs
pinMode(gPot, INPUT);
pinMode(bPot, INPUT);
pinMode(rLED, OUTPUT); //Set LEDs as outputs
pinMode(gLED, OUTPUT);
pinMode(bLED, OUTPUT);
analogWrite(9,50); //Set LCD contrast to 50 (in order to eliminate the use of a pot to control contrast)
lcd.begin(16,2); //Set up LCD dimensions
}
void loop() {
int r=map(analogRead(rPot), 0, 1023, 0, 255); //Set r to pot value (/(1023/255) to match the pot range of 1023 to the LED's of 255)
int g=map(analogRead(gPot), 0, 1023, 0, 255); //Do the same with g
int b=map(analogRead(bPot), 0, 1023, 0, 255); //Do the same with b
setColor(r, g, b); //Set the color of the LED to r, g, and b
lcd.clear(); //Clear the LCD
printRGB(r, g, b); //Print the RGB value on the LCD
printHex(r, g, b); //Print the HEX value on the LCD
delay(30); //Delay for 30 milliseconds in order to eliminate of the "strobe light" effect on LCD
}
void setColor(int r, int g, int b){
analogWrite(rLED, r); //Set R LED value to r
analogWrite(gLED, g); //Same with G
analogWrite(bLED, b); //Same with B
}
void printRGB(int r, int g, int b){
String strR = String(r); //Set strR to string of r
String strG = String(g); //Set strG to string of g
String strB = String(b); //Set strB to string of b
String rgb="("+strR+","+strG+","+strB+")"; //Compile strR, strG, and strB with parenthesis at the ends and commas in between each value
lcd.setCursor(0,0); //Print at column 1, row 1
lcd.print("RGB"); //Print RGB to indicate that it is the rgb value
lcd.print(rgb); //Print the value
}
void printHex(int r, int g, int b){
String strR = String(r, HEX); //Convert r to hex and set the string of it to strR
String strG = String(g, HEX); //Convert g to hex and set the string of it to strG
String strB = String(b, HEX); //Convert b to hex and set the string of it to strB
strR.toUpperCase(); //Capitalize strR
strG.toUpperCase(); //Same with strG
strB.toUpperCase(); //Same with strB
if (strR.length()==1) //Ensures that strR is two characters instead of one
strR=strR+strR;
if (strG.length()==1) //Same with strG
strG=strG+strG;
if (strB.length()==1) //Same with strB
strB=strB+strB;
String hex=strR+strG+strB; //Compile strR, strG, and strB to form hex code
lcd.setCursor(0,1); //Print at column 1, row 2
lcd.print("Hex:"); //Print "Hex:" to indicate that it is the hex value
lcd.print(hex);
}