RGB LED not responding to Serial Monitor Input

Good afternoon,

I recently started learning Arduino coding and some basic circuit building from Paul McWhorter on Youtube (great teacher btw). I was following his tutorial of building a circuit with an RGB LED that responds to the Serial Monitor Input. However, I haven't been able to get my code to work and I cannot figure out why. My code looks very similar to Paul's in his video and as far as I can tell, my circuit looks like it is built correctly. What am I missing here?

(link to Paul's video)

Here is my code:


int redLed=11;
int greenLed=3;
int blueLed=6;
String colourChoice; //var for user input

int ma=255; //ma stands for maximum
int h=200; //h stands for high
int mH=150; //mH stands for midHigh
int m=125; //m stands for middle
int mL=100; //mL stands for midLow
int l=50; //l stands for low
int z=0; //z stands for zero

String blankSpace=(" ");

void setup()
{

Serial.begin(9600);
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(blueLed,OUTPUT);

}

void loop()
{

Serial.println("What colour would you like the LED to be? (red, green, blue)");
while (Serial.available()==0){ }
colourChoice=Serial.readString();

Serial.print("You have chosen ");
Serial.println(colourChoice);

if (colourChoice=="red")
{
analogWrite(redLed,ma);
analogWrite(greenLed,z);
analogWrite(blueLed,z);
}

if (colourChoice=="green")
{
analogWrite(redLed,z);
analogWrite(greenLed,ma);
analogWrite(blueLed,z);
}

if (colourChoice=="blue");
{
analogWrite(redLed,z);
analogWrite(greenLed,z);
analogWrite(blueLed,ma);
}

if (colourChoice != "red" && colourChoice != "green" && colourChoice != "blue")
{
Serial.println("That is not one of the options.");
}

Serial.println(blankSpace);
delay(1000);

}

First thing...
I'm not a big fan or this, but if it's from the tutorial - let it slide for now...

Next - Double check your serial monitor 'speed' is set to match the Serial.begin() statement. (i.e. 9600)

Hi, @sticky_joe
Welcome to the forum.
To add code please click this link;

What problem do you have?
Will it compile?
Will it upload?
Do you get any message on the IDE monitor liike "What colour would you like the LED to be? (red, green, blue)"

Make sure as @lastchancename has pointed out, that your monitor baud rate is correct.

Your code works for me, on a Nano, much the same as a UNO.

Tom. :smiley: :+1: :coffee: :australia:

Do you have the same LED as in the video?

Can you make the LED work by powering directly or did it ever work?

TBC direct… 5 volts - series resistr - LED segment - GND.

Oh, and welcome to the community.

a7

Hi lastchancename, I'm sorry but I forgot to mention that I haven't purchased an Arduino yet which is why I've been working on my Arduino projects on the Tinkercad website. When I first learned about baud rate, I wasn't able to find it on the Tinkercad software - I then did a few Google searches but I haven't been able to find out whether Tinkercad has the baud rate feature or not. Would you happen to know if the Tinkercad software includes the ability to edit the baud rate?

Also what would be a good alternative to
while(Serial.available()==0){}
? If there is a more efficient/cleaner way to achieve the same result, I'm all for it.

Hi,
Do you get the ""What colour would you like the LED to be? (red, green, blue)"
When you run your code on TinkerCad?

Tom... :smiley: :+1: :coffee: :australia:

Hi TomGeorge, thanks for the warm welcome.

As I mentioned in my reply to lastchancename, I forgot to mention a few key aspects of my learning, that I'm learning on Tinkercad and as well as what specifically my problem is (can't belive I forgot this, sorry). Please refer to my reply to lastchancename regarding the baud rate (if you happen to know the answer, please let me know). As for my specific problem, it is that although the Serial monitor responds to the input I put in as the user, i.e. the
"You have chosen " (colourChoice)
print line is printed everytime. However, although the RGB LED turns on, it is always the same dark blue colour regardless of what I answer in the Serial monitor, even when I type in a random string to prompt the
"That is not an option."
string.
In short, my problem is that although the code executes and the Serial monitor responds to it, the RGB LED does not.

Hi alto777, thank you also for the warm welcome.

As I mentioned in my replies to lastchancename and TomGeorge, I am using a virtual Arduino simuilator through Tinkercad and so do not have the physical Arduino components on hand. As you can see from my screenshot, I have connected all of my resistors in series, coming from different pins. All of the resistors are set to 330 Ohms of resistance each. As for the RGB LED, I cannot control anything about it except for its name in the circuit.

As I also mentioned in my reply to TomGeorge, my issue is that the RGB LED does not respond to the Serial monitor and I have no clue why.

Hi,
Good that you got that far, iit means your monitor speed is correct and your Serial input and output is working.
Thats why we asked about the monitor, it tells us a lot of things.

Tom.. :smiley: :+1: :australia:

Hi,
You need to write some code that JUST flashes your LEDs to check that you have control of them.

Tom... :smiley: :+1: :coffee: :australia:

Hi Tom,

