Arduino 101: Tutorial #7 |Playing with LED|



Hello people!

In this tutorial we will try to make various patterns to blink a single LED. This is going to be fun :). So let's get started.!

Suggested Reading

If you aren't familiar with the following concepts, I recommend checking out these tutorials before continuing.
In this tutorial we're going to use the same circuit that we made in our previous tutorial here. The circuit goes as follows:


The only new thing that we're going to do today is to blink the LED differently by making slight changes to our code.

Coding

Until now, we've been executing all lines of our code. But what if we want to do something on a certain condition, and something else on some other condition. These kinds of situations as very common in coding and is an important part of programing. These situations are handled in programing by something known as the control structure. The very first type of control structure is an if statement and it is very easy to use. Let us see how to use an if statement by an example. Consider the code below:

As you can see, the only thing that we did differently from our previous example is:
  1. Declaring a new variable named waitTime initialized at a value of 1000 and changing its value every time in the loop.
  2. Using an if statement
Let us now try to understand the effect of this code on the behaviour of our LED. Let us first look at the variable waitTime. We can see, that every time the code loops again, the value of variable waitTime decreases by 100. For the first time in loop, it will be 1000 and for each consecutive time it will decrease by 100 until it reaches a value of 0. Also, in the delay command we are using this variable, hence the delay of the LED will also change in every loop making the LED blink differently in each cycle.

Now let us try to understand what will happen when the value of variable waitTime reaches zero. This is where the if statement comes into play. 

The if statement: Working

  1. The if statements takes a condition as an input. This input condition is written in parenthesis.
  2. Checks if the condition is true or false.
  3. If the condition is true, it executes the code in the curly braces following the statement.
In our case, the input condition is "waitTime==0". Here, the"==" sign is known as the comparison operator. It compares the value to its left and right and checks for their equality. If the values are equal, the condition is set to be true else to false. Hence, in our code, whenever the value of variable waitTime is equal to zero, the statement in the curly braces is executed, and the value of variable waitTime is set to 1000.

The loop runs over and over producing a blinking pattern of the LED as described in the video below:



To summarize, in this tutorial we learnt to produce a blinking pattern in an LED using if statement.
In next tutorial, we will see how to use a potentiometer to control the brightness of an LED.

Hope you enjoyed the tutorial. If you have any suggestions/complaints or questions feel free to comment below. :)

Comments

Popular posts from this blog

Tutorial: Data Acquisition System

Project: Electronic Steering Wheel

Project: FSAE Variable length Intake Manifold(VLIM)