LilyPad ProtoSnap Plus Activity Guide

Pages
Contributors: MikeGrusin, Gella
Favorited Favorite 1

Troubleshooting

As you begin to edit the example code and write your own programs, you may experience some error messages. This section highlights a few common issues and solutions as you work on your Arduino code.

At the bottom of the Arduino IDE is section with a black background. This is the Debug Window - if something unexpected or incorrect happens and Arduino can’t communicate to your LilyPad USB Plus or run the code you’ve written, an error message will display here. The top of the section will turn orange and a “Copy Error Messages” button will display.

Example Arduino Error

An example of an error displaying in the debug window

Common Error Messages

Error: “Couldn’t find a Board on the selected port”

  • Check that you have the correct port selected.
  • Check that LilyPad USB Plus is selected in the board menu, NOT LilyPad Arduino or LilyPad Arduino USB.
  • Try pressing the board’s reset button after initiating the upload.
  • Check that the LilyPad USB Plus is switched to ON.
  • Check that the USB cable is correctly connected to your computer and LilyPad USB Plus.
  • Check that the USB cable is not a ‘power only’ cable.

Error: “Expected ‘;’ before”

This error happens when there is a missing ; at the end of a statement. Arduino will highlight the line in red in the code window and print a line number in the code around where the error occurred to help you identify where you need to fix something. You can turn on line numbering in Arduino > Preferences.

Semicolon error

In this example, a missing ; after digitalWrite(A5, HIGH) caused the error.

Error: “‘variable’ was not declared in this scope”

Scope errors can happen for a variety of reasons, here are some common things to check:

  • Variable mismatch (spelling, capitalization, spacing differences) when trying to use a variable you have declared in another part of your program
  • Variable was not declared with a type before use.
  • Variable was declared within a function and not globally and is trying to be used outside of that function.

Variable Declaration Error

This error happened because the variable blueLED was mistyped as BlueLED. Arduino did not recognize BlueLED, while it may look similar enough to you, Arduino is case sensitive.

Error: “Expected ‘}’ at end of input”

As you copy and paste to move pieces of your code around or work with nested functions, your code will accumulate a lot of curly brackets. To keep track of nested functions in the example code, we use indented formatting. Each new nested layer of function is indented an addition time to visually organize the code. If one of the opening { or closing } is accidentally deleted or not added, you will get an error.

Closing Brackets Syntax Error

This error in Activity 4 happened because the closing bracket for the for() loop is missing. This can be initially misleading, as Arduino highlighted a closing bracket - this one belongs to the loop. The space above it is where Arduino expected a match for the opening bracket of the for() loop - highlighted in blue.

Keeping Track of Brackets

Arduino has a handy feature built in that highlights the matching curly bracket if you place your cursor after one. You can use this to quickly check that there are no brackets left behind.

Bracket Highlighting

Placing the cursor next to the closing bracket of the for () loop in Activity 4 highlights its matching opening bracket with a blue outline.

Depending on the Arduino IDE version that you have installed, you may also be able to view the function that was associated with the bracket if it is out of frame. In this example, we were using Arduino IDE v1.8.3.

Bracket Highlighting

Placing the cursor next to the closing bracket of the loop() function highlights the function and bracket when it is out of frame.

Semantics Error and Debugging

Why is my code not working even though it compiles?

This sounds like a pretty general question, but it’s likely a semantic error. While the code is able to compile and is free from syntax errors, the code might not be written to do what you intended. Assuming the hardware connections and boards are good, it is possible that:

  • A pin or variable was not initialized correctly.
  • A variable was not calculated and saved correctly.
  • The wrong variable is printing to the Serial Monitor.
  • You are using values outside of an array[].
  • There is a baud rate mismatch.
  • A delay() function is preventing a certain line from executing fast enough.
  • The sequence of code is not executed properly.

The list of reasons why this may be happening can go on depending on the complexity of the project. The simplest method of debugging can be turning on an LED when we reach a certain part of the code. However, the best method of troubleshooting Arduino code is to try to step through using the serial.print() function to debug. If used correctly, the function is more flexible and can indicate that we have entered a line of code.

Maybe you want to print “I entered this function” to the Serial Monitor after pressing a button or when a sensor reaches a certain value. The serial.print() function can also be used to inspect variables in order to know what to expect from a sensor’s output range. The function can also be used to verify calculations. Other environments allow you to step through the code to simulate what may happen without the need for serial.print().

Learn more SparkFun Troubleshooting Tips