Tuesday, 7 April 2015

Simple Arduino Serial Monitor Calculator


/*
Author : Kunchala Anil
Arduino Calculator
This Program demonstrates the simple calculator using Arduino Serial Monitor
*/

#define max_data 20
char data_buffer[max_data];
boolean got_add = false,got_sub=false,got_mul=false,got_div=false;
float A,B;

void setup(){
Serial.begin(9600);
Serial.println("\t\t\t Arduino Calculator");

}//end of setup

void loop(){
ask_question_get_values();
ask_get_operator();
result();
}//end of loop

void ask_question_get_values(){
//get the value of A from user and display it
Serial.println("Enter the Value of A");
A = get_value();
Serial.print("the value of A you entered is\t");
Serial.print(A);
Serial.println("");

//get the value of B from user and display it
Serial.println("Enter the value of B");
B= get_value();
Serial.print("the value of B you entered is\t");
Serial.print(B);
Serial.println("");
}//end of ask_question_get_values() function



float get_value(){
static byte index = 0;
char input;
boolean read_data = false;
while(!Serial.available())
{
//wait until user enters data
}//end of while loop

input = Serial.read();
if(input !='\r'){
data_buffer[index] = input;
index = index++;
get_value();//loop until get the full word
}
else{
data_buffer[index] = 0;//terminate the char array with NULL to make it string
index = 0;
read_data = true;
}//end of if else condition

if(read_data){
float result = atof(data_buffer);//atof --> ascii to float
return result;
}//end of if condition

}//end of get_value() function

void ask_get_operator(){
Serial.println("please choose the computational operator");
Serial.println("\t addition \t\t--> a \n\t subtraction \t\t--> s\n\t multiplication \t-->m\n\t division\t\t-->d\n");
operator_check();
return;
}//end of ask_get_operator() function

void operator_check(){
char input;
static byte index;
boolean received = false;
while(!Serial.available())
{
//wait until user enters the data
}
input = Serial.read();
if(input != '\r'){
data_buffer[index] = input;
index = index + 1;
operator_check();//loop until receiving is completed
}
else{
data_buffer[index] = 0;//terminating with NULL
index = 0;
received = true;
}//end of if else condition

if(received == true){
received = false; // restore the previous value
if(strcmp(data_buffer,"a") == 0){
got_add = true;
Serial.println("you selected addition");
return;

}
else if(strcmp(data_buffer,"s") == 0){
got_sub = true;
Serial.println("you selected subtraction");
return;
}
else if(strcmp(data_buffer,"m") == 0){
got_mul = true;
Serial.println("you Selected Multiplication");
return;
}
else if(strcmp(data_buffer,"d") == 0){
got_div = true;
Serial.println("you Selected Division");
return;
}
else{
Serial.println("you entered a wrong character");
return;
}//end of if else-if else condition

}//end of If condition
}//end of operator_check() function

void result(){
float output;
if(got_add){
output = A+B;
got_add = false;
Serial.print("the result of addition is\t");
Serial.print(output);
Serial.println("");
}
else if(got_sub){
output = A-B;
got_sub = false;
Serial.print("the result of subtraction is\t");
Serial.print(output);
Serial.println("");
}
else if(got_mul){
output = A*B;
got_mul = false;
Serial.print("the result of multiplication is\t");
Serial.print(output);
Serial.println("");
}
else if(got_div){
output = A/B;
got_div = false;
Serial.print("the result of division is\t");
Serial.print(output);
Serial.println("");
}
else{
Serial.println("something went wrong");
}
}//end of result() function

Using Functions in Arduino

What is a Function ?
Function is a block of code that performs a specific task within a large program and relatively independent of the remaining code

what's the use of function ?
1.Functions make code more readable
2.we can reuse the code without rewriting it
3.easy to debug the code
4.function can be accessed from any location within a program and name of function is unique and global


How to Write a Function ?
The Syntax(technical name) of function is


returnType functionName(arguments)
{

functionBody;

return returnValue;
}



The arduino is based on c/c++ so a function declaration in arduino is same as C.

Before going to what is what in function syntax, let me remind you about function once agian.

function is a few lines of code with a separate name in a large / main program.

so we have to specify the name for the function

which is
functionName
in syntax

To do a Task we need specific information and that may/may not be coming from main program... But what if Our function needed information from main program?

so we need a way to pass information to the main program if needed

which is
arguments
in syntax and as stated before this is optional.

the few lines of code that performs specific task is called functionName

when we call function from main program, it must have a way to return the result of the task it performed to the main program or calling function.

returnType is used to specify the datatype of the function return variable
and
return returnValue; is used to return the actual value into calling function


But what if we don't want to return anything from the function.
void is used as returnType if function returns nothing.

structuring code into functional blocks is an good practice which is useful when we write the large code and it is easy to understand (if we given the specified functionNames)

Arduino Ex:

