18
delay(1000);
digitalWrite(2,LOW); // turn LED off
delay(1000);
}
Here is the same using a symbol to define "LED"
#define LED 2 // define the LED pin
void setup()
{
pinMode(LED,OUTPUT);
}
void loop()
{
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);
}
Note how the use of symbols reduces the need for comments. Symbols are extremely useful to
define for devices connected to pins because if you have to change the pin that the device
connects to, you only have to change the single symbol definition rather than going through the
whole program looking for references to that pin.
7.3 Program Structure
All Arduino programs have two functions, setup() and loop(). The instructions you place in the
startup() function are executed once when the program begins and are used to initialize. Use it to
set directions of pins or to initialize variables. The instructions placed in loop are executed
repeatedly and form the main tasks of the program. Therefore every program has this structure
void setup()
{
// commands to initialize go here
}
void loop()
{
// commands to run your machine go here
}
The absolute, bare-minimum, do-nothing program that you can compile and run is
void setup() {} void loop() {}
The program performs no function, but is useful for clearing out any old program. Note that the
compiler does not care about line returns, which is why this program works if typed all on one
line.