Tuesday 21 April 2015

Unary Minus Operator Vs Binary Minus

we know minus sign(-) which is used for subtraction(binary minus operator), But what is mean by unary minus operator?

first.. What's the meaning of unary operator?

a unary operation is an operation with only one operand, i.e. a single input


when minus sign is used as unary operator the value of variable is negated.

void main(){
int num = 4;
int negeted_num = -num; //unary operator
printf("Demonstration of Unary Minus Operator\n");
printf("the value of num is %d\n",num);
printf("the value of negeted number \n");
printf("negeted_num = - num --> %d",negeted_num);
getch();
}


and the output is
unary_minus

of all the arithmetic operators unary minus has a highest precedence level

Integer Truncation Error in C

the integer truncation error is displayed using the following simple program

void main(){
int a = 5;
int b = a/2;
int c = b*2;
printf("integer truncation error in C\n");
printf("the value of a is %d\n",a);
printf("the value of b = a\\2 is %d\n",b);
printf("the value of c = b*2 is %d\n",c);
getch();
}


and the output is
integer_truncation

which displays the result of (5/2)*2 = 4 which is wrong.

Points to Remember when Writing a C program

When Declaring a Variable :
Declaration must be placed after the opening braces of the main() function.all declarations must come before the first executable(non-declaration) statements of the program.

all types of variables must be declared before they are used.


When Naming a Variable :
1.Variable name must begin with a letter of alphabet.In C underscore(_) is considered as a letter
2.Compiler treats uppercase and lowercase letters as different
3.No special characters, such as blank space,period,semicolon,comma, or slash are permitted in variable names

what is a variable ?
it is a symbolic name for a memory location in which assigned value is stored

Comments:
It is easier to put in the comments as you code the program rather than after the entire program has been completed.

Variable Initialization :
It is not sufficient to declare a variable before it is used, the variable must also be initialized.
the compiler doesn't automatically assign 0 to it when it is created.


void main(){
int a;
printf("%d",a);
getch();
}


prints the garbage value.when i try to run the output is

init_garbage


Integer Truncation Error :
fractional parts are truncated into integers which leads to horrendous errors\



Note: printf the letter f in that stands for either function or formatted

[In Progress]

Pointers in C

simple code to demonstrate the pointers in C.

#include stdio.h
#include conio.h

void main(){
int *p_num; //define a pointer which holds the integer value
int num;

p_num = # // assign a value(address) to a pointer

printf("enter number : ");
scanf("%d",&num);

printf("the integer number assigned to variable num is: num = ");
printf("%d\n\n",num);

printf("the address integer variable num is stored: &num = ");
printf("%d\n\n",&num);

printf("the value stored in the pointer p_num is: p_num = ");
printf("%d\n\n",p_num);

printf("the address of pointer is in the memory is: &p_num = ");
printf("%d\n\n",&p_num);

printf("the value which address is stored in the pointer is:*p_num = ");
printf("%d\n\n",*p_num);

getch();//to hold the display
}//end of main


Output

C_pointers


1.we have to assign a address to the pointer not a value
2."&"(ampersand) sign is used to get the address of the variable stored in the memory
3."*"(atrisk) is used to declare the pointer and to get the value from pointer

char strings Vs Integers in C

simple program to demonstrate the inputting and displaying of character strings and integers in C


#include stdio.h
#include conio.h
#include string.h

void main(){
char name[15];
int age;

printf("enter name : ");
scanf("%s",name); // please note there is no & sign for char string

printf("enter age : ");
scanf("%d",&age); // note the & sign for the integer


printf("%s\t%d",name,age);

getch();

}//end of main


Result
char string Vs integer

Please note the & sign difference between char string and integer

structures with arrays in C

The following simple code demonstrates use of structures with arrays in C

#include stdio.h
#include conio.h
#include string.h
#define max_no 20

/*defining structure */
typedef struct{
char name[15];
int age;
}person;

