Tuesday, 21 April 2015

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