PROGRAM TO BLINK A LED USING ARDUINO UNO

 DIWALI IS FUN....


JUST BLINK SOME LED TO MAKE DIWALI LIGHT

Now that you’ve got the basics of electricity under control, it’s time to move onto controlling things with your Arduino. In this project, you’ll be building something that could have been a spaceship interface in a 1970s science fiction movie. You’ll make a cool control panel with a switch and lights that turn on when you press the switch. You can decide whether the lights mean “Engage Hyperdrive” or “Fire the lasers!”. A green LED will be on, until you press a button. When the Arduino gets a signal from the button, the green light will turn off and 2 other lights will start blinking.

int switchState = 0;

 void setup()
        {
             pinMode(3,OUTPUT); 
             pinMode(4,OUTPUT); 
             pinMode(5,OUTPUT); 
             pinMode(2,INPUT);
         }
 
void loop()
    {
         switchState = digitalRead(2);

        if (switchState == LOW) 
                {
                    digitalWrite(3, HIGH);     
                    digitalWrite(4, LOW); 
                    digitalWrite(5, LOW); 
                
        else 
                
                    digitalWrite(3, LOW); 
                    digitalWrite(4, LOW);
                    digitalWrite(5, HIGH);
                     delay(250); 
                    digitalWrite(4, HIGH); 
                    digitalWrite(5, LOW);
                    delay(250);
                }
 }



The Arduino’s digital pins can read only two states: when there is the voltage on an input pin, and when there’s not. This kind of input is normally called digital (or sometimes binary, for two states). These states are commonly referred to as HIGH and LOW. HIGH is the same as saying “there’s voltage here!” and LOW means “there’s no voltage on this pin!”. When you turn an OUTPUT pin HIGH using a command called digitalWrite(), you’re turning it on. Measure the voltage between the pin and ground, you’ll get 5 volts. When you turn an OUTPUT pin LOW, you’re turning it off.


Every Arduino program has two main functions. Functions are parts of a computer program that run specific commands. Functions have unique names and are “called” when needed. The necessary functions in an Arduino program are called setup() and loop(). These functions need to be declared, which means that you need to tell the Arduino what these functions will do. setup() and loop() are declared as you see on the right. In this program, you’re going to create a variable before you get into the main part of the program. Variables are names you give to places in the Arduino’s memory so you can keep track of what is happening. These values can change depending on your program’s instructions. 


Variable names should be descriptive of whatever value they are storing. For example, a variable named switch state tells you what it stores: the state of a switch. On the other hand, a variable named “x” doesn’t tell you much about what it stores.


To create a variable, you need to declare what type it is. The data type int will hold a whole number (also called an integer); that’s any number without a decimal point. When you declare a variable, you usually give it an initial value as well. The declaration of the variable as every statement must end with a semicolon (;). The setup() runs once when the Arduino is first powered on. This is where you configure the digital pins to be either inputs or outputs using a function named pinMode(). The pins connected to LEDs will be OUTPUTs and the switch pin will be an INPUT. The loop() runs continuously after the setup() has completed. The loop() is where you’ll check for voltage on the inputs, and turn outputs on and of. To check the voltage level on a digital input, you use the function digitalRead() that checks the chosen pin for voltage. To know what pin to check, digitalRead() expects an argument. Arguments are information that you pass to functions, telling them how they should do their job. For example, digitalRead() needs one argument: what pin to check. In your program, digitalRead() is going to check the state of pin 2 and store the value in the switchState variable. If there’s the voltage on the pin when digitalRead() is called, the switchState variable will get the value HIGH (or 1). If there is no voltage on the pin, switchState will get the value LOW (or 0).


Above, you used the word if to check the state of something (namely, the switch position). And if() statement in programming compares two things, and determines whether the comparison is true or false. Then it performs actions you tell it to do. When comparing two things in programming, you use two equal signs ==. If you use only one sign, you will be setting a value instead of comparing it. digitalWrite() is the command that allows you to send 5V or 0V to an output pin. digitalWrite() takes two arguments: what pin to control, and what value to set that pin, HIGH or LOW. If you want to turn the red LEDs on and the green LED of inside your if() statement, your code would look like this. You’ve told the Arduino what to do when the switch is open. Now define what happens when the switch is closed. The if() the statement has an optional else component that allows for something to happen if the original condition is not met. In this case, since you checked to see if the switch was LOW, write code for the HIGH condition after the else statement. To get the red LEDs to blink when the button is pressed, you’ll need to turn the lights off and on in the else statement you just wrote. To do this, change the code to look like this. After setting the LEDs to a certain state, you’ll want the Arduino to pause for a moment before changing them back. If you don’t wait, the lights will go back and forth so fast that it will appear as if they are just a little dim, not on and of. This is because the Arduino goes through its loop() thousands of times each second, and the LED will be turned on and of quicker than we can perceive. The delay() function lets you stop the Arduino from executing anything for a period of time. delay() takes an argument that determines the number of milliseconds before it executes the next set of code. There are 1000 milliseconds in one second. delay(250) will pause for a quarter second.

Comments