Debugging some issues

I’m finding compiled error messages to be a bit “ not so helpful “

I have a lengthy bit of code stretched over a few tabs . If I miss say the end bracket of a print or some other not so serious typo the compiler will come back with “XYZ “ ( my function or variable ) does not name a type . The error is often nothing to do with that variable or function and could be a misplaced ‘;’ or ‘/‘ somewhere else.

Are there any tips I can use for finding where these errors are ( apart from blocking out big chunks of code )?? .

Sometimes a message with a line number helps , but often these are the consequence of an issue somewhere else .

While I’m here my 2.x ide doesn’t pick up on things such as “serial.begin” verses Serial.begin or misspelling of variables and happily shows these in orange until I compile . Maybe related ? Have I switched something off ?

PC latest 2.x IDE and checked on 1.8 . UNO 328

Any ideas welcome, thx

Are those all *.ino? That makes this way more complicated.

A good strategy is to use *.cpp tabs, and to make changes carefully in small areas trying to verify as you go.

It can be a challenge but beats hunting down a missing <whatever>.

a7

the types of errors you describe reported typically hint at the real problem. And yes, it's often on the preceeding line.

Not likely to see an error such as "missing semicolon". Over time thru the school of hard knocks the causes of the reported error become obvious because you've made them so often.

the missing brace or parenthesis can often be found with modern editors (e.g. vim) including the wysiwyg IDE editor.

enabling compiler warnings under the preferences tab helps of course

Yes !

The compiler tells where it noticed an error and why it stopped, not what the actual error is.

The answer is really what @gcjr said. Use a modern IDE (note: I have no experience with Arduino 2.x). VS Code and the like have plugins that will check your code as you type and flag errors before they get to the compilation stage.

Derailing this thread for a second: I use VS Code for Python but never considered it for Arduino/ESP work. How does the workflow go? Do you create the code in VS Code and then open it in the IDE to compile and upload?

No I do the whole thing in Arduino IDE ! I only use VS if I want to print some out.

there's an option under preferences for using an external editor. After making changes, write/save the file (don't necessarily need to close the editor) and then compile/load using the IDE

the editor can help with some things, but it's best to learnhowto interpret the error msgs.

I did not know this; thanks!

I did think ide v2.x did this for you, but doesn’t seem to .

Just a happy half hour and found the error - a left over *\ …..

more modern editors will color code text making it easier to see comments

I use platformIO with VSCode. I can write code, build the project and debug it all from inside VScode

Note that this is specific to Arduino IDE 1.x. Like most modern applications, Arduino IDE 2.x does this transparently. As long as the sketch is in a saved state, Arduino IDE 2.x will automatically pick up any changes you make to the files externally, without any need to configure it to do so.

It does have a language-server based diagnostic capability, as we would get from VS Code (when the relevant extension is installed) or other professional IDEs.

However, this is disabled by default. We decided to do this because the diagnostics are prone to false positives. Those false positives are likely to cause confusion for beginners. The primary cause of false positives is the deferred library discovery system used by the language server:

The "context-aware" features (e.g., autocomplete, "IntelliSense", suggestions, "Go to Definition") only work for objects from a library after one of these events occurs following the time of the addition of the #include directive for a library's header file in a sketch:

  • The sketch is opened in Arduino IDE
  • A different board is selected in the Arduino IDE menus
  • The sketch is compiled

The reason for the deferred awareness of objects from libraries compared to the awareness of other objects in the sketch (which is updated after every edit to the code) is that the awareness of library objects is dependent on a process known as "library discovery", where Arduino IDE scans all installed libraries for a header files matching the #include directives of the sketch program and picks which libraries to add to the compiler's "search path". That "library discovery" process is somewhat resource intensive. The overhead of that resource usage is insignificant when it is done as part of the occasional manually triggered compile operation, but more significant when it comes to the continuous processing that is done to support the "context-aware" features. The developers decided to change to this "deferred discovery" approach for the "context-aware" feature support in response to user complaints that the previous approach was too heavy for lower spec PCs.

So this means that, until a library discovery is triggered, false diagnostics would be shown for any reference to an object from a library.

It is possible to enable the "problems" feature via the Arduino IDE advanced settings. Even though it might not be suitable for a new user, a more experienced user has the ability to evaluate each of the problem detections and differentiate the legitimate ones from the spurious ones that should be ignored. I'll provide instruction you can follow to enable the feature:

  1. Press the Ctrl+Shift+P keyboard shortcut (Command+Shift+P for macOS users) to open the "Command Palette".
    A menu will appear on the editor toolbar:
  2. Select the "Preferences: Open Settings (UI)" command from the menu.
    You can scroll down through the list of commands to find it or type the name in the field.
    A "Preferences" tab will open in the Arduino IDE main panel.
  3. Type arduino.language.realTimeDiagnostics in the "Search Settings" field of the "Preferences" tab.
  4. Check the box under the "Arduino › Language: Real Time Diagnostics" setting.
  5. Close the Preferences tab by clicking its X icon.

You will now find that a "squiggle" appears under each "problem" detected by the language server.

You can open a "Problems" view that lists all the diagnostics by running the "Toggle Problems View" command from the command palette.


Note that, in addition to the expected false positives that occur due to library discovery not yet having been performed, there are other causes of false positives. Some of these spurious detections are the result of corner case mismatches in behavior between the Clang C++ compiler used by the language server that produces the diagnostics and the GCC compiler that is used to compile the sketch programs.

So you definitely must be prepared to evaluate the diagnostics and ignore those that don't actually represent a problem.

You can easily do the same - write the code in VSCode, compile and upload Arduino sketches - without Platformio. There are several VSCode extensions for work with Arduino code.
I should noted that it is my preferred way to work with big Arduino projects.

Thanks for all the replies , a few things for me to look at and study.