If you mean that I should check if my LED works at all, I wrote a temporary analogWrite(redLed,255); just by itself (without input from serial monitor) and it worked. The LED turns to whatever colour I want it to when I code the values in. It is just that as soon as I start using the serial monitor to control the colours, the LED doesn't work properly.

Hi,
Thats good.
Try this edit of your code.
It will keep asking for you to enter a colour repetitively, don't worry about that at the moment.
Run it and see what happens.

int redLed = 11;
int greenLed = 3;
int blueLed = 6;
String colourChoice; //var for user input
int ma = 255; //ma stands for maximum
int h = 200; //h stands for high
int mH = 150; //mH stands for midHigh
int m = 125; //m stands for middle
int mL = 100; //mL stands for midLow
int l = 50; //l stands for low
int z = 0; //z stands for zero

String blankSpace = (" ");

void setup()
{

  Serial.begin(9600);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);

}

void loop()
{

  Serial.println("What colour would you like the LED to be? (red, green, blue)");
  while (Serial.available() > 0) {
    colourChoice = Serial.readString();

    Serial.print("You have chosen ");
    Serial.println(colourChoice);

    if (colourChoice == "red")
    {
      analogWrite(redLed, ma);
      analogWrite(greenLed, z);
      analogWrite(blueLed, z);
    }

    if (colourChoice == "green")
    {
      analogWrite(redLed, z);
      analogWrite(greenLed, ma);
      analogWrite(blueLed, z);
    }

    if (colourChoice == "blue");
    {
      analogWrite(redLed, z);
      analogWrite(greenLed, z);
      analogWrite(blueLed, ma);
    }

    if (colourChoice != "red" && colourChoice != "green" && colourChoice != "blue")
    {
      Serial.println("That is not one of the options.");
    }
  }
  Serial.println(blankSpace);
  delay(1000);

}

Tom... :smiley: :+1: :coffee: :australia:

Hi,
Have you tried the exact code that Paul McWhorter posted.

Also keep in mind Tinkercad being a simulation is not always perfect.

Have you ordered your parts?

Tom... :smiley: :+1: :coffee: :australia:

Hi, I pasted the code into my project but I encountered the exact same problem... maybe my project file itself just contains a glitch that I cannot find?

oh wait... I just tried pasting in Paul's code as you suggested and it worked... I wonder why? I'll analyze it and get back to you

hm ok so Paul's code definitely works and I tried my code in a different project but it still does not work, meaning there is something wrong with my code. However, after comparing the two, I've come to the conclusion that they are very similar (as I wrote it while following his tutorial). The key differences are the variables that I have present to mark various output levels for the pins while he does not, and that I also have an extra print function (that repeats the user's input for reference) while Paul does not. I am still very confused...
(Paul's code):

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 brightness=255; //Set brightness to 75
String colorChoice; //Will hold users input of color choice

void setup() {
// put your setup code here, to run once:

Serial.begin(9600); //Turn on Serial port
pinMode(redPin, OUTPUT); //Set redPin to be an output
pinMode(greenPin, OUTPUT); //Set greenPin to be an output
pinMode(bluePin, OUTPUT); //set bluePin to be an output

}

void loop() {

Serial.println("What color would you like the LED? (red, green, or blue)"); //Prompt user for color
while (Serial.available()==0) { } //Wait for input
colorChoice = Serial.readString();

if (colorChoice=="red") {

analogWrite(redPin, brightness); //turn on red pin
analogWrite(greenPin, 0); //turn off green pin
analogWrite(bluePin, 0); //write off blue pin
}

if (colorChoice=="blue") {

analogWrite(redPin, 0); //turn off red pin
analogWrite(greenPin, 0); //turn off green pin
analogWrite(bluePin, brightness); //write on blue pin
}

if (colorChoice=="green") {

analogWrite(redPin, 0); //turn off red pin
analogWrite(greenPin, brightness); //turn on green pin
analogWrite(bluePin, 0); //write off blue pin
}

if (colorChoice!="red" && colorChoice!="green" && colorChoice != "blue") {
Serial.println("That is not a valid color choice, please try again");
Serial.println("");
}

}

Hi,
Go back to your original code, I have it setup here, and only blue comes up, no matter what you send.
Yes Paul"s does work.
I'll be back..
Tom... :smiley: :+1: :coffee: :australia:

Hi,
Found it, just a simple keyboard input.
Your original code

if (colourChoice=="blue");

The semi-colon shouldn't be there, easily done.

if (colourChoice=="blue")

Tom... :smiley: :+1: :coffee: :australia:

Yes, the stray semicolon.

Oddly the first

 colourChoice = Serial.readString();

for me is corrupted somehow (wokwi simulator, blaming it until I can use a real board).

And did we ask about the line ending setting in the serial monitor? It must set to "No line ending".

OK ona real Arduino, the first read is not corrupted and the program works fine.

I got no clue. Def not the spelling of colour. :expressionless:

a7

That is how the Queen's English spells it. :+1:
Thats the proppa way. :+1:

color is just plainlazy... :-1:

It must set to "No line ending".

I think is the default for the monitor.

Tom.... :smiley: :+1: :coffee: :australia: