Step 3 – Concept of programming variables in your sketch

After going through blog # 1 and # 2, you would have gotten a fair idea of how to setup, connect and program your Genuino board. This blog will introduce you to the concept of using programming variables in your Arduino sketch.

Why use Programming variables?

A variable is used to store a value and reference it with a meaningful name. For example, instead of using the value ‘13’ for LED pin in the blink sketch, we could use reference it by variable with the name ‘led_pin’. The variable can be, then, assigned a value of 13 to blink the LED connected to pin 13 of the Genuino.

How to use a variable?

The programming syntax, requires you to declare the variables before you could use them in the sketch or program i.e., you need to define the type of variable (what sort of ‘value’ it will store) and optionally set the initial value.

For example:

In the similar fashion, we can also use another variable for the blink timing, i.e., the delay between on and off of the LED.

As you would notice, it is also possible to set (initialize) the value of the variable right at the declaration. Use of variables makes our sketch easy to read and maintain (imagine how easy it is to change the blinking of LED on another pin of your Genuino board or to modify the blinking timing by changing the value at one place!).

More information on the this topic can be obtained from: https://www.arduino.cc/en/Reference/VariableDeclaration

Let’s test it!

Modify the blink sketch by making use of variables for LED pin and timing delay. Specify (set) the value of time delay > compile and upload the sketch > validate the blinking rate change.

You can play around the timing delay value and produce different blinking rate for the LED.

(Click on the images below to see different blinking rate)

Variable declaration in Blink sketch - Pakronics

Leave a Reply

Your email address will not be published. Required fields are marked *