Arduino> Structure> Further Syntax>
#define
[Further Syntax]
Description
#define
is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in arduino don't take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.This can have some unwanted side effects though, if for example, a constant name that had been #defined is included in some other constant or variable name. In that case the text would be replaced by the #defined number (or text).
In general, the const keyword is preferred for defining constants and should be used instead of #define.
Syntax
#define constantName value
Parameters
constantName
: the name of the macro to define.value
: the value to assign to the macro.Example Code
#define ledPin 3
// The compiler will replace any mention of ledPin with the value 3 at compile time.
Notes and Warnings
There is no semicolon after the #define statement. If you include one, the compiler will throw cryptic errors further down the page.
#define ledPin 3; // this is an error
Similarly, including an equal sign after the #define statement will also generate a cryptic compiler error further down the page.
#define ledPin = 3 // this is also an error
-------------------------------------------------------------------#include
[Further Syntax]
Description
#include
is used to include outside libraries in your sketch. This gives the programmer access to a large group of standard C libraries (groups of pre-made functions), and also libraries written especially for Arduino.The main reference page for AVR C libraries (AVR is a reference to the Atmel chips on which the Arduino is based) is here.
Note that
#include
, similar to #define
, has no semicolon terminator, and the compiler will yield cryptic error messages if you add one.Example Code
This example includes the Servo library so that its functions may be used to control a Servo motor.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
-------------------------------------------------------------------/* */
[Further Syntax]
Description
Comments are lines in the program that are used to inform yourself or others about the way the program works. They are ignored by the compiler, and not exported to the processor, so they don't take up any space in the microcontroller’s flash memory. Comments' only purpose is to help you understand (or remember), or to inform others about how your program works.
The beginning of a block comment or a multi-line comment is marked by the symbol
The beginning of a block comment or a multi-line comment is marked by the symbol
/*
and the symbol */
marks its end. This type of a comment is called so as this can extend over more than one line; once the compiler reads the /*
it ignores whatever follows until it enounters a */
.Example Code
/* This is a valid comment */
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
(Another valid comment)
*/
/*
if (gwb == 0) { // single line comment is OK inside a multi-line comment
x = 3; /* but not another multi-line comment - this is invalid */
}
// don't forget the "closing" comment - they have to be balanced!
*/
Notes and Warnings
When experimenting with code, "commenting out" parts of your program is a convenient way to remove lines that may be buggy. This leaves the lines in the code, but turns them into comments, so the compiler just ignores them. This can be especially useful when trying to locate a problem, or when a program refuses to compile and the compiler error is cryptic or unhelpful.
-------------------------------------------------------------------//
[Further Syntax]
Description
Comments are lines in the program that are used to inform yourself or others about the way the program works. They are ignored by the compiler, and not exported to the processor, so they don't take up any space in the microcontroller’s flash memory. Comments' only purpose is to help you understand (or remember), or to inform others about how your program works.
A single line comment begins with // (two adjacent slashes). This comment ends automatically at the end of a line. Whatever follows // till the end of a line will be ignored by the compiler.
A single line comment begins with // (two adjacent slashes). This comment ends automatically at the end of a line. Whatever follows // till the end of a line will be ignored by the compiler.
Example Code
There are two different ways of marking a line as a comment:
// pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Notes and Warnings
When experimenting with code, "commenting out" parts of your program is a convenient way to remove lines that may be buggy. This leaves the lines in the code, but turns them into comments, so the compiler just ignores them. This can be especially useful when trying to locate a problem, or when a program refuses to compile and the compiler error is cryptic or unhelpful.
-------------------------------------------------------------------;
[Further Syntax]
Description
Used to end a statement.
Example Code
int a = 13;
Notes and Warnings
Forgetting to end a line in a semicolon will result in a compiler error. The error text may be obvious, and refer to a missing semicolon, or it may not. If an impenetrable or seemingly illogical compiler error comes up, one of the first things to check is a missing semicolon, in the immediate vicinity, preceding the line at which the compiler complained.
-------------------------------------------------------------------{}
[Further Syntax]
Description
Curly braces (also referred to as just "braces" or as "curly brackets") are a major part of the C programming language. They are used in several different constructs, outlined below, and this can sometimes be confusing for beginners.
An opening curly brace "{" must always be followed by a closing curly brace "}". This is a condition that is often referred to as the braces being balanced. The Arduino IDE (integrated development environment) includes a convenient feature to check the balance of curly braces. Just select a brace, or even click the insertion point immediately following a brace, and its logical companion will be highlighted.
Beginning programmers, and programmers coming to C from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.
Unbalanced braces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a large program. Because of their varied usages, braces are also incredibly important to the syntax of a program and moving a brace one or two lines will often dramatically affect the meaning of a program.
An opening curly brace "{" must always be followed by a closing curly brace "}". This is a condition that is often referred to as the braces being balanced. The Arduino IDE (integrated development environment) includes a convenient feature to check the balance of curly braces. Just select a brace, or even click the insertion point immediately following a brace, and its logical companion will be highlighted.
Beginning programmers, and programmers coming to C from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.
Unbalanced braces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a large program. Because of their varied usages, braces are also incredibly important to the syntax of a program and moving a brace one or two lines will often dramatically affect the meaning of a program.
Example Code
The main uses of curly braces are listed in the examples below.
Functions
Functions
void myfunction(datatype argument) {
// any statement(s)
}
Loops
while (boolean expression) {
// any statement(s)
}
do {
// any statement(s)
} while (boolean expression);
for (initialisation; termination condition; incrementing expr) {
// any statement(s)
}
Conditional Statements
if (boolean expression) {
// any statement(s)
}
else if (boolean expression) {
// any statement(s)
}
else {
// any statement(s)
}
-------------------------------------------------------------------
Post a Comment
Hi Users, if you have any queries then please let me know.