What you learnt so far?
If you followed the previous blog(s), by now you would have installed and tested the Genuino board successfully. In this blog, you will get an insight of how Genuino program works by making some changes in the example code. This will help you write your own program in the future.
How Genuino program / Arduino works?
The Genuino program is called as ‘sketch’. To understand how a sketch makes Genuino board work, you will need an example code – Blink.
As mentioned in the previous blog, we follow the 4 basic steps:
- Connect the Genuino board to computer using USB cable
- Open the Arduino IDE (the software)
- Open the BLINK example code using the menu: File > Examples > 01.Basics > Blink
- Ensure the BOARD and PORT settings are correct: Tools > Board & Tools > Port
Now that BLINK sketch is open, let’s have a closer look at how it works?
Any Genuino sketch or code will have a minimum of two functions:
- This is the function or piece of code that is executed by Genuino board once it starts
- This is executed only ONCE i.e., all the instructions written within SETUP function will be executed when Genuino starts (or restarts after a reset!)
- This is the function or piece of code that is executed repeatedly (that’s why it is called as loop).
- After setup execution, the instructions written in LOOP function are executed from top to bottom, sequentially. Once it finishes executing the last instruction in the LOOP function, it start all over from the beginning (1st instruction) of the LOOP function.
Understanding the BLINK sketch…
Let’s have a closer look at the BLINK sketch and the instructions used in the code.
The SETUP Function:
– in the SETUP, we define the digital pin 13 of Genuino board as output. Once a pin is defined as output, you can make the Genuino write (or set) the pin voltage. Since it is a digital output pin, you can set it as HIGH (~5V) or LOW (0V).
Pin # 13, on the Genuino board is internally connected to an on-board LED in such a way that when this pin is HIGH, the LED glows.
The LOOP Function:
Since the loop function is repeated infinitely (until Genuino is powered off or reset), the LED is turned on and off in this function.
The LED connected on pin 13 is turned on by setting the pin to a logic HIGH (or 5V) using the instruction digitalWrite. This function lets you set a specified digital pin as HIGH or LOW.
You would be required to wait for some time before you turn the LED off. The “wait” is inserted using delay function which takes time in milliseconds as its argument. The delay specified in the BLINK sketch is 1000ms = 1second.
After this delay, the LED is turned off using digitalWrite function again.
Having understood the code, can you play around with the code and see if you can change the way LED blinks now?
For example, you may change the delay (in both the delay functions) to 500ms à compile the sketch and à upload it on the Genuino board and check if blinking became faster !
Other things you could do…
- Change the delay to 2000ms and see if the blinking slows down.
- Have a different blinking pattern by using digitalWrite and delay combination multiple times