/*
Author : Kunchala Anil
Arduino code to describe the use of Functions
In this we ask the user to enter two Number and display it on serial monitor
*/
#define data_max 20
char data_buffer[data_max];
float A;
float B;
void setup(){
Serial.begin(9600);
ask_numbers();
serial_check();
}//end of setup
void loop(){
}//end of loop


void ask_numbers(){
Serial.println("Please enter A");
A = serial_check();
Serial.print("the value you entered is \t");
Serial.print(A);
Serial.println("");
Serial.println("Please enter B");
B = serial_check();
Serial.print("the value you entered is \t");
Serial.print(B);
Serial.println("");
}//end of ask_numbers

float serial_check(){
static byte index = 0;
while(!Serial.available())
{
//wait until user enters the data
}
char input = Serial.read();
if (input != '\r'){
data_buffer[index] = input;
index = index+1;
serial_check();
}else{
data_buffer[index] = 0;//terminate array with NULL to make it string
index = 0;
return atof(data_buffer); // atof --> ascii to float
}
}//end of serial_check



and the result is
function

Monday, 6 April 2015

Using Arrays and For loop

The following code demonstrates the simple use of Arrays Using for loop
In this code four Led's are On and Off Sequentially.


/*
Author : Kunchala Anil
demonstrating the use of Arrays using For loop
*/
int led[] = {8,9,10,11}; // initialize the 4 pins using array
void setup(){
Serial.begin(9600);
//for loop is used to set all pins to Outputs
for(int i=0;i<4;i++){
pinMode(led[i],OUTPUT);
Serial.print("pin\t");
Serial.print(i);
Serial.print("\t is set as Output")
Serial.println("");
}//end of for loop
}//end of setup() function

void loop(){
//for loop is used to On and Off Led's Sequentially
for(int j=0;j<4;j++){
digitalWrite(led[j],HIGH);
Serial.print("LED\t");
Serial.print(j);
Serial.print("\t is ON")
Serial.println("");
delay(1000);
digitalWrite(led[j],LOW);
Serial.print("LED\t");
Serial.print(j);
Serial.print("\t is OFF")
Serial.println("");

}//end of for loop
}//end of loop

Sunday, 5 April 2015

Using Strings

Strings are nothing more than a array of characters terminated by NULL. The null is not displayed, but it is needed to indicate the end of string to the software.

To hold the NULL character at the end of array the size of string is 1 + no.of characters in string.
ex: to hold "anil" the size of string must be 5. 4 characters and another one to hold NULL character.

string declaration
Actually we do not have to place the null character at the end of character array. the compiler will automatically places it.


String declaration
char string_a[5];//declares string up to 4 characters and NULL and index is like 0,1,2,3,4

char string_b[5] = "anil";//declares string with 4 characters and initializes it

note that in arduino double quotes " " used to indicate string and single quotes ' ' used to indicate characters

char string_c[] = "anil";//compiler initializes the string and calculates its size

char string_d[10] = "anil"; // is same as string_b but has room to grow.

Useful functions to manipulate the Null terminated strings

strlen short for string length
is used to determine the number of characters before the null
ex : int string_a_length = strlen(string_a);

strcat short for string concat
is used to concatenate two strings
ex : strcat(string_a,string_b);//concatenates strings_b on the end of string_a

strcmp short for string compare
is used to compare two strings
ex:strcmp(string_a,string_b);//returns 0 if both are same, lessthan zero if string_a < string_b

strncpy short for string copy
is used to copy string source to destination
ex : strncpy(string_c,string_a);//which copies string_a to string_c

Arduino code to demonstrate strings

char string_a[] = "anil";
char string_b[] = "kunchala";

unsigned int cmp_value;

void setup(){
Serial.begin(9600);
int length_a = strlen(string_a);
int length_b = strlen(string_b);

Serial.print("the length of string_a is \t -->\t");
Serial.print(length_a);
Serial.println("");
Serial.print("the length of string_b is \t -->\t");
Serial.print(length_b);
Serial.println("");
Serial.print("the string come by concating string_a and string_b is \t -->\t");
strcat(string_a,string_b);
Serial.print(string_a);
Serial.println("");
Serial.print("the length of newly formed string is\t --->\t");
int length_c = strlen(string_a);
Serial.print(length_c);
Serial.println("");
}//end of setup

void loop(){
//do nothing here
}//end of loop



and the result isstring_code

Saturday, 4 April 2015

LED Brightness Control Via Serial.....


//Author : Kunchala Anil
//Led Brightness control Via Serial
#define ledstep 5
int ledbrightness = 0;
int led[] = {3,5,6,9,10,11};
void setup(){
Serial.begin(9600);
for(int i=0;i<6;i++){
pinMode(led[i],OUTPUT);
}//end of for loop
}//end of setup

void loop(){
Serial.println("enter y to increse the led brightness and n to decrease it");
serial_check();
}//end of loop


