TROUBLESHOOTING SOLUTIONS: CODE PROBLEMS, COMPILE ERRORS

Compile errors are errors that happen when you attempt to compile your code and the Arduino software (the compiler) finds an error in it. If you receive an error when you attempt to compile and upload your code, you know you have either a compile error or an upload error.

symptoms
If you’re not sure if you have a compile error or an upload error, scroll to the top of the black area at the bottom of the Arduino window. If you see the name of your program followed by .ino in orange text at the top of the box, you know you have a compile error (below, top). If you see a message in white text at the top of the box that looks something like “Binary sketch size: 4,962 bytes (of a 30,720 byte maximum)” you know that you have an upload error (below, bottom).

COMPILE ERROR:

A FUNCTION-DEFINITION IS NOT ALLOWED HERE BEFORE ‘{’ TOKEN
Blink.ino: In function ‘void setup()’:
Blink:19: a function-definition is not allowed here before ‘{’ token
Blink:24: error: expected ‘}’ at end of input

 

UPLOAD ERROR:

SERIAL PORT ‘/DEV/TTY.BLUETOOTH-MODEM’ ALREADY IN USE. TRY QUITTING ANY PROGRAMS THAT…
Binary sketch size: 4,962 bytes (of a 30,720 byte maximum)
processing.app.SerialException: Serial port ‘/dev/tty.Bluetooth-Modem’ already in use. … uiting any programs that may at processing.app.Serial.(Serial.java:171)
at processing.app.Serial.(Serial.java:77)

 

finding compile errors in your code
Compile errors often occur because of missing or incorrect punctuation, misspellings, and mis-capitalizations. They will also occur if you attempt to use a variable you have not initialized at the top of your program or if there is extra text anywhere in your program. If you get a compile error, begin by carefully reading the error messages in the feedback area of the Arduino window for clues. Investigate your code around the location that the Arduino software highlights or jumps to. If you cannot see any problems in that area, carefully read through your entire program line by line, looking for the problems described in this section.

the rest of this section
The next few pages explore common code mistakes (the ones listed below) and the compile errors they generate, along with tips on how to find and fix these problems.

Each section begins with examples of problematic pieces of code. The location of the problem is highlighted in yellow. Below each code example is an example of the kind of error you might receive if you made a similar mistake in your code. This is followed by advice on finding and fixing similar errors.

COMPILE ERRORS COVERED IN THIS SECTION
missing semicolons
missing curly brackets
missing parentheses
missing commas
misspellings and mis-capitalizations
missing variable initializations
random extra text in program

 

MISSING SEMICOLONS ;

symptoms
Error messages for missing semicolons are relatively straightforward. In each error message, either in the orange or the black feedback area, there is a line that says: error: expected ‘;’ before… This is an indication that you’re missing a semicolon. The Arduino software will usually highlight the line immediately after the missing semicolon to indicate the location of your error.

int led = 13_

void setup() {
    pinMode(led, OUTPUT);
}
EXPECTED UNQUALIFIED-ID BEFORE NUMERIC CONSTANT
Blink:11: error: expected unqualified-id before numeric constant
Blink:13: error: expected ‘,’ or ‘;’ before ‘void’

 

void loop() {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000)_
}
EXPECTED ‘,’ OR ‘;’ BEFORE ‘}’ TOKEN
Blink.ino: In function ‘void loop()’:
Blink:24: error: expected `;’ before ‘}’ token

 

if (touchValue > 1)
{
    tone(speaker, C)_
    delay(100);
}
EXPECTED ‘,’ OR ‘;’ BEFORE ‘DELAY’
piano.ino: In function ‘void checkPianoKey(int, int)’:
piano:37: error: expected `;’ before ‘delay’

 

FIX MISSING SEMICOLONS ;

Look for instances of error: expected ‘;’ in your error. Look for similarities between your error and the ones above. Replace the missing semicolon and recompile your code. If the missing semicolon was the only compile error in your program, your code will now compile. If you have additional errors, you will get a new error message with information about the next error in your program.
 

MISSING CURLY BRACKETS { }

symptoms
These error messages are often confusing and cryptic. Most will say something about either a ‘{’ or ‘}’ somewhere in the orange or black feedback area. The Arduino software will sometimes highlight the line immediately after the missing curly bracket to indicate the location of your error. Sometimes, unfortunately, it will highlight a completely unrelated line.

void setup() {
    pinMode(led, OUTPUT);
_
A FUNCTION-DEFINITION IS NOT ALLOWED HERE BEFORE ‘{’ TOKEN
Blink.ino: In function ‘void setup()’:
Blink:19: a function-definition is not allowed here before ‘{’ token
Blink:24: error: expected ‘}’ at end of input

 

void setup()_
    pinMode(led, OUTPUT);
}
EXPECTED INITIALIZER BEFORE ‘PINMODE’
Blink:16: error: expected initializer before ‘pinMode’
Blink:17: error: expected declaration before ‘}’ token

 

