An Arduino Sketch That will Make your LED Blink & Much More than That!

I think you know that a source code or program in Arduino Programming Language, is known as sketch. Told to you so you dont get confused. LED blinking program is considered to be the Hello World Program for various Microcontrollers, and for Arduino here it is!

/*
Blinking an LED, by turning on an LED on for half a second, then off for another half of the second, continuously forever.
This example code is in the public domain.
*/


// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led=13;
// the setup routine runs once when you press reset:
void setup() {
// initializing the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for 500 milliseconds, or half a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for 500 milliseconds, or half a second
}
//End of the program

LED Blinking With Arduino
LED Blinking With Arduino ( A simple connection indeed, just insert the LED like that!), but applicable to 1 LED only

NOTE: One caution to be expressed that, you should consider it (one of the terminal of the LED ) connecting with a resistor ( 330 ohms or nearby ) to limit the current, to avoid burnout.

The following video will show you how the above code will work.


You can also try these blinking code with 2 or even array of LEDs, if your arduino board has got the pins in lesser number, try the multiplexing & charlieplexing techniques, which will lead you to use more LEDs that actual number of pins on your board.
Suppose You have connected your 2 LEDs in pin 12 & 13 respectively.
So the Arduino Sketch/Program will go like this:-

int led1= 13;
int led2= 12;
// the setup routine runs once when you press reset:
void setup() {
// initializing the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led1, HIGH);// turn the LED 1 on (HIGH is the voltage level)
digitalWrite(led2, HIGH);// turn the LED 2 on (HIGH is the voltage level)
delay(500); // wait for 500 milliseconds, or half a second
digitalWrite(led1, LOW); // turn the LED 1 off by making the voltage LOW
digitalWrite(led2, LOW); // turn the LED 2 off by making the voltage LOW
delay(500); // wait for 500 milliseconds, or half a second
}
//End of the program

Simple isnt it. The above program will give you 2 LED's blinking at the same time, you can try some other varient of this code, that will do the following:-

Keeping one LED High the other goes Off, and vice versa with the other.

int led1= 13;
int led2= 12;
// the setup routine runs once when you press reset:
void setup() {
// initializing the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led1, HIGH);// turn the LED on (HIGH is the voltage level)
digitalWrite(led2, Low);// turn the LED on (HIGH is the voltage level)
delay(500); // wait for 500 milliseconds, or half a second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH); // turn the LED off by making the voltage LOW
delay(500); // wait for 500 milliseconds, or half a second
}
//End of the program

In the above program, the LED 1 which is connected to pin 13, will be glowing bright ( i.e., ON), when the other LED 2 which is connected to pin 12, is OFF & the vice versa is true for the next 500 milliseconds time span.

Now suppose for an another varient of LED blinker, you have now 6 leds connected to digital IO pins, 8,9,10,11,12,13. Now what you want is like the light is running from one end to another. Like it is starting from LED connected to pin 7 & going through all pins in between it ends up in pin 13 connected LED, that will give the illusion of a running light pattern that we will make through arduino.

So here is the code you will proceed.
int led1= 8;
int led2= 9;
int led3= 10;
int led4= 11;
int led5= 12;
int led6= 13;
// the setup routine runs once when you press reset:
void setup() {
// initializing the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led1, HIGH);
delay(200);
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
delay(200);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
delay(200);
digitalWrite(led4, HIGH);
digitalWrite(led3, LOW);
delay(200);
digitalWrite(led5, LOW);
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
delay(200);
}
//End of the program

The run if the light will complete in 1.2 seconds, and you can readily turn the speed of it more faster or more slower. The above way of doing it, is actually an inefficient way of doing it. Later on if you got much experienced in its programming you will do it, in a much shorter way, using arrays, or your custom made methods!

Getting back to the 2 LEDs example, that are connected to pins 12, & 13. Suppose we want to transmit an optical code to the other end, which will turn led 1 (which is taken to be green) five times, LED 2 2 times (that is red in color) & then green again 2 times. When this color code is conveyed to the end user that means, something to him! [That you decide, its your secret message!!]
May be you are thinking that it will be a veri long program, like the previous one, but no, it will not be so....
So the code for this would be like.

int i=0;
int led1= 13;//Green one
int led2= 12;/Red one
// the setup routine runs once when you press reset:
void setup() {
// initializing the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
for(i=0;i<5;i++)
{
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);
}
for(i=0;i<2;i++)
{
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);
}
for(i=0;i<2;i++)
{
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);
}
}
//End of the program

So the program will do your someting like cryptographic work even....!!