UNO R3 while loop not compiling for UNO R4 WiFi

Although an Arduino newbie I have programming experience in other languages.
In my sketch I have the following lines of code which compiles, loads and runs on my Arduino UNO R3

char FwdRev = " ";  // "F" = Forwards ; "R" = Reverse

void setup () {
  // in which I do some setup stuff
}
void loop () {
  // This goes into a ‘wait’ state until something sets a wanted value

  while (!(FwdRev = "F") or (FwdRev="R")) {

/*
I read an input pin until its state tells the code to set the variable to “F” or “R” and break out of the ‘while’ loop.
*/
  }

  // and now the code processes the value of the variable.
}

When I compile this code for an Arduino R4 WiFi I get the following error.

/Users/Alan/Documents/Arduino/Locomotive_Shuffle/Locomotive_Shuffle.ino:201:16: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
  char FwdRev = " ";  // "F" = Forwards ; "R" = Reverse
                ^~~


Can anyone tell me why this is?

The compiler clearly treats the variable as if it had been declared as a constant that I am trying to convert to a variable – which is not the case.

I would have expected that no matter what the version of UNO we choose to use, the IDE compiler would continue to successfully compile code used on one version (R3) in this case for use on a newer one (R4).

In C, use single quote marks around char literals.

char FwdRev = ' ';  // 'F' = Forwards ; 'R' = Reverse

Use double quotes for strings (they get automatically null-terminated for you.

This is the same in Uno R3 or R4 because it's a C language thing

Welcome to the forum

char FwdRev = " ";

A char variable can only hold a single character but the string that you are trying to use has at least 2 characters, the space and the terminating zero of the string

Use single quotes like this instead

char FwdRev[] = ' ';

Then, in the comparisons, do this

  while (!(FwdRev == 'F') or (FwdRev == 'R')) {

Note the use of == rather than = to correct another mistake

Thanks - that compiles now. Curious the R3 compiler accepted double quotes.

Thanks. Odd that the Uno R3 compiler accepted the original and seems to run ok with it. I'll try the same on that one. Cheers.

Switch on the warnings in 'Preferences', and the UNO R3 compiler will also complain about these lines. Switching warnings on is always a good idea. Many warnings are in fact errors.

1 Like

That explains much. Thank you.

The R4 uses a different toolchain for compiling which is differently configured. What might result in a warning for the R3 might result in an error for the R4.

Just luck :slight_smile:

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