if (touchValue > 1)
_
    tone(speaker, C);
    delay(100);
}
EXPECTED UNQUALIFIED-ID BEFORE ‘ELSE’
piano:18: error: expected unqualified-id before ‘else’
piano:19: error: expected unqualified-id before ‘else’
piano:20: error: expected unqualified-id before ‘else’
piano:21: error: expected unqualified-id before ‘else’
piano:39: error: expected unqualified-id before ‘else’
piano:43: error: expected declaration before ‘}’ token

 

FIX MISSING CURLY BRACKETS { }

Look for mentions of ‘{’ or ‘}’ in your errors. Look for similarities between your error and the ones above. Replace the missing bracket and recompile your code. If you have no additional compile errors in your code, your code will compile successfully, otherwise you’ll get a new error message with information about the next error in your program.
 

MISSING PARENTHESES ( )

symptoms
These error messages are often confusing and cryptic. Some, but not all, will mention a ‘(’ or ‘)’ token in either the orange or black feedback area. The Arduino software will sometimes highlight the line with the missing parenthesis to indicate the location of your error, but sometimes it will highlight an unrelated line.

void setup(_{
    pinMode(led, OUTPUT);
}
VARIABLE OR FIELD ‘SETUP’ DECLARED VOID
Blink:14: error: variable or field ‘setup’ declared void
Blink:14: error: expected primary-expression before ‘{‘ token

 

void loop() {
    digitalWrite(led, HIGH);
    delay(1000_;
    digitalWrite(led, LOW);
    delay(1000);
}
EXPECTED ‘)’ BEFORE ‘;’ TOKEN
Blink.ino: In function ‘void loop()’:
Blink:22: error: expected `)’ before ‘;’ token

 

void checkPianoKey (int key, int note_{
    touchValue = readCapacitivePin(key);
    Serial.print(touchValue);
    Serial.print("\t");
...
‘CHECKPIANOKEY’ WAS NOT DECLARED IN THIS SCOPE
piano.ino: In function ‘void loop()’:
piano:23: error: ‘checkPianoKey’ was not declared in this scope
piano.ino: At global scope:
piano:30: error: expected `)’ before ‘{‘ token

 

FIX MISSING PARENTHESIS ( )

Look for mentions of ‘(’ or ‘)’ in your errors. Look for similarities between your error and the ones above. Replace the missing parenthesis and recompile your code. If you have no additional compile errors in your code, your code will compile successfully, otherwise you’ll get a new error message with information about the next error in your program.
 

MISSING COMMAS ,

symptoms
These error messages are often confusing and cryptic. Very few will mention a comma in the orange or black feedback area. The Arduino software will usually highlight the line with the missing comma to indicate the location of your error.

void setup() {
    pinMode(led_OUTPUT);
}
EXPECTED `)’ BEFORE NUMERIC CONSTANT
Blink.ino: In function ‘void setup()’:
Blink:16: error: expected `)’ before numeric constant
/Applications/Arduino.app/Contents/Resources/Java/…
Blink:16: error: at this point in file

 

while (i < numberOfKeys)
{
    checkPianoKey(keys[i]_notes[i]);
    i = i+1;
}
EXPECTED ‘)’ BEFORE ‘NOTES’
piano.ino: In function ‘void loop()’:
piano:23: error: expected `)’ before ‘notes’
piano:4: error: too few arguments to function
‘void checkPianoKey(int, int)’
piano:23: error: at this point in file

 

const int numberOfKeys = 7;
int keys[numberOfKeys] = { 6, 9_10, 11, A2, A3, A4 };
EXPECTED ‘}’ BEFORE NUMERIC CONSTANT
piano:2: error: expected `}’ before numeric constant
piano:2: error: expected ‘,’ or ‘;’ before numeric constant
piano:2: error: expected declaration before ‘}’ token

 

FIX MISSING COMMAS ,

Look for similarities between your error and the ones above. Replace the missing comma and recompile your code. If you have no additional compile errors in your code, your code will compile successfully, otherwise you’ll get a new error message with information about the next error in your program.
 

MISSPELLINGS AND MIS-CAPITALIZATIONS

symptoms
Error messages for misspellings and mis-capitalizations are some of the clearest and most helpful you’ll encounter. All take the form of ‘word with error’ was not declared in this scope. The Arduino software will almost always highlight the line with the misspelling or mis-capitalization to indicate the location of your error. Also, if you have misspelled one of the built-in Arduino procedures or variables, the color of the misspelled or mis-capitalized word will change from orange or blue to black, giving you another valuable clue about the cause of the error.

