I am new to both coding and microcontrollers. I have just started learning Arduino code by viewing Paul McWhorter’s lessons on YouTube. I have an Arduino Uno R3 board connected to an HP laptop running Windows 10. I have copied Lesson 19. It compiles and uploads with no error messages but will not activate the LEDs when I type the desired color and press enter. The Rx and Tx LEDs on the board activate to indicate that the program is running. I know that the breadboard is set up properly as the LEDs activate when I run a program that directly blinks the LEDs rather than asking for an input from the keyboard.
Paul McWhorter’s YouTube Lesson 19:
int redPin = 12;
int greenPin = 11;
int bluePin = 10;
String myColor;
String msg("What color LED? ");
void setup() {
// put your setup code here, to run once:
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
When I run the program, nothing happens. What am I doing wrong?
I'm sorry that you got into this. We don't use functions such as "Serial.readString()" and we often don't use the "String" with a Arduino Uno. It is completely normal that you encounter these problems, you are still on the right track
Do you have programming experience ? (any language)
Is there something that has your special interest ?
Serial communication is not straight forward. When you type something in the Serial Monitor, then the baudrate has to be right and sometimes extra characters are added, such as CarriageReturn and LineFeed.
You can trim away those extra characters, and I suggest to use more Serial.println() to show what is going on, and add a delay so you can see the leds.
This might work better:
int redPin = 12;
int greenPin = 11;
int bluePin = 10;
String myColor;
String msg("What color LED? ");
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(msg);
while (Serial.available() == 0) {
}
myColor = Serial.readStringUntil('\n'); // Changed
myColor.trim(); // Added
Serial.print("You typed: "); // Added
Serial.println(myColor); // Added
if (myColor == "red" || myColor == "Red") {
Serial.println("Red activated"); // Added
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
delay(5000); // Added
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
Upload the following sketch (your one with some moderation) with Newline option. Enter RED in the InputBox of Serial Monitor and then click on the Send button; check that only Red LED is On. (It is no good to use String data type in UNO.) Sketch:
int redPin = 12;
int greenPin = 11;
int bluePin = 10;
char myColor[5];
char msg[] = "What color LED? ";
void setup()
{
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.println(msg);
}
void loop()
{
while (Serial.available() == 0)
{
; //wait
}
byte m = Serial.readBytesUntil('\n', myColor, sizeof myColor - 1);
myColor[m] = '\0'; //null charcater is appended at the end of received string
if (strcmp (myColor, "RED") == 0) //RED message has arrived from Serial Monitor
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
Serial.println(msg);
}
well, they all kind of blink, but that's a wad (working as designed) not a bug..
it does seem read in the word red..
at least in the sim.. Uno Serial Leds..
no delays, so the blink is super fast, i see you've added a delay in there, try adding it in the sim..
Thanks for your suggestion but it still won't work. It worked fine for McWhorter in lesson 19 on YouTube but as hard as I have tried I can't it to do anything. I have deleted and reentered the code numerous times without any change in the way it works. No problem for compile or upload.
Again, thanks for your reply.
I verified that the LEDs were good by connecting to the 5v outlet. I then activated them by using a program that activates the LEDs within the program. They all worked as programed. They don't work when the input is from the keyboard.
This is my first post. I apologize that I didn't do everything right. I did read the copy of the Forum but do not understand how to choose the right category or to use code tags. I did Auto Format and Copy for Forum before posting. If you can enlighten me I would appreciate it.
Thanks for your suggestion. I made the changes and it turned on the red LED but not the green or blue. I still can't understand why it works for Paul McWhorter in his lesson 19 on YouTube.
Some think that Categories are very important, but I don't care.
We use the common "Reply" button at the bottom. That works better on this forum.
There is a button <CODE/>, you can use that to put your sketch between lines of three backslash single quotes:
```
your sketch
```
If you say that it does not work, then you give us no information. It probably does something different that what you expect, so please tell what you did, what the result was and what you were expecting.
My example in Wokwi simulation ( LEDs will not Blink - Wokwi ESP32, STM32, Arduino Simulator ) turns the red led on for a few seconds when I type "red" followed by enter. If you understand the changes, then you can add the green and blue.
If you mention something, can you give a link to it ? Where can I see the code by Paul McWhorter ?
Here is tutorial 19 by Paul McWhorter in Wokwi simulation:
If something does not work with tutorial 19, then blame Paul McWhorter. He has to make a new video with new code.
We would never write such code and we would certainly never bother new users with it.
Thanks for your input. When I compile I get the
Compilation error: 'myColor' does not name a type. I don't understand how to correct this error. I have researched it but do not under stand the answers.