Visual Studio Code help - main sketch file?

I am using VSCode to write some code for my arduino and have what is probably a relatively dumb question...

How do I change the main sketch file?

As in, when you first open the program and hit the "verify" or "upload" button, it gives you a text box to select which file you want to work with. But I want to upload a different file now but am not sure how to change which one the IDE is working with.

The only way I am able to work around this without completely restarting the program is to rename the previous file to "[filename].h" or some other extension that cannot upload to an arduino board and only then it gives me the option to select the main sketch file again.

Thanks, learning a new IDE can be frustrating sometimes >:(

That really sounds like dumb question, maybe you don't describe your problem properly?

Write your code and save it once, then try to upload it to the board of your choice???

It is like any other text editor, so use it like one.

Cheers,
Kari

I'm having the same problem as the OP. Instead verifying and uploading the sketch in the active tab (like the Arduino IDE does), the VSCode Arduino extension is operating on whatever file is referred to in a line of .vscode/arduino.json constructed as "sketch": "examples/01.Basics/Blink.ino". The only way I have found to verify or upload a different sketch is to edit or delete the arduino.json file.

I'm running VSCode on Linux Mint 19 Cinnamon 64-bit, using the Microsoft Arduino extension. Here are some of the steps of how the problem happens:

  1. Delete the .vscode/arduino.json file in my Arduino workspace to start fresh
  2. Open VSCode
  3. Open an arduino sketch (sketch A)
  4. Select board and serial port
  5. Click on "verify" -- a message pops up asking me to "Select the main sketch file", with an unorganized dropdown menu of every single sketch that is in any of the workspace folders.
  6. I select the filename of sketch A, designating a "main sketch file", and it verifies the sketch
  7. Open another sketch (sketch B)
    8 ) Click on "verify"
    PROBLEM: VSCode verifies sketch A, but I want it to verify sketch B.

I can't figure out how to tell VSCode that I want to verify the sketch I am editing, rather than the one I first selected. The dropdown list does not even appear a second time.

Other than editing the json file, does anyone know how to select a different sketch?

Thanks,
Abe

I've just spent two hours with this same problem (long-time Arduino IDE user, new VScode user).

The OP was told that it sounded like a dumb question..but what's the answer? I'm stuck too...

The simplest solution I found is to save each file in a different Workspace.
Then you can switch the Workspace when you like to upload the other file.

More on VS Code Workspaces on stackoverflow.


There is no such thing as a stupid question, only stupid answers

1 Like

It is no dumb question IMO... (there is no such thing as dumb question anyway :wink:

Also struggling with this. I am working on 2 sketches simultaneously for 2 arduinos communicating thru I2C. Switching from one to the other is just a pain.

I have to

  • swap the sketch (did not find any efficient way)
  • change the COM port
  • and possibly changing the board type

I often end up uploading the sketch to the wrong arduino or compile for the wrong board :frowning:

I'll try the multiple workspaces trick.
May be also the multi root workspace would be helpful.

JS

GaryP:
That really sounds like dumb question, maybe you don't describe your problem properly?

Wow, that's a bit harsh.

In VSCode, the 'Main' or 'Workspace' .ino file that will get uploaded should be listed at the VERY bottom of the VS Code window. As in the very thin anchor toolbar (Mine is background in blue and shows current setup board, com port, path to the sketch .ino file, plus a few other pieces of info). Click that .ino file and you'll get that popup box that you first had in the beginning. Paste in your new file path, and you're set.

Also - If you right click the tab of the file you want to be the new 'Main' .ino file, there is a dropdown for 'Copy Relative Path'. So right click and copy the relative path of the file you want, click the main sketch file in the footer, and paste into the popup box.

1 Like

I have 2 sketches in the same workspace. I changed the filename down in the footer, but it still compiles the other sketch. I even tried changing it in the vscode.json file, but it got changed straight back. Any ideas?

I agree that the solution posted by markr959 above does not work as described, at least on OSX.

I spent too much time struggling with this. So my temporary solution is to use VScode for my main programming tool, to develop and upload larger projects which utilize a single sketch. However, I've gone back to the Arduino IDE for quick and easy uploading of multiple different sketches, quick tests of example code, analyzing sketches downloaded from web pages, etc.

I'm aware of the advantages of a workspace-based IDE, but for quick tests the Arduino IDE is more efficient. It would be nice if VScode generated a default arduino.json file for each new sketch loaded, but it doesn't...that file has to be manipulated for each new example or new sketch. Am I wrong?

I have yet to see someone describe a way to make VScode work as quickly and easily as the Arduino IDE for quickly moving among new or existing different sketches, and uploading them for quick operational analysis...but I would love to get that answer...

So the second solution proposed by markr959 does work on OSX(i just tried it), basically left clicking on the current tab and copying the relative path from the drop down menu, then clicking on the sketch button at the bottom of the ide (between 'select programmer' and 'select board config') and pasting in the relative address. That will instantly change which sketch is verified/uploaded. But it's not great and seems like some that could be an easy fix and would really improve usability.

Yeah, your workaround is what I've been doing also, but instead of [filename].h, I just remove the file extension in the vscode explorer, which automatically makes it a .txt file. It would still retain the language mode to C++ and will automatically create a settings.json for language association to .txt file.

Once you have the settings.json file in your workspace, you can make a new file and create a sketch as txt, if you want a scratchpad to copy paste to your main .ino file.

I haven't seen another way of quickly uploading/verifying a different sketch in a workspace, but I think by just removing the file extension is easier and faster.


Now, I don't know why old timers in this forum are so disrespectful, I guess they just want to accumulate post count even though they don't really have anything useful to say and contribute.. :roll_eyes:

Agreed, it's awkward.

I've tried a number of ways to do this, including the tips posted here, but either it doesn't work as expected or something screws up and it makes a lot more work than expected to keep the old project intact.

So now I just go to the desktop, clone the project folder, give it a new name, open the clone as a new project, and tweak the settings.json to reflect the new name. Now the previous version of the project is safely preserved and I'm working on the new one.

It's a hassle but it's a workflow that doesn't seem to have the "gotchas" that leave behind a messed up project.

A simple "Save Project As..." that worked as expected would be nice.

In the .vscode/arduino.json file the line beginning with "sketch" contains the main sketch file. You can update it manually, but it's much easier to simply delete this line, go to .ino file and select Verify or Upload option. VSCode will then offer a list of files to choose the new main sketch file.

1 Like