//SERIAL_CHECK() FUNCTION
void serial_check(){
while(!Serial.available()){
//wait until user enters the data
}

while(Serial.available()){
char input = Serial.read();
if(input == 'y'){
increase_brightness();
}//end of if
else if(input == 'n')
{
decrease_brightness();
}//end of else if
else {
error();
}//end of else
}//end of while
}//end of serial_check() function

//INCREASE_BRIGHTNESS FUNCTION
void increase_brightness(){
if(ledbrightness < 250){
ledbrightness = ledbrightness + ledstep;
}//end of if
else
{
Serial.println("brightness is maximum resetting it to 0");
ledbrightness = 0;
}//end of else
for(int j=0;j");
Serial.print(ledbrightness);
Serial.println("");
}//end of increase_brightness() function

//MAINTAIN_BRIGHTNESS FUCNTION
void decrease_brightness(){
if(ledbrightness > 0){
ledbrightness = ledbrightness - ledstep;
}//end of if
else
{
Serial.println("brightness is zero resetting it to 250");
ledbrightness = 250;
}//end of else
for(int j=0;j");
Serial.print(ledbrightness);
Serial.println("");
}//end of maintain_brightness() function

//ERROR FUNCTION
void error(){
Serial.println("you entered a wrong character");
}//end of error function

Thursday, 2 April 2015

C/C++ make over in Arduino code

I am neither experienced in Arduino Programming and c language... but i am trying to learn both.
Arduino code is based on the c/c++ language..
anyone who knows simple basics of c language may wonder where the main() entry point function gone?
actually it's there it is hidden under the covers by the Arduino Build environment.
these covers make Arduino an Very easy Language to learn and once you know Arduino it is very hard to learn/program any other controller using C

now i am actually facing the same problem.

the Build process creates an intermediate file that includes the sketch code and the following additional statements.


int main(void)
{
init();
setup();
for(;;)
loop();
return 0;
}


the first thing that happens is a call to an init() function that initializes the Arduino hardware.
next setup() function is called. finally the loop() function is called over and over.
because the loop never terminates, the return statement never executed.

Tuesday, 24 March 2015

Arrays in Arduino

A variable is a label that we give to a piece of information.
This provide us with a simple way to save the information, change it and access it.

when we wanted to name a group of variables of same type, rather than declaring separately we can declare the array of variables.

Arrays are collections of consecutive variables of same type.Each variable in the collection is called an element. The number of elements is called dimensions of the array

array declaration :
one way to declare array without initialization is
int exarray[10];
int is datatype of array
exarray is name of the array
10 is the dimension of array which has a range from 0,1,2,3.... to 9\
if n is the dimension of array it holds the index ranging from 0 to n-1.

another way of declaring array is
int exarray[] = {2,4,5,6,8,10,12,45,78,98};
the above code creates an array of integers with four elements and initializes the each element.

But in the array declaration without initialisation each variable is initialized as 0.

void setup()
{
Serial.begin(9600); // start the serial communication
int exarray[4]; // declare the array with no initial values
int ex2array[] = {4,12,17,19}; // declare the array with initial values
Serial.println("The elements stored in exarray is");
// print the elements in the exarray with index number
for (int i=0;i<4;i++)
{
Serial.print(i);
Serial.print("\t" );
Serial.print(exarray[i]);
Serial.println("");
}

Serial.println("The elements stored in ex2array is");
//print the elements in the ex2array with index number
for (int j=0;j<4;j++)
{
Serial.print(j);
Serial.print("\t");
Serial.print(ex2array[j]);
Serial.println("");
}
}
void loop()
{
//do nothing.
}


and the output is like
array initialization

and surprisingly the elements in the array without initialization are not zeros. all are coming garbage values....


And as Always I go to the Arduino Forum to clarify it. the reply from AWOL says what is the reason for it.


The correct answer is "it depends where you declared the array".
If the array has global scope, or is qualified "static", then it will be initialised (to zero if no initialiser is specified) before "setup()" runs.

If the array has local scope (i.e. it is within a function) and is not qualified "static", then it will not be initialised at all, and its elements will pick up whatever happens to be already there.


which is proved using the below code...

//code for array initialization global
int exarray[4]; // declare the array with no initial values
int ex2array[] = {4,12,17,19}; // declare the array with initial values
void setup()
{
Serial.begin(9600); // start the serial communication
Serial.println("The elements stored in exarray is");
// print the elements in the exarray with index number
for (int i=0;i<4;i++)
{
Serial.print(i);
Serial.print("\t" );
Serial.print(exarray[i]);
Serial.println("");
}

Serial.println("The elements stored in ex2array is");
//print the elements in the ex2array with index number
for (int j=0;j<4;j++)
{
Serial.print(j);
Serial.print("\t");
Serial.print(ex2array[j]);
Serial.println("");
}
}
void loop()
{
//do nothing.
}


Output:
array initialization global

you can see the forum post here