Visual Studio Code for Arduino Basics

I'm partway through a robots project using Arduino and Processing. While getting into OpenCV I managed to cock-up my Python installation so badly on my Mac that I just trashed the whole machine and started again. Having come back into Arduino and Processing after a long break I decided not to persist with the rudimentary IDEs that each provides. Visual Studio Code time!

Software Installation

I'm working on a Mac so this wouldn't historically be platform on which to use Visual Studio. But Microsoft is more enlightened these days and Visual Studio Code is available for Windows, Mac and Linux. So I just downloaded and installed it. Done.
Arduino and Processing don't have the polished installation process of VS Code. Download, unzip and then copy the .app into /Applications. Having "installed" these, you're still banned from using them so after trying to open each, go to System Preferences > Security & Privacy and "Open Anyway".

Visual Studio Code Extensions

In Visual Studio Code we need to install the extensions for both Arduino and Processing. The fifth button down the left side icon menu gets to extensions. Search both Arduino and Processing and install them. Easy.

Arduino & Processing Configuration

While Arduino worked with VS Code out of the box, Processing didn't. Processing needs to be runnable from a terminal and it isn't out of the box. I had to run the Processing IDE and from the Tools menu select Install "processing-java". Then in a terminal I could run:

 ➜ ~ processing-java  
   
 Command line edition for Processing 0269 (Java Mode)  
   
 --help        Show this help text. Congratulations.  
   
 --sketch=<name>   Specify the sketch folder (required)  
 --output=<name>   Specify the output folder (optional and  
            cannot be the same as the sketch folder.)  
   
 --force       The sketch will not build if the output  
            folder already exists, because the contents  
            will be replaced. This option erases the  
            folder first. Use with extreme caution!  
   
 --build       Preprocess and compile a sketch into .class files.  
 --run        Preprocess, compile, and run a sketch.  
 --present      Preprocess, compile, and run a sketch in presentation mode.  
   
 --export       Export an application.  
 --no-java      Do not embed Java. Use at your own risk!  
 --platform      Specify the platform (export to application only).  
            Should be one of 'windows', 'macosx', or 'linux'.  
   
 The --build, --run, --present, or --export must be the final parameter  
 passed to Processing. Arguments passed following one of those four will  
 be passed through to the sketch itself, and therefore available to the  
 sketch via the 'args' field. To pass options understood by PApplet.main(),  
 write a custom main() method so that the preprocessor does not add one.  
 https://github.com/processing/processing/wiki/Command-Line  
   
 ➜ ~  

Visual Studio Code Project Configuration

I didn't find the process of using VS Code for Arduino intuitive at all. So here's a stumble-through:
  1. Visual Studio Code has this thing called the Command Palette. It's a search box to find functionality by name. OK. Shift-Command-P and search "Arduino". One of the options is "Arduino: Initialize". Cool, hit that. Result: Please open a folder first.
  2. From the File menu, use Open and in the File Open dialog create a folder for the project. Try to Arduino: Initialize again? No *.ino sketch file was found or selected, so skip initialize command.
  3. OK, so File, New File, then File, Save as a .ino file. So here I violated normal Arduino procedure and named the sketch differently from the project folder name. Initialize again? The workspace is initialized with the Arduino extensions support.
Now above the editor pane there are buttons for Arduino: Verify and Arduino: Upload. Nice. In the Explorer we can see under the .vscode node that arduino.json has been added, with the content:

 {  
   "sketch": "sketch.ino"  
 }  

So if I now populate the code file with the skeleton of a sketch how do we get on?

 void setup() {  
   
 }  
   
 void loop() {  
     
 }  

If I try to verify this code I get: Please select the board type first. But I realise there's now a set of options along the footer of the application.

Selecting my Mega 2560 from the list I now see a new configuration file c_cpp_properties.json, with the content:
 {  
   "configurations": [  
     {  
       "name": "Mac",  
       "includePath": [  
         "/Applications/Arduino.app/Contents/Java/tools/**",  
         "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/**"  
       ],  
       "forcedInclude": [  
         "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"  
       ],  
       "macFrameworkPath": [  
         "/System/Library/Frameworks",  
         "/Library/Frameworks"  
       ],  
       "intelliSenseMode": "clang-x64",  
       "compilerPath": "/usr/bin/clang",  
       "cStandard": "c11",  
       "cppStandard": "c++17"  
     }  
   ],  
   "version": 4  
 }  

So with the default configuration we should be right to compile, yeah? No. Autocomplete is marking the code with red squiggles already.
Can't find avr/pgmspace.h. So where is this little fella?

 ➜ ~ find /Applications/Arduino.app -name "pgmspace.h"               
 /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/pgmspace.h  
 ➜ ~   

Having done lots of stuffing around with the includePath in c_cpp_properties.json I've learned it's important that the error message shows the file that can't be found is avr/pgmspace.h. Since the avr folder is defined, we need to ensure the includePath includes not the folder containing pgmspace.h but one folder back. So I added to the includePath section so that it now looks like this:

       "includePath": [  
         "/Applications/Arduino.app/Contents/Java/tools/**",  
         "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include",  
         "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/**"  
       ],  

With this change implemented the error is gone. Great! Can we verify now? Yes!

OK, so let's fill out a basic sketch and run it. Ugh! What now?


So I tried a lot of things to fix this one. Eventually I found this post that held the answer. Adding a "defines" section to the config fixed this complaint.

 {  
   "configurations": [  
     {  
       "name": "Mac",  
       "includePath": [  
         "/Applications/Arduino.app/Contents/Java/tools/**",  
         "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include",  
         "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/**"  
       ],  
       "forcedInclude": [  
         "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"  
       ],  
       "macFrameworkPath": [  
         "/System/Library/Frameworks",  
         "/Library/Frameworks"  
       ],  
       "intelliSenseMode": "clang-x64",  
       "compilerPath": "/usr/bin/clang",  
       "cStandard": "c11",  
       "cppStandard": "c++17",  
       "defines": [  
         "USBCON"  
       ]  
     }  
   ],  
   "version": 4  
 }  

So, code now contains basic verifiable functionality and is not throwing errors or warnings. When I verify it completes without error. If I try to upload I get: Serial port is not specified. Do you want to select a serial port for uploading? Answering yes and then picking the appropriate port from the box and trying again I get: [Done] Uploaded the sketch: sketch.ino. Excellent!

Now, can I get the serial monitor somewhere? Hitting the "plug" icon in the blue footer bar (next to serial port name) connects the serial monitor, resets the Arduino and shows the output. Awesome!
I think that's the basics. Not as easy as the Arduino IDE but has waaaaay more capability. I'm looking forward to a more sophisticated IDE.

Workspaces

Visual Studio Code supports Workspaces. If you've used the full Visual Studio you might see these as a necessary feature of every project, but they're not. You'd only use one if you needed to manage a project with multiple root folders. More here from jabacchetta on stackoverflow.

That is all.

Comments

  1. OMG , thanks a ton! I thought I was doomed to a lifetime using the Arduino IDE!

    ReplyDelete
  2. Excellent idea!!! I really enjoyed reading your post. Thank you for your efforts. Share more like this.
    Robot Framework Online Course
    Python Online Course

    ReplyDelete

Post a Comment