void main(){
person p[max_no]; // defining structure with array
int i,j,max;

printf("enter the max number of students\n");
scanf("%d",&max);

for( i=0;i<max;i++)
{
printf("\n enter name : ");
scanf("%s",&p[i].name);
printf("\n enter age for %s : ",p[i].name);
scanf("%d",&p[i].age);
}//end of for loop

printf("\n Name \t age \n");
printf("-----------\n");

for(j=0;j<max;j++)
{
printf(" %s \t %d \n",p[j].name,p[j].age);
printf("-----------\n");
}//end of for loop

getch(); // used to hold the display
}//end of main



Output

structures with arrays

Monday 20 April 2015

Basic Structure Usage in C

the simple code to use structure is

#include stdio.h
#include conio.h

struct person{
char name[15];
int age;
};//please note the semicolon

void main(){
struct person p1; /* struct person --> datatype
p1 --> variable name */
printf("enter the name of person : ");
scanf("%s",&p1.name);/* please note the & sign
i don't know why we have include that & sign for string*/

printf("enter the age of %s",p1.name);
scanf("%d",&p1.age);

printf("the age of %s is %d",p1.name,p1.age);

getch();// to hold the display
}//end of main



we can avoid struct in the datatype declaration using typedef

using typedef we can rewrite the above code as following

#include stdio.h
#include conio.h

typedef struct{
char name[15];
int age;
}person; /* please note the variable name
typedef struct --> datatype
person --> variable name */
void main(){

person p1;

printf("please enter the name of person");
scanf("%s",&p1.name);
printf("enter the age of %s",p1.name);
scanf("%d",&p1.age);

printf("the age of %s is %d",p1.name,p1.age);
getch();//to hold the display
}//end of main


and the output of both is same
strings

Saturday 11 April 2015

Logical Operations

|| -- logical OR
&& -- logical AND
! -- logical NOT

logical AND

void setup(){
Serial.begin(9600);
Serial.println("checking the use of &&(logical AND) operator");
}

void loop(){
Serial.println("enter character or number");
while(!Serial.available()){
//wait until user enters the data
}//end of while

if(Serial.available()){
char ch = Serial.read();
Serial.println("the character");
if((ch = '0')){
Serial.println("you entered a number ");
Serial.print("the Number is \t");
Serial.print(ch - '0');
Serial.print("\n");
}else{
Serial.println("you entered character");
Serial.print("the chraracter you entered is\t");
Serial.print(ch);
Serial.println("");
}//end of If-Else condition
}//end of If condition
}//end of loop

Checking the Char is digit or not

The following program is used to check whether char is digit or not

void setup(){
Serial.begin(9600);
Serial.println("checking whether char is digit or not");
}

void loop(){
Serial.println("enter character or number");
while(!Serial.available()){
//wait until user enters the data
}//end of while

if(Serial.available()){
char ch = Serial.read();
Serial.println("the character");
if((ch = '0')){
Serial.println("you entered a number ");
Serial.print("the Number is \t");
Serial.print(ch - '0');
Serial.print("\n");
}else{
Serial.println("you entered character");
Serial.print("the character you entered is\t");
Serial.print(ch);
Serial.println("");
}//end of If-Else condition
}//end of If condition
}//end of loop


alternatively we can use isdigit() function from C reference.



isdigit
int isdigit ( int c );
Check if character is decimal digit
Checks whether c is a decimal digit character.

Decimal digits are any of: 0 1 2 3 4 5 6 7 8 9

Error "avrdude: stk500_getsync(): not in sync: resp=0x00" How to get Rid of it

when the error avrdude: stk500_getsync(): not in sync: resp=0x00 comes in screen
error

it is usually comes because of selecting the wrong com port in the arduino tools menu
we can usually see which port we connected by the following method

go to the control panel --> Hardware and Sounds --> device manager --> Ports(Com & LPT)
COM port

and set the correct the com port in Arduino Tools Menu. OR may be due to selection of other Arduino Board

Parsing Commands


/*
Author : Kunchala Anil
*/
#define max_size 40
char data_buffer[max_size];
char data_cpy[max_size];
char data_cmd[max_size];
char data_val[max_size];
char question_1[] = "Please enter the command with value";
char cmd_1[] = "anil";
char cmd_2[] = "sunil";
char end_char = '@';
void setup(){
Serial.begin(9600);
Serial.println("\t\t Arduino Parsing commmands");
}//end of setup()

