Running Your First Program

Opening Files in Visual Studio Code

  • Once you have Visual Studio Code running, click the file explorer icon in the top left (#1 in the image below). This will open the file explorer on the left side of the screen. From this you can view the files in your project.

  • Expand the src folder to see the files in your project. We will use a different file in this folder for each activity we do today.
  • Double-click on the file named main.cpp to open it in the editor. This program contains the main() function, which is the first function that runs when you upload your code to the board.
  • Look at the main() function (#2 in the image above). You will see that right now it runs the function test_program(). All of the other functions are commented out using // at the start of each line. This means that they will not run when you upload your code to the board. In later activities, you will come back to this file, comment out the test_program() function, and uncomment the activity function you want to run. For now, leave it as is.

Compiling and Uploading

In order to check that your code doesn’t have any mistakes that will prevent it from running, it has to be checked by a special program called a compiler. The compiler takes the code you wrote and transforms it into instructions that the computer chip can understand.

You should now compile and upload the test program to your Y-Board:

  • Click the right arrow button at the bottom of the screen or select “Upload” from the dropdown in VS Code.

If your code compiles correctly and uploads to the board, you should see something like this. Note the green SUCCESS text.

The board should be programmed with the test program, that will show the status of the buttons, switches and knob on the screen. If you press and hold the center button, you can record a short audio clip, and it will it back when you release the button. It should look like this:

Compile Errors

Compiling and uploading can fail for many reasons. For example:

In the above example, the second Yboard.set_led_color() statement is missing a semicolon at the end. Fortunately, the compiler detects this and provides a helpful error message. The red error message in compiler log states: “src/main.cpp:29:40: expected ‘;’ before ‘Yboard’”. The src/main.cpp indicates that the problem is in the main.cpp file, and the 29:40 means that the problem is on line number 29, and the 40th character. Notice that there is a red squiggle in the code where the error is too.

Another common error you might run into is when you misspell a function or variable name. Function names must be spelled perfectly to compile correctly. Code is also case-sensitive, so capitalization matters! The code below has an error, although it is easy to miss. One Yboard.set_led_color() statement has a capitalized ‘s’ that should be lower-case. Can you spot the error?

Just because your code compiles without error, doesn’t mean that it does what you are expecting. Compiling and uploading without error just means that your code contains valid instructions for the computer to execute. The instructions may not actually do what you are try to accomplish.

What If My Board Isn’t Working

Sometimes your board might get stuck in a bad state, and either you won’t be able to upload your code. If your board is stuck, you can reset it by following these steps:

  1. Turn off the PWR switch.

  2. While holding down the button labeled BOOT, turn on the PWR switch.

  3. Let go of the BOOT button.

  4. Try uploading your code again.