Tuesday, 15 July 2014

Dissection of an Arduino sketch

Hi, in this post i will be explaining about the Arduino sketch and what are the different parts of an arduino sketch.
First of all Arduino micro-controller comes with arduino software which could be downloaded from here. Arduino software are called as Sketch which are uploaded to the micro-controller. A basic sketch looks like figure shown below. These sketches are saved as .ino extension.
 

Now let me explain about the  different parts of the Arduino sketch.
  1. The #define part: #define is a keyword for defining constant or some small expressions in the code. It is used like this, there is an on board led attached to the arduino which is at pin number 13, so whenever you are going to give commands like on or off you will also be needing the pin number which you want to one off. Now it is very confusing to remember all the pin numbers especially if you have a lot of pins so you will define pin 13 as led pin and this is done as #define LED_PIN 13(#define Name Value/Expression).
  2. The #include part: There are a lot of libraries are available to use in any project like LCD for display and Wifi library for wifi connection, so you don't need always start with writing function for different peripherals from scratch as it is already written by someone. So you have to just include those in your sketch and that is done by #include keyword. Like say you want to use the SPI library , then just write #include<spi.h> and all the SPI function will be available to you. You can see the libraries available at arduino.cc.
  3. Then comes the variables: Variables are as name suggest variables , means there value can be changed while your program is running. It is used like this say, you have sensor data in Fahrenheit and you want to convert it and save it then you will make a variable say float cel_temp and you can save your converted value every time in this variable. If you want to make any variable constant so that they don't change their value while program is running just add a const keyword before int, float or double,e.g: const float pi=3.14;
  4. setup() function: This part of sketch defines, different task you want to do once when micro-controller starts, this block contains instructions like initialization of serial port, other different initialization which should be done once.
  5. loop() function: After executing the setup part, micro-controller comes in this block and goes into an infinite loop means it will execute the instruction in this block again and again until micro-controller is reset. So, this part contains the instruction for the task which you to repeat like getting sensor data , do some computation , output to motors according to inputs then again get data from sensor and then same task.
So, these are the basic parts of an arduino sketch, it may or may not contain the first three parts but it must contain the last two part. In my next posts i will be explaining more about the programming and different tips and tricks about using arduino baords.

No comments:

Post a Comment