Obviously not an expert at this stuff...
I'm trying to get VS Code set up for Arduino (on Windows 10). Right now, I can upload basic samples via VSC and they work fine, but I have problems with sketches I developed in the Arduino IDE.
I believe it's around the library paths but I'm not sure. I'm trying to use the FastLED library and my first include line (for FastLED.h) gets a red underline. The errors are:
cannot open source file "avr/io.h" (dependency of "FastLED.h")C/C++(1696)
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (C:\Users\****\Documents\Arduino\project1\project1.ino).C/C++(1696)
cannot open source file "avr/pgmspace.h" (dependency of "C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Arduino.h")C/C++(1696)
I've found the "avr/io.h" file in the C:\Program Files (x86)\Arduino\hardware\tools... folder and tried adding it to the c_cpp_properties.json file but I'm not having any luck. (I only tried that after some internet searches led me down that path).
Everything works fine via the Arduino IDE.
Can anyone help me out? I can provide more info, but I'm not sure what's relevant...
Are you using the Microsoft provided plugin for Arduino? If so, it is a thin wrapper on the Arduino command line, calling pretty much the same operations as the standard IDE.
Is the issue a compile error where the sketch fails to build or intelli-sense error in the editor?
You may have already done this but: When you load your own sketch into VS code you need to select 'Arduino initialise' from the CTRL-p menu. This creates an Arduino configuration in the sketch directory, and in that file there's configuration for include paths etc.
Although saying that it uses the same operations, I have noticed changes in its behaviour since I upgraded to the latest Arduino IDE version recently. I now get a lot of Java logging I didn't used to get in the build console.
But still, I've not had a sketch that would build in Arduino IDE not build in VS Code. I've got quite a few where I can't get the intelli-sense fully working.
Those look like intellisense error messages from vscode. You likely have intellisense configuration issues. If the code compiles using the IDE then it will still compile when you use the vscode plugin since the plugin uses the ide to do the compile. You can optionally configure vscode build tasks and use the arduino cli.
If you want vscode to properly show the errors so you need to configure the vscode intellisense c_cpp_properties.json. It is important to understand that this file needs to be configured with the include paths and the proper build defines so that vscode lints the code properly.
If you are missing an include path, vscode cannot find it because it doesn't know the paths to the include files unless it is specified in the include paths. So, every include statement in your source files must have an include path in c_cpp_properties.json. I have found that recursive paths does not work for the arduino libraries folder for some reason. So, you need to specify each path individually. Here is a sample:
Equally important is the linting requires that the defines for your build environment are properly specified as well. Here is a sample of the defines I use for a MKR WAN board:
You should also force include "Arduino.h":
And finally, use the arm compiler for linting as follows:
Generally speaking, this configuration works pretty well for me.
Many Thanks @sslupsky that's very helpful. I'm running on Windows and kind of put up with a few intelli-sense problems for a while.
To be honest I use VS with visual micro for heavier development. But VS code is really good for testing libraries because it's really easy to switch between examples and tests.
I usually get the header paths right but have never tried the compiler options before. I'll give it a try.
...You may have already done this but: When you load your own sketch into VS code you need to select 'Arduino initialise' from the CTRL-p menu. This creates an Arduino configuration in the sketch directory, and in that file there's configuration for include paths etc.
Thank you!! I guess I need to learn more about VSC (or programming in general?). I was only working with a "file" and I guess it was trying to use a global json file? Anyway, I did an "open folder" instead of "open file", then did the "initialize" (which I had no idea that I had to do), and now everything works (and I have a .vscode folder in my project folder too).
Thanks again, I was really getting discouraged, but now I'm moving forward again!!!