Correcting Arduino Compiler Errors

(Intended) Errors and Omissions

What happens after you select the Arduino menu item Sketch -> Verify/Compile and you get error messages that your program failed to compile properly. Sometimes the compiler warnings help you spot the problem code. Other times the error messages don’t make much sense at all. One way to understand the error messages is to create some intentional errors and see what happens.

Create a new program named: LEDBlink_errors

This is one time when it is better to copy the following code so we don’t introduce more errors into the program than are already there.


/*--- Blink an LED  ---//
//Associate LEDs with an Arduino Digital pin.
//The Arduino already has a built-in LED that we can use on Digital Pin 13.
int ledPin = 23;  \\We're using Digital Pin 23 on the Arduino.

void setup();
{
   pinMode(ledPin OUTPUT);   //Set up Arduino pin for output only.
}

loop()
(
   /The HIGH and LOW values set voltage to 5 volts when HIGH and 0 volts LOW.
   digitalWrite(ledPin, high); //Setting a digital pin HIGH turns on the LED.
   delay(1000):  //Get the microcontroller to wait for one second.
   digitalWrite(ledPin. LOW);  //Setting the pin to LOW turns the LED off.
   Delay(1000);  //Wait another second with the LED turned off.
   }
}

If you run Verify/Compile command you should see a number of compiler error messages at the bottom of the dialog display.

Arduino Compiler Error Messages

Arduino Compiler Error Messages

Line 1 Error

Uncaught exception type:class java.lang.RuntimeException

java.lang.RuntimeException: Missing the */ from the end of a /* comment */

at processing.app.Sketch.scrubComments(Sketch.java:2008) …

/*— Blink an LED  —//

The error messages go on for several more lines without adding much information to help solve the problem. You will find a clue to the problem above the compiler message box stating:

Missing the */ from the end of a /* comment */“.

The article “Introduction to Programming the Arduino” describes the comment styles used by C programs. The error on line 1 is caused by mixing the comment styles. The comment begins with the “/*” characters but ends with “//” instead of the “*/” characters. Correct the line as follows:

/*— Blink an LED  —*/

Now, rerun the Verify/Compile command.

Line 4 Error

error: stray ‘\’ in program

int ledPin = 23; \\We’re using Digital Pin 23 on the Arduino.

This is another problem with incorrect commenting technique. In this line the “\\” characters are used to begin a comment instead of the “//” characters. The correct line follows:

int ledPin = 3;  //We’re using Digital Pin 3 on the Arduino.

Now, rerun the Verify/Compile command.

Line 6 Error

error: expected unqualified-id before ‘{’ token

void setup();

This is an easy mistake to make. The problem is the semicolon “;” at the end of a function declaration. The article “Learning the C Language with Arduino” contains a section about correct usage of semicolons. To correct this problem remove the semicolon as shown below:

void setup()

Now, rerun the Verify/Compile command.

Line 8 Error

In function ‘void setup()’:

error: expected `)’ before numeric constant/home/myDirectory/Desktop/myPrograms/arduino-0015/hardware/cores/arduino/wiring.h:102:

error: too few arguments to function ‘void pinMode(uint8_t, uint8_t)’ At global scope:

pinMode(ledPin OUTPUT); //Set up Arduino pin for output only.

The clue to this problem is found in the message error: too few arguments to function ‘void pinMode(uint8_t, uint8_t)’“. The message includes a list of the function’s arguments and data types (uint8_t). The error is complaining that we have too few arguments. The problem with this line of code is the missing comma between ledPin, and OUTPUT. The corrected code is on the following line:

pinMode(ledPin, OUTPUT); //Set up Arduino pin for output only.

Now, rerun the Verify/Compile command.

Line 11 Error

error: expected constructor, destructor, or type conversion before ‘(’ token

loop()

In this line the type specifier for the function is missing.

To fix this problem place the data type for the function’s return type. In this case we’re not returning any value so we need to add the keyword void in front of the loop function name. Make the following change:

void loop()

Now, rerun the Verify/Compile command.

Line 12 Error

error: function ‘void loop()’ is initialized like a variable

(

The block of code that makes up the loop function should be contained within curly braces “{“ and “}”. In this line a left parenthesis character “(“ is used instead of the beginning curly brace “{“. Replace the left parenthesis with the left curly brace.

Now, rerun the Verify/Compile command.

Line 13 Error

error: expected primary-expression before ‘/’ token At global scope:

/The HIGH and LOW values set voltage to 5 volts when HIGH and 0 volts LOW.

This line is supposed to be a comment describing what the program is doing. The error is caused by having only one slash character “/” instead of two “//”. Add the extra slash character then recompile.

//The HIGH and LOW values set voltage to 5 volts when HIGH and 0 volts LOW.

Line 14 Error

error: ‘high’ was not declared in this scope At global scope:

digitalWrite(ledPin, high); //Setting a digital pin HIGH turns on the LED.

This error message is complaining that the variable “high” was not declared. Programming in C is case sensitive, meaning that it makes a difference if you are using upper or lower case letters. To solve this program replace “high” with the constant value “HIGH” then recompile.

digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.

Line 15 Error

error: expected `;’ before ‘:’ token At global scope:

delay(1000): //Get the microcontroller to wait for one second.

This error message is helpful in identifying the problem. This program statement was ended with a colon character “:” instead of a semicolon “;”. Replace with the proper semicolon and recompile.

delay(1000); //Get the microcontroller to wait for one second.

Line 16 Error

error: expected unqualified-id before numeric constant At global scope:

digitalWrite(ledPin. LOW); //Setting the pin to LOW turns the LED off.

This error can be particularly troublesome because the comma “,” after the variable ledPin is actually a period “.” making it harder to spot the problem.

The C programming language allows you to build user defined types. The dot operator (period “.”) is part of the syntax used to reference the user type’s value. Since the variable ledPin is defined as an integer variable, the error message is complaining about the unqualified-id.

digitalWrite(ledPin, LOW); //Setting the pin to LOW turns the LED off.

Line 17 Error

In function ‘void loop()’:

error: ‘Delay’ was not declared in this scope At global scope:

Delay(1000); //Wait another second with the LED turned off.

This error was caused by the delay function name being spelled with an incorrect upper case character for the letter “d”. Correct the spelling using the lower case “d” and try the Sketch Verify/Compile again.

delay(1000); //Wait another second with the LED turned off.

Line 18 Error

error: expected declaration before ‘}’ token

}

}

There is an extra curly brace at the end of this program. Delete the extra brace to correct this error.

Now, rerun the Verify/Compile command.

Success (or so it seems)

The compiler completed without any more error messages so why doesn’t the LED flash after loading the program on my Arduino?

Line 4 Error

No error message was given by the compiler for this problem.

int ledPin = 23; \\We’re using Digital Pin 23 on the Arduino.

The Digital pins are limited to pin numbers 0 through 13. On line 4 the code is assigning a non-existant pin 23 to the ledPin variable. Change the pin assignment to pin 3 and the program should compile, upload to your Arduino and flash the LED if everything is wired properly on your breadboard.

int ledPin = 3; \\We’re using Digital Pin 3 on the Arduino.

(c) 2009 – Vince Thompson

Tags: , , , , ,

2 Responses to “Correcting Arduino Compiler Errors”

  1. Learning the C Language with Arduino « DIY Robotics Lab Says:

    […] Robotics Lab Bringing Robotics Home « Microcontrollers as Time Keepers Correcting Arduino Compiler Errors […]

  2. hotdog Says:

    Nice, thank you.
    The “stray ‘\’ in program” error can also occurs if there’s an accent (like è) in the name of a function.

Leave a comment