I'm relatively new to arduino, but using the knowledge I've gained from about 13 lessons of Paul McWhorter's tutorials on arduino, I want to create a Color Changer program for the Arduino Uno.
Materials List This program makes use of the Serial Monitor, a common-anode RBG LED, and a potentiometer.
The Goal: I want a user to select a color when prompted (red, green, or blue) and be able to adjust the amount of a color that shows up in the LED via the potentiometer knob. The user can change that single color for as long as they want until they type in another color. I want the program to remember the manipulated colors in between picks. The LED is on the entire time.
Example: The program asks which color the user wants to change, he types in 'red,' and turns the potentiometer so that there is no red in the RBG LED. Then he types in 'blue' and turns the knob until he's satisfied with the amount of blue present. He types in 'green' until he sees a nice turquoise color shown on the RBG LED
Problem 1 I want the user to be able to manipulate their chosen LED for as long/as short as they want. This requires a loop, but I don't know how to keep the user in the loop until they type in "Red/Blue/Green"
Problem 2 To temporarily get around Problem 1, I hard-coded in a time limit of 10 seconds for the user to change an LED's color before they are asked what color they want to change. After selecting "red," for example, I can change the red LED color balance for 10 seconds, but the program will force me to change the blue LED without prompt immediately afterwards.
I have attached my schematic and code below. Thank you very much for your help.
// NOTE - THIS CIRCUIT AND PROGRAM WAS DESIGNED FOR A COMMON **ANODE** RGB LED //
// 255 CORRESPONDS TO THE LOW/OFF STATE FOR AN LED //
// 0 CORRESPONDS TO THE HIGH/ON STATE FOR AN LED //
int redPin = 11; //Set red LED pin to 11
int greenPin = 10; //Set green LED pin to 10
int bluePin = 6; //Set blue LED pin to 6
int potPin = A0; //Assigning potPin to A0
int readValue; //Declaring readValue to read pot values
int redWriteValue; //Declaring writeValue to write to Red LED
int blueWriteValue; //Declaring writeValue to write to Red LED
int greenWriteValue; //Declaring writeValue to write to Red LED
String ledChoice; //Declaring ledChoice string to hold selected LED choice
int delayTime = 50;
void setup() {
pinMode(redPin,OUTPUT); //Declare redPin an output
pinMode(bluePin,OUTPUT); //Declare bluePin an output
pinMode(greenPin,OUTPUT); //Declare greenPin an output
pinMode(potPin,INPUT); //Declare potPin an input
Serial.begin(9600); //Start your Serial port
Serial.println("Welcome to the color changer! ");
redWriteValue = 255 - 100; //Setting the program by making the RBG LED white
greenWriteValue = 255 - 100;
blueWriteValue = 255 - 100;
}
void loop() {
Serial.println("Which LED would you like to manipulate? (Red, Blue, or Green) "); //Ask user which LED's color they want to adjust
while(Serial.available()==0){ //Wait until choice is made
}
ledChoice = Serial.readString(); //Read the String
if(ledChoice == "Red" || "red" || "RED");{
Serial.println("Red selected! ");
for(int j = 1;j<100;j++) //Hard-coding a 10 second period to manipulate the LED color
{
readValue = analogRead(potPin); //Read voltage from potentiometer
redWriteValue = 255-((255./1023.)*readValue); //Calculating writeValue
Serial.println(redWriteValue); //Print writeValue (for debugging purposes)
analogWrite(redPin,redWriteValue); //Control redPin
analogWrite(greenPin,greenWriteValue); //Control greenPin
analogWrite(bluePin,blueWriteValue); //Control bluePin
delay(delayTime);
}
}
if(ledChoice == "Blue" || "blue" || "BLUE");{
Serial.println("Blue selected! ");
for(int j = 1;j<100;j++) //Hard-coding a 10 second period to manipulate the LED color
{
readValue = analogRead(potPin); //Read voltage from potentiometer
blueWriteValue = 255-((255./1023.)*readValue); //Calculating writeValue
Serial.println(redWriteValue); //Print writeValue (for debugging purposes)
analogWrite(redPin,redWriteValue); //Control redPin
analogWrite(greenPin,greenWriteValue); //Control greenPin
analogWrite(bluePin,blueWriteValue); //Control bluePin
delay(delayTime);
}
}
}