Unwanted spaces showing up in code - SOLVED

Has anyone else noticed unwanted spaces being injected to your code? It happens around special characters, like : @ # ! ?

For instance, I'll code something like: lcd.print("ABCD: ");

The next time I look at the file, that line says: lcd.print("ABCD : "); (notice the new, extra space before the colon)

This has happened to many of my projects. I'll reopen one, make an update to the code someplace NOT near the problem, and when I recompile and load the code to an Uno or a Mega, the LCD has extra spaces where they didn't used to be, sometimes causing row overflow. It happens in commands and in comments -- everything gets zapped with unwanted spaces around special characters.

If you have seen this behavior with the IDE (1.8.5) and know of a fix, please share it with me. I'm using a Windows 10 Home operating system on a year-and-a-half-old desktop, running version 2004 (OS Build 19041.450)


Always use code tags when posting code.

The example doesn't have any of the "special characters" that you mentioned. If I put

lcd.print("ABCD:  ");

into a new file, save it, close the Arduino IDE 1.8.12, open the IDE and file again, nothing has changed.

Yes, it does. The first special character I showed was a colon. Also, it doesn't happen EVERY time, just whenever it wants. But when it happens, it affects the entire file. I edited the original post putting a carriage return after the word "like" to minimize the confusion. Sorry.

Sorry, missed the colon. Install the most recent version of the IDE and try again.


jremington - Good idea, and I'll do that. But I'd like to know if anyone else has noticed this behavior. For years, it's worked just as it should. Then, across several projects (but not all), I'm noticing these extra spaces. It is frustrating, to say the least.

spycatcher2k - I know. Sometimes, it still works fine, just like your experiments suggest. But it's happening often enough to me that I really want to find the cause.

Are you using Tools > Auto Format or Ctrl + T?

My suspicion is that a syntax error in your code somehow causes "ABCD: " to not be recognized as a string. The Arduino IDE's auto format tool is configured to pad operators with spaces. For example, this code:

digitalWrite(LEDPin, turnLightOn?LOW:HIGH);

will look like this after auto format:

digitalWrite(LEDPin, turnLightOn ? LOW : HIGH);

However, the auto format ignores strings, so it should not affect the code inside quotes unless a syntax error is causing the text you intend to be string to not actually be one as far as the auto formatter is concerned.

I have run into something like this a few times myself but I can't remember exactly what the required syntax error is.

pert - Yes, that makes sense, and I do (occasionally) use the auto format command. But as for syntax errors... I can't find anything. With or without the extra spaces, compilation happens normally, and the only unwanted result is LCD row overflow (plus it looks wrong to the person who designed it!). I'll have to watch my use of auto format. Thanks for that insight!

In my case, I made a syntax error, did the auto format, noticed the syntax error and fixed it, and only later when I looked at the code farther down in the sketch did I notice that the auto format I did when I had the syntax error messed up other parts of my code.

I've had the same issue with auto formatting Python scripts, but in that case it was much more serious because in Python the program structure is controlled by indentation and the syntax error combined with the auto format caused the indentation to change!

pert - You're saying things that feel really familiar to me. I don't use auto format all the time, but usually when I do use it, I do so because I have nested logic that is easier to "comb out" when it's formatted the way the compiler sees it. It could well be that in the instances when things have gone amok in my attempt to put deeply-nested (incorrect) logic into my code, the compiler does not see my string AS a string -- and I don't look farther than the issue that caused the odd indentation (usually a missed quote or parenthesis!). And that happens only rarely, so it fits with the problem being only an occasional one. I think this one is solved. THANK YOU!

You're welcome. I'm glad if I was able to be of assistance.

I should clarify that, although I mention some rare problems I've had with auto format tools here, I am actually a huge fan of them and use them constantly for any code I write.