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.
Uploading
In order to compile your code and upload it to the 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.
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, or your code will upload but won’t run.
Note: Before following the next steps to reset your board, make sure the problem isn’t actually in your code! It’s easy to think that the board isn’t working when it’s really a bug in your code that’s causing it to get stuck. A good thing to try is to turn an LED on or off at the start of your code to see if it works. Also, double check main.cpp
to make sure the correct function is being called.
If your board is stuck, you can reset it by following these steps:
-
Unplug the board from your computer.
-
Hold down the button labeled “BOOT1” and the button labeled “RESET” at the same time, while plugging the board back into the computer.
-
Let go of both buttons and wait 10-15 seconds before attempting to upload your code again.
-
Repeat as needed.