1

I just started two weeks ago with Arduino and I'm hooked!

I got off to a good start and went through some online tutorials quite easily. Then came the RGB LED, which I couldn't get to work all of a sudden (it required putting in a color through the serial monitor, which would then be given by the RGB).

And the strange thing is, I can't get anything to work anymore that requires input through the serial monitor, where I used to get it working immediately...

I presume it's something with the if-statement, because if I remove it and just say digitalwrite HIGH to a certain pin, it works fine....

Checks I did:

  • Full re-install of Arduino (didn't solve it).
  • Check the LEDs by hooking them up to 5V directly (they all work).
  • Reconnecting the Arduino (didn't solve it).
  • Use a multimeter to measure current (there isn't, although the if-statement matches...

Here's my code. Does anybody see it? Because I don't...

Regards,

nannev (using Mac and Arduino Uno)

int yellowPin=8;
int redPin=9;
int greenPin=10;
String myLED;

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

void loop() {
  Serial.println("Your color?");
  while (Serial.available()==0) {
  }

  myLED=Serial.readString();
  Serial.print(myLED); //check if the input came through correctly, which it did
  delay(1000);
  if(myLED == "yellow"){ .    //nothing happens at yellow....
    digitalWrite(yellowPin, HIGH);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
  }

  if(myLED == "red"){             //nothing happens at red....
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
  }

  if(myLED == "green"){       //nothing happens at green....
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
  }
}
6
  • check the line ending setting in Serial Monitor. 'yellow\r\n" is not equal to "yellow". you can trim the line ends with myLED.trim();
    – Juraj
    Jan 26 '20 at 18:37
  • you can add Serial.print(myLED.length()); so that you can see how many characters there are in the string
    – jsotola
    Jan 26 '20 at 19:39
  • " if(myLED == "yellow"){ . //nothing happens at yellow...." Look at the period after {. That's a Mac quirk: sometimes when you press space twice it converts it to ". " – there's no way your code is compiling as is.
    – dda
    Jan 27 '20 at 3:08
  • @Juraj Wow, this solved it! It appeared that the length of the String was 1 character greater than the actual word. Strange though, that I had it working several times before without the Trim function. Thanks, wouldn't have come up with this myself.
    – nannev
    Jan 27 '20 at 7:42
  • 1
    @jsotola Thanks too!
    – nannev
    Jan 27 '20 at 7:42
0

The Arduino IDE uses the C and C++ programming languages.

Some string comparison basics using C:

In the C programming language, a string can be thought of as an array of 8 bit values. One way to compare two strings is to compare each 8 bit value and consider the strings different (not equal) upon finding the first not-matching 8 bit value. Here is an example from programmingsimplified.com:

int compare_strings(char a[], char b[])
{
   int c = 0;

   while (a[c] == b[c]) {
      if (a[c] == '\0' || b[c] == '\0')
         break;
      c++;
   }

   if (a[c] == '\0' && b[c] == '\0')
      return 0;
   else
      return -1;
}

There is a common function called strcmp() which can be used instead. Here is another example from the same web page which uses strcmp():

#include <stdio.h>
#include <string.h>

int main()
{
   char a[100], b[100];

   printf("Enter a string\n");
   gets(a);

   printf("Enter a string\n");
   gets(b);

   if (strcmp(a,b) == 0)
      printf("The strings are equal.\n");
   else
      printf("The strings are not equal.\n");

   return 0;
}

Note that in both cases the array of strings is assumed to end with a NULL or zero value byte. If you do not wish to follow this format you can use an alternate version of the function called strncmp() where the number of characters to be compared is passed along with pointers to the 2 strings.

Some string comparison options using C++:

There are C++ ways to compare 2 strings which make coding Arduino Sketches easier. But C++ string manipulation usually requires more RAM. And RAM is usually in short supply on inexpensive embedded processors.

As the code in the question is written using type String we can explore that approach. And, as there is some concern the strings may have unexpected trailing characters, we will continue to check only the necessary number of characters. In this slightly modified code example from geeksforgeeks.org string s1 is compared with string s2. But only the first 6 characters are compared:

void compareOperation(string s1, string s2) 
{ 
    // Compares 5 characters from index number 3 of s2 with s1 
    if((s2.compare(0, 6, s1)) == 0) 
        cout << "Here, "<< s1 << " are " << s2; 

    else
        cout << "Strings didn't match "; 
} 

The number 6 was chosen because "yellow" contains 6 printable characters. In this way, we can avoid the uncertainty of what character(s) follow the characters that make up "yellow" as received from the serial port.

4
  • the Arduino String class has == operator overloaded to compare with char*. why this complicated off-topic answer?
    – Juraj
    Jan 27 '20 at 9:58
  • The "compare()" was the 1st example found. So it was used. Also, there was talk in the comments about string length and problems with different unprintable characters after the desired printable word. So I offered a solution which mitigated problems associated with unprintable characters by counting the number of characters to compare. Lastly, this is an Arduino forum. Most Arduino platforms contain a very-low-cost embedded processor with very-limited resources. So it is almost always better to control string manipulation explicitly with clearly defined string lengths using C programming.
    – st2000
    Jan 27 '20 at 13:04
  • I've changed the answer making it clear that the 1st part is C and the 2nd part is C++. I also added text explaining that character counting in both examples was used to mitigate any differences between the 2 strings which might occur after the desired word.
    – st2000
    Jan 27 '20 at 13:08
  • all true but not an answer. the compare failed only because the Serial Monitor input did contain line ends. this as a common mistake by beginners
    – Juraj
    Jan 27 '20 at 13:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.