Thursday, 12 February 2015

#8 Arduino UNO - Basic Functions in Programming.

This is 10th post on Arduino tutorial.
If you want the list of posts click here.
Now Since we saw that programming plays a very important role, lets us see few functions that will help you to program the arduino & also to achieve desired result in a better way.
Arduino programming is similar to C - programming so many of the functions work the way they work in C.

Lets make a list of few :-

1.#include :- this function allows us to import libraries in our program.

2.if() :- if(Condition)
           {
               // if condition is satisfied do whatever is written here
           }  
this are few comparison statements that we can write inside 'if' brackets

 x == y (x is equal to y)    //note '=' is declaration & '==' is checking
 x != y (x is not equal to y)
 x <  y (x is less than y)  
 x >  y (x is greater than y) 
 x <= y (x is less than or equal to y) 
 x >= y (x is greater than or equal to y)

3.if...else :- if (condition)                                if(Condition)
                      {                                                  {
                            // action A                                  //Action A
                      }                                                    }
                  else                                                  else if (Condition 2)
                       {                                                             {
                             // action B                                              //action B
                       }                                                              }
                                                                                      else

                                                                                            {
                                                                                                 //Action C
                                                                                             }
We can see one normal 'if..else' and nested 'if else' loops [Self explanatory].

4.for():- for (initialization; condition; increment) 
                 {
                         //statement(s);
                 }
The for loop is very powerful function, used when we need to execute few statements repeatedly. The for loops will be terminated when the condition becomes invalid.
Best part about for loop is we can initialize variable & increment it too.

5.while() :- while(expression)
                          {
                                   // statement(s)
                          }
while loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit.
The loop begins only when the condition is true.

6.do-while :- do
                        {
                                // statement block
                         } while (test condition);
"Don't forget the semicolon at the end of while". The do loop works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.
Both While & do-while loops require a separate initialization and a proper way of termination, for loop has self increment system.

7.switchCase :-  switch (var) {
                                         case 1:
                                                       //do something when var equals 1
                                                       break;
                                          case 2:
                                                       //do something when var equals 2
                                                        break;
                                          default: 
                                                      // if nothing else matches, do the default
                                                     // default is optional
                                                }
Switch case is used to select one out of many depending on input.
The variable decides which case is to be selected.
The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions until a break, or the end of the switch statement is reached.

8.break :- break is used to exit from a do, for, or while loop, bypassing the normal loop condition. It is also used to exit from a switch statement.
for (x = 0; x < 10; x ++)
{
   digitalWrite(13, HIGH);  
   delay(100);
   digitalWrite(13, LOW);
 if (sens > threshold){      // bail out on sensor detect
       break;
    }  
    delay(50);

}
Ideally the loop would have been executed 10 times but if sensor detects a threshold value then the for loop is terminated irrespective of the condition in for loop.

9.return :- Terminate a function and return a value from a function to the calling function, if desired.

return;

return value; // both forms are valid

The return keyword is handy to test a section of code without having to "comment out" large sections of possibly buggy code.

void loop(){
// brilliant code idea to test here
return;
// the rest of a dysfunctional sketch here
// this code will never be executed
}

A function to compare a sensor input to a threshold
 int checkSensor(){       
    if (analogRead(0) > 400) {
        return 1;
    else{
        return 0;
    }
}

This are few programming functions that we can use in arduino programming for desired output. This functions helps us to do achieve results easily. Keep a habit of implementing this functions wherever required and this can help you to put your logic into code, in a better way.

Visit the next post on '.......'.
click here.

Follow the blog, 
Hit like on the Facebook page and stay tuned :)
Comments are welcomed :)
Thank You.