Help on RGB program please!

I have been trying to create a program that controls a RGB light with the serial monitor, but (I believe) the if statement does no work... Here's the program:

int redPin=5;
int greenPin=6;
int bluePin=9;
String RGBcolor;
String msg="What Color Do You Want?";

void setup() {
  Serial.begin(9600);
  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
}

void loop() {
  Serial.println(msg);
  while (Serial.available()==0) {}
  RGBcolor=Serial.readString();

if(RGBcolor=="red");{
  digitalWrite(redPin,HIGH);
  digitalWrite(greenPin,LOW);
  digitalWrite(bluePin,LOW);
}

if(RGBcolor=="green");{
  digitalWrite(redPin,LOW);
  digitalWrite(greenPin,HIGH);
  digitalWrite(bluePin,LOW);
}

if(RGBcolor=="blue");{
  digitalWrite(redPin,LOW);
  digitalWrite(greenPin,LOW);
  digitalWrite(bluePin,HIGH);
}
}

What happens is that when I start the program and write "red" (without the " ") the LED turns green (it is wired correctly) and I can't change the color without restarting the arduino... Thanks for any help!

At the bottom of serial monitor there is a drop-down selection for "line ending". Try setting it to "none".

Sorry I started using with the arduino this week, so I am not completely sure of what you are talking about. But my Serial Monitor has the options of: No Line Ending / New Line (the one I am using) / Carriage Return / Both NL & CR

I removed the ";" after the if statement, but now my RGB doens't turn on

It worked! I removed the ";" from the if statement and put in "No Line Ending" mode and it is working perfectly. Thank you guys, I spent I lot of time trying to fix this!

2 Likes

how many hours?
it may take you 10 hours to work through this tutorials but after that you know most of the basics
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Yeah, that one! Sorry, I couldn't remember the exact wording.

What was going wrong was that the String read in from Serial contained the characters "r", "e", "d" and the New Line character. 4 characters long, so it did not match "red" which is only 3 long.

Plus, as pointed out, you had those semicolons, which I should have spotted also! :man_facepalming:t2:

Thanks!

I am following a tutorial from Paul McWhorter. He is really good, what happened is that since my Serial Monitor was "New Line" instead of "No Line Ending" the program (the exact same as his) didn't work).

So I started trying a bunch of stuff and changing my code (hence the ";" in the if statement, because for some reasons the LED lit up with it instead of not lighting up a all).

But besides the hours of stress I spent trying to fix it I learned a lot and now I can code a program like this with my eyes closed hahah

Have a great day!

For analysing such things you can print the received data to the serial monitor

  Serial.print("#");
  Serial.print(RGBcolor);
  Serial.println("#");

This code has a variant of readin-in serial data

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


int redPin   = 5;
int greenPin = 6;
int bluePin  = 9;
String RGBcolor;
String msg = "What Color Do You Want?";

void setup() {
  Serial.begin(9600);
  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
}

void loop() {
  dbgi("01:",digitalRead(redPin),1000);
  dbgi("01:",digitalRead(greenPin),1000);
  dbgi("01:",digitalRead(bluePin),1000);
  Serial.println(msg);
  while (Serial.available() == 0) {}
  RGBcolor = Serial.readStringUntil(0x0A);
  //RGBcolor = Serial.readString();
  Serial.print("#");
  Serial.print(RGBcolor);
  Serial.println("#");

  if(RGBcolor == "red") {
    Serial.println("received red");
    digitalWrite(redPin,HIGH);
    digitalWrite(greenPin,LOW);
    digitalWrite(bluePin,LOW);
  }

  if(RGBcolor == "green") {
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,HIGH);
    digitalWrite(bluePin,LOW);
  }

  if(RGBcolor == "blue") {
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,LOW);
    digitalWrite(bluePin,HIGH);
  }
}
  //RGBcolor = Serial.readStringUntil(0x0A);
  RGBcolor = Serial.readString();

This variant reads in until a terminating character (the line-ending that you disabled)

If you have the line-ending activated and it is received the serial printing looks like this

What Color Do You Want?
#red
#
What Color Do You Want?

the second double-cross in a new line indicates that there is a line-ending character in the string

without line-ending it looks like this

What Color Do You Want?
#red#
What Color Do You Want?

second double-cross in the same line

another approach would be to examine the received string if there is a line-ending-character and if yes to keep only the characters before the line-ending
then the if-condition fits

best regards Stefan

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