Writing your first Arduino Program
Blinking an LED
Arduino
has on-board LED at PIN13, which can be used for testing purpose. The
program for blinking an LED for 1 second, can be written with the
following methods.
void setup()
{
pinMode(13, OUTPUT); //Set the PIN 13 as output.
}
void loop ()
{
digitalWrite(13, HIGH); //Set PIN 13 High.
delay(1000); //Wait for 1 sec.
digitalWrite(13, LOW); //Set PIN 13 Low.
delay(1000); //Wait for 1 sec.
}
You can give a name to the PIN no.-
- By declaring a variable to the PIN no.
int LED=13;
void loop ()
#define LED 13
void setup()
{
pinMode(LED, OUTPUT); //Set the PIN 13 as output.
}
void loop ()
{
digitalWrite(LED, HIGH); //Set PIN 13 High.
delay(1000); //Wait for 1 sec.
digitalWrite(LED, LOW); //Set PIN 13 Low.
delay(1000); //Wait for 1 sec.
}
- Or by defining the pin-
#define LED 13
#define TIME 1000
void setup()
{
pinMode(LED, OUTPUT); //Set the PIN 13 as output.
}
void loop ()
{
digitalWrite(LED, HIGH); //Set PIN 13 High.
delay(TIME); //Wait for 1 sec.
digitalWrite(LED, LOW); //Set PIN 13 Low.
delay(TIME); //Wait for 1 sec.
}
:Where-
#define is a directive command which tells the compiler that, whenever
the terms LED, and TIME come, replace them by 13 and 1000 respectively.
Comments
Post a Comment