void loop() {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    dellay(1000);
}
‘DELLAY’ WAS NOT DECLARED IN THIS SCOPE
Blink.cpp: In function ‘void loop():
Blink:21: error: ’dellay’ was not declared in this scope

 

void loop() {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, low);
    delay(1000);
}
‘LOW’ WAS NOT DECLARED IN THIS SCOPE
Blink.ino: In function ‘void loop()’:
Blink:23: error: ‘low’ was not declared in this scope

 

if (touchValue < 1000)
{
    Song(2000);
}
‘SONG’ WAS NOT DECLARED IN THIS SCOPE
monster.ino: In function ‘void loop()’:
monster:29: error: ‘Song’ was not declared in this scope

 

FIX MISSPELLINGS AND MIS-CAPITALIZATIONS

Look for errors of the form ‘word with error’ was not declared in this scope. Correct the misspelling or mis-capitalization and recompile your code. If you have no additional compile errors in your code, your code will compile successfully, otherwise you’ll get a new error message with information about the next error in your program.
 

MISSING VARIABLE DEFINITIONS

symptoms
Errors for missing variable definitions are fairly clear. All take the form of ‘missing variable’ was not declared in this scope. The Arduino software will highlight the first line in your program that uses the missing variable. For example, in the code below (top), Arduino will highlight the pinMode(led, OUTPUT); line when you attempt to compile the code since it is the first line that uses the missing variable led.

// missing int led=13; line

void setup()
    pinMode(led, OUTPUT);
}
‘LED’ WAS NOT DECLARED IN THIS SCOPE
Blink.ino: In function ‘void setup()’:
Blink:4: error: ‘led’ was not declared in this scope
Blink.ino: In function ‘void loop()’:
Blink:9: error: ‘led’ was not declared in this scope

 

// missing const int numberOfKeys = 7; line

int keys[numberOfKeys] = { 6, 9, 10, 11, A2, A3, A4 };
‘NUMBEROFKEYS’ WAS NOT DECLARED IN THIS SCOPE
piano:2: error: ‘numberOfKeys’ was not declared in this scope
piano:3: error: ‘numberOfKeys’ was not declared in this scope
piano.ino: In function ‘void setup()’:
piano:11: error: ‘numberOfKeys’ was not declared in this scope

 

int led = A4;
int speaker = 5;
// missing int aluminumFoil=A2; line
int sensorValue;
‘ALUMINUMFOIL’ WAS NOT DECLARED IN THIS SCOPE
monster.ino: In function ‘void setup()’:
monster:18: error: ‘aluminumFoil’ was not declared in this scope
monster.ino: In function ‘void loop()’:
monster:24: error: ‘aluminumFoil’ was not declared in this scope

 

FIX MISSING VARIABLE DEFINITIONS

If you receive errors in the form of ‘missing variable’ was not declared in this scope, check for missing or misspelled variable definitions at the top of your code. Correct or add the necessary definitions. Once you fix the problem, if you have no additional compile errors in your code, your code will compile successfully. Otherwise, you’ll get a new error message with information about the next error in your program.
 

RANDOM EXTRA TEXT IN PROGRAM

symptoms
Errors for extra text in your program can be confusing, but Arduino will usually highlight the line where the extra text is, making these problems easier to find. A common extra text error is a mistyped comment—a comment that is missing a ‘/’, ‘/*’ or ‘*/’. An example of this kind of error is shown below in the middle. Here the comment is missing one of its beginning slash ‘/’ characters. If you have a mistyped comment, its color will be black instead of grey, giving you a clue to the problem.

void loop() {
    digitalWrite(led, HIGH); x
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
}
‘X’ WAS NOT DECLARED IN THIS SCOPE
Blink.ino: In function ‘void loop()’:
Blink:9: error: ‘x’ was not declared in this scope
Blink:10: error: expected `;’ before ‘delay’

 

void setup() {
    / set all keys to be inputs
    int i = 0;
    while (i < numberOfKeys)
    {
    ...
EXPECTED PRIMARY-EXPRESSION BEFORE ‘/’ TOKEN
piano.ino: In function ‘void setup()’:
piano:9: error: expected primary-expression before ‘/’ token
piano:9: error: ‘set’ was not declared in this scope
piano:9: error: expected `;’ before ‘all’
piano:11: error: ‘i’ was not declared in this scope

 

void song(int duration) {
    tone(speaker, C);
    delay(duration);
    tone(speaker, D)k;
    delay(duration);
}
EXPECTED ‘;’ BEFORE ‘K’
monster.ino: In function ‘void song(int)’:
monster:40: error: expected `;’ before ‘k’

 

FIX RANDOM EXTRA TEXT IN PROGRAM

Correct your mistyped comment or remove the extra text from your code. When you recompile, if you have no additional compile errors, your code will compile successfully. If you have additional issues you’ll get a new error message with information about the next thing you need to fix.