void loop(){
ask_question(question_1);
serial_check();
serial_output();
parse_commands();

}//end of loop()

void ask_question(char* question){
Serial.println(question);
}//end of ask_question() function

void serial_check(){
while(!Serial.available()){
//wait until user enters the data
}//end of while
while(!received()){
//loop until full data received
//Serial.println("received loop In serial_check() function");
}//end of while(received) function

}//end of serial_check() function

boolean received(){
static byte index = 0;
char input;
if(Serial.available()){
input = Serial.read();
if(input == '\r'){
data_buffer[index] = end_char;//Null terminating the string
data_buffer[index+1] = 0;//Null terminating the string
index = 0;//reset the index variable
return true;
}
else{
data_buffer[index] = input;
index = index + 1;
}//end of If-Else condition
}//end of IF condition
return false;
}//end of received() function

void serial_output(){
Serial.println("the data you enteres is");
Serial.println(data_buffer);
Serial.print("having a length of\t-->");
int length = strlen(data_buffer);
Serial.print(length);
Serial.print("\n");
Serial.print("\tindex \tchar\n");
for(int i=0;i<length;i++){
Serial.print("\t ");
Serial.print(i);
Serial.print("\t ");
Serial.print(data_buffer[i]);
Serial.print("\n");
}//end of for loop
}//end of serial_output() function


//FUNCTION TO PARSE COMMANDS
void parse_commands(){
byte index_cpy = 0;

int cmd_no = 0;

int length = strlen(data_buffer);
for(int i =0;i<length+1 ;i++){
if(data_buffer[i] == end_char){
cmd_no = cmd_no+1;
Serial.print("command no \t");
Serial.print(cmd_no);
Serial.print("\tis received\n");

parse_it(data_cpy);//call the function to find command and value
memset(data_cpy,0,strlen(data_cpy));
index_cpy = 0;
}else{
data_cpy[index_cpy] = data_buffer[i];
index_cpy = index_cpy + 1;
}//end of If Else condition
}//end of for loop

}//end of parse_commands() function

//FUNCTION TO SEPERATE COMMAND AND VALUE
void parse_it(char* data){
byte index_cmd = 0;
int length = strlen(data);
for(int i = 0; i < length+1; i++){
if(!isdigit(data[i])){
data_cmd[index_cmd] = data[i];
index_cmd = index_cmd + 1;
}else{
know_cmd(data_cmd,data);
memset(data_cmd,0,strlen(data_cmd));
memset(data,0,strlen(data));
break;
}//end of If Else condition
}//end of for loop
}//end of parse_it() function


void know_cmd( char* cmd,char* val){
if(strncmp(cmd,cmd_1,strlen(cmd_1)) == 0){
Serial.println("you received command for Anil");
float result = get_val(val);
Serial.print("The value for anil is\t");
Serial.print(result);
Serial.println("");
}else if(strncmp(cmd,cmd_2,strlen(cmd_2)) == 0){
Serial.println("you received command for Sunil");
get_val(val);
}else{
Serial.println("You received wrong command");
}//end of If Else-If condition
//memset(cmd,0,i);

}//end of know_cmd() fucntion

float get_val(char* val){
byte index_val = 0;
for(int j=0; j<strlen(val)+1; j++){
if(isdigit(val[j])){
data_val[index_val] = val[j];
index_val = index_val + 1;
}
}//end of for loop
Serial.println("the value of the command is");
Serial.println(data_val);
float return_val = atof(data_val);
memset(data_val,0,strlen(data_val));
return return_val;
}//end of get_val function

Friday 10 April 2015

Thursday 9 April 2015

Checking the char is digit or not ?


/*
Author : kunchala Anil
checking that if entered char is digit or not
*/
void setup(){
Serial.begin(9600);
}//end of setup()

void loop(){
Serial.println("enter char");
while(!Serial.available()){
//wait until user enters data
}
while (Serial.available()){
char ch = Serial.read();
Serial.print("the character you entered ");
if(isdigit(ch)){
Serial.print("is digit\n");
}else{
Serial.print("not digit\n");
}//end of If
}//end of while
}//end of loop

and the result is
isdigit

Note the Noline ending and baud rate at serial monitor

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.