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

No comments:

Post a Comment