How to turn ON / OFF two LED's using arduino


We need a  program for Arduino to Turn ON or Turn OFF two LED's. Source code is very simple. Only on logic is used in this Arduino program that is if-else. I hope you are familiar with if-else statements. I you don’t know what is if-else statement then don’t worry and read this: Conditional statements in programming.
 A complete source code is shown below for arduino board. Using this source code we can Turn ON or Turn OFF two AC devices. 
char incomingByte; 
int  LED1 = 11;
int LED2=12; 

void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);       }

void loop() {
  if (Serial.available() > 0)
  {           incomingByte = Serial.read();
   
    if(incomingByte == '0')
    {      digitalWrite(LED1, LOW);       }
else if(incomingByte == '1')
    {       digitalWrite(LED1, HIGH);               }
else if(incomingByte == '2')
{
digitalWrite(LED2, HIGH);       }
else if(incomingByte == '3')
    {
 digitalWrite(LED2, LOW);        }
  }
}

Line by line working of program

Char incommingByte;   
IncommingByte is a variable of type character.Android device will send character to Bluetooth and Bluetooth will send character to Arduino.Then that character will be stored in incommingByte variable. Remember incommingByte   is not constant or keyword we can change it.

Int LED1=11;
It means our first led or first AC device will be attached to digital pin 11.

Int LED2=12;
It means our second led or second AC device will be attached to digital pin 12.

Void setup()
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes. The setup function will only run once, after each power-up or reset of the Arduino board.

Serial.begin(9600);
Basic syntax:     Serial.begin(speed)
It sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer.

pinMode(LED1, OUTPUT);
Basic syntax:   pinMode(pinNumber , Type)
Type maybe output or input.
It specifies pin mode for specific pin. In this statement pin mode of led1 is set to output.Led1 is pin 11 here because LED1 is integer variable and we have assigned value 11 to LED1.So basically pin 11 is set for output.

pinMode(LED2, OUTPUT); 

Same as previous line

void loop()
An infinite loop function.
What is infinite loop?
An infinite loop (or endless loop) is a sequence of instructions in a computer program that runs statements infinite number of times.
If(Serial.available())
Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes).
Important thing
All other if statements or if-else statements are inside this if statement.
Inside this statement there are many if-else statements.

if(incomingByte == '0')
    {
       digitalWrite(LED1, LOW); 
       }

It will check if incoming data is 0 then it will set led1 to low. It means our led 1 will be off. Otherwise inside statements will not run.

Else if(incomingByte == '1')
    {
       digitalWrite(LED1, High); 
       }

It will check if incoming data is 1 then it will set led1 to HIGH. It means our led 1 will be ON. Otherwise inside statements will not run.

In the same sense other two else-if statements will be executed.
So here our one loop is executed. So execution will again start from void loop and execute statements it the same sense. So in the same way void loop will execute infinite number of times.
Reactions

Post a Comment

0 Comments