Issue w/ user choice and staying in a loop (loop within a loop?)

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

Hello and good morning
Well, I guess it is useful to separate your sketch into three parts:
I=input -->>> read keyboard // poti
P=processing -->>>> prepare colour setting wrt to the input
O=output -->>> write color settings to RGB led
Each part can be programmend and tested separatley.

IPO model - Wikipedia.

This approach protects you a bit from programming spaghetti code.

Hello

Consider something like this

void loop()
{
  static bool messageShown = false;
  static uint8_t ledPin = 0;

  if ( messageShown == false )
  {
    messageShown = true;
    Serial.println( "Which LED would you like to manipulate? (Red, Blue, Green, None)" );
  }
  
  if ( Serial.available() )
  {
    String ledChoice = Serial.readString();
    ledChoice.toLowerCase();
    
    if ( ledPin != redPin && ledChoice == "red" )
    {
      Serial.println( "Red selected!" );
      ledPin = redPin;
    }
    else if ( ledPin != greenPin && ledChoice == "green" )
    {
      Serial.println( "Green selected!" );
      ledPin = greenPin;
    }
    else if ( ledPin != bluePin && ledChoice == "blue" )
    {
      Serial.println( "Blue selected!" );
      ledPin = bluePin;
    }
    else if ( ledPin != 0 && ledChoice == "none" )
    {
      Serial.println( "Deselected!" );
      ledPin = 0;
      messageShown = false;
    }
  }
  
  if ( ledPin != 0 )
  {
    analogWrite( ledPin, analogRead( potPin ) / 4 );
  }
}

@guix nice sketch
Try

    String ledChoice = Serial.readString();
    ledChoice.trim(); // remove any /r/n and space
    ledChoice.toLowerCase();

so the == "green" works if IDE has a line ending selected.
Also adding
Serial.setTimeout(100);
to setup() will make the input more responsive. The default is 1sec which is noticable.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.