• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Simple led not lighting ??

Q&A Forum › Category: Arduino › Simple led not lighting ??
0 Vote Up Vote Down
Larry asked 3 years ago

     So I have been studying the only way i know how, I take a simple concept and keep writing it until it becomes second nature. I have a very basic code that as far as I can tell should work, no errors and compiles every time. Starting to think there maybe something wrong with the module but can’t find it or I am just missing something so basic . 
     I am using an Arduino Nano flashed to Uno ,I have ran as a Nano and also as a Uno to no avail. This is just a code to ask ” What color led ” and then turn on that led, simple. If I write in the code setup digitalWrite(redLed,HIGH); and run the code the led works so I know its not an output issue and the led actually works and wired correctly. I remark that out and run the same code and I select the led ie: red it does not illuminate . I have tried a different module and again no difference. Pulling what little hair out I have, thanks for taking the time to look at this.

Start your code here

[code]
String msg = ” What color led do you want ?” ;
String myColor;
String msg1 = ” You picked “;
int redLed=8;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redLed,OUTPUT);
//analogWrite(redLed,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println (msg);
while (Serial.available() == 0) {
}
myColor = Serial.readString();
Serial.print(msg1);
Serial.print(myColor);
if (myColor==”red”){
digitalWrite(redLed,HIGH);
delay(500);
}
}
[/code]

Question Tags: Basic programming
11 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
I think this discussion answers your question: https://forum.arduino.cc/t/read-and-compare-string-form-serial-port/
Mainly this response: https://forum.arduino.cc/t/read-and-compare-string-form-serial-port/100028/3
I hope this helps.
Regards,
Sara

0 Vote Up Vote Down
Larry answered 3 years ago

Doesn’t help, I don’t understand any of it LOL
This code as written should just turn on a led when prompted by Serial.available()==0{ } and I am having difficulty understanding why when myColor=Serial.readString(); actually reads the string that the  if statement doesn’t seem to work but the Serial monitor sees the answer. 
 

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Maybe this is a simpler explanation for your scenario. It uses a similar example: https://arduino.stackexchange.com/questions/79716/comparing-a-string-after-reading-it-from-serial-fails
Basically, you need to use Serial.readStringUntil(‘\n’); instead of Serial.readString() because it will read the carriage return. Everything is explained in the previous link.
 
Meanwhile, it may also be interesting to take a look at this: https://forum.arduino.cc/t/serial-input-basics-updated/382007
 
I hope this helps.
Regards,
Sara

0 Vote Up Vote Down
Larry answered 3 years ago

So I tried the code again, can’t explain it but it now works as written, added some comments and more led’s but that was all . See below 

Start your code here

[code]
String msg = ” What color led do you want ?” ; //Select led.
String myColor; //Defines Color.
String msg1 = ” You picked “; //Message what led you picked.
int redLed = 8; //Defines Red led pin.
int blueLed = 7; //Defines Blue led pin.
int greenLed = 6; //Defines Green led pin.
int dt=500;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenLed,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println (msg);
while (Serial.available() == 0) {
}
myColor = Serial.readString();
Serial.print(msg1);
Serial.print(myColor);
if (myColor == “red”) {
digitalWrite(redLed, HIGH);
digitalWrite(blueLed,LOW);
digitalWrite(greenLed,LOW);
delay(dt);
}
if (myColor == “blue”) {
digitalWrite(redLed, LOW);
digitalWrite(blueLed,HIGH);
digitalWrite(greenLed,LOW);
delay(dt);
}
if (myColor == “green”) {
digitalWrite(redLed, LOW);
digitalWrite(blueLed,LOW);
digitalWrite(greenLed,HIGH);
delay(dt);
}
}
[/code]

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
I’m sorry. But what do you mean? Is it working as expected?
The code you shared doesn’t work for me. It only works if use Serial.readStringUntil(‘\n’); instead of Serial.readString().
Regards,
Sara

0 Vote Up Vote Down
Larry answered 3 years ago

     Not sure why but all I did was to remove cable from Arduino, shut down computer and went back to it later. Finished writing it and reloaded it to the Arduino, set serial monitor to proper baud rate , no line ending . 
     Actually figured out that if I used any other settings for the serial monitor wouldn’t work short of the baud rate which doesn’t affect the led. 

0 Vote Up Vote Down
Larry answered 3 years ago

I guess the real reason was the serial monitor settings although I thought I tried that before. Thanks for looking, will have to try and remember the Serial.readStringUntil(‘\n’). I thought I asked that in a previous question trying to get a device to get the serial monitor to advance or something.
On a side note , I want to thank you for your support. Never been good at studying and your suggestions about one line at a time has helped me in writing code. Still not good at but getting better, decided to work on Arduino for some time to get better at it before attempting FireBase . The code I sent was written by me wino compiled errors and very little if any looking up code. Thanks again, real value in this forum.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Thanks.
With practice and time, you’ll surely evolve and become better at coding. It takes a lot of time.
When I started writing for Random Nerd Tutorials I knew very little about these subjects and I feel like I still have a long way to go. So, don’t give up.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

0 Vote Up Vote Down
Larry answered 3 years ago

Thanks teacher 🌹🍎

0 Vote Up Vote Down
Larry answered 3 years ago

Thanks teacher 🌹🍎

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Thank you 🙂

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.