Further Syntax
#define
Allows the programmer to assign a name to a constant value.
Example
1
2
3
|
#define ledPin 3
// The compiler will replace any mention of ledPin with the value 3 at compile time.
|
#include
it is used to include external libraries into arduino program
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#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
}
}
|
Block comments are written inside these symbols: /* */
Example
/* This is a valid comment */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/*
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!
*/
|