A Variable Approach to Blinking LEDs
Arduino Fundamentals
A Variable Approach to Blinking LEDs"
Hello guys welcome to the world of arduino.
A series to understand arduino from basics and reach at advance level.
Now we are at our lesson number 4.
In previous articles we write the code in the form of numbers such as pin number 13 or delay 1 second are thousand Milli second but here we will not use the number we will initialize a variable, hence we use variable instead of numbers.
our project is about blinking leds, blink red LED 5 times then blink green LED 5 times and then blink yellow LED five times.
The code is same as in previous article but use the variables instead of numbers.
Circuit
Circuit is same as in previous article and let's move to our code section.
Code
int variable
int variable is used to represent integers like 1, 2 ,3 , 1000, 5000 these are integers.
We have pin like pin number 13 now 13 is an integer we also have delay of 500 milli second are 1000 milli second these also are integer so we start our code writing variable.
This variables are declared before
void setup ().
So start Code
int redled=13;
int greenled=12;
int yellowled=11;
int wait=500;
int LongT=2000;
void setup ()
{
pinMode(redled, OUTPUT);
pinMode (greenled, OUTPUT);
pinMode (yellowled, OUTPUT);
}
void loop ()
{
digitalWrite (redled, HIGH);
delay (wait);
digitalWrite (redled, LOW);
delay (wait);
digitalWrite (redled, HIGH);
delay (wait);
digitalWrite (redled, LOW);
delay (wait);
digitalWrite (redled, HIGH);
delay (wait);
digitalWrite (redled, LOW);
delay (wait);
digitalWrite (redled, HIGH);
delay (wait);
digitalWrite (redled, LOW);
delay (wait);
digitalWrite (redled, HIGH);
delay (wait);
digitalWrite (redled, LOW);
delay (wait);
digitalWrite (greenled, HIGH);
delay (wait);
digitalWrite (greenled, LOW);
delay (wait);
digitalWrite (greenled, HIGH);
delay (wait);
digitalWrite (greenled, LOW);
delay (wait);
digitalWrite (greenled, HIGH);
delay (wait);
digitalWrite (greenled, LOW);
delay (wait);
digitalWrite (greenled, HIGH);
delay (wait);
digitalWrite (greenled, LOW);
delay (wait);
digitalWrite (greenled, HIGH);
delay (wait);
digitalWrite (greenled, LOW);
delay (wait);
digitalWrite (yellowled, HIGH);
delay (wait);
digitalWrite (yellowled, LOW);
delay (wait);
digitalWrite (yellowled, HIGH);
delay (wait);
digitalWrite (yellowled, LOW);
delay (wait);
digitalWrite (yellowled, HIGH);
delay (wait);
digitalWrite (yellowled, LOW);
delay (wait);
digitalWrite (yellowled, HIGH);
delay (wait);
digitalWrite (yellowled, LOW);
delay (wait);
digitalWrite (yellowled, HIGH);
delay (wait);
digitalWrite (yellowled, LOW);
delay (wait);
delay(LongT);
}
Conclusion
Wow, amazing our code is completed now I explain it.
We initialize redled to pin no. 13,
Initialize greenled to pin number 12
Initialize yellowled to pin number 11
Initialise wait to 500 milli seconds
Initialise LongT to 2000 Milli seconds
And we use all these variables in our whole program.
Let's practice it and upload the program to the arduino board.
Wow our LED's are blinking.
We are successful. If we want to change our deley time we only change in our initialization of variables we will write wait is equal to 1000 Milli second and then upload to our Arduino board.
Deley time will change at all places we use the variable wait.
Hence this article completed here please practice it and if you want to ask some question you can also ask in the comment section.
Assignment
Now at the end of this article I want to give you an assignment you have to use 4 LEDs and each LED blink 5 times so you will make a circuit using 4 LED is and 4 resistors.
Writet code in a similar way and add a new LED it may be a blue LED.
Best of luck


Comments
Post a Comment