Monday, 1 December 2014

Declaring the variable as Static

When I digging the net to learn some Serial monitor Basics, I gone through this post
it is really helpful though.

So I think I can Start some.. I started to write it down on arduino IDE to execute it.
So first I literaaly copied it into IDE and executed it and get the OUTPUT as expected..
I think Whoh... I learned everything about Serial communication and i started to code in my own words..

and it ended up something like this..

#define DATABUFFERSIZE 80
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
String question_1 = "Enter a value";
void setup(){
Serial.begin(9600);
ask_question(question_1);
while(!getSerialString()){
//wait here
}
int a = atoi(dataBuffer);
Serial.println("the value of A is::");
Serial.println(dataBuffer);

}
void loop(){

}

void ask_question(String question){
Serial.println(question);
}

boolean getSerialString(){
byte dataBufferIndex = 0;
while(Serial.available()){
char incomingbyte = Serial.read();

if(incomingbyte=='\r'){
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//Our data string is complete. return true
return true;
}
else{
dataBuffer[dataBufferIndex] = incomingbyte;
dataBufferIndex++;
}

}

return false;
}

in the line byte dataBufferIndex
I dont understand the Variable declaration static
so i removed it and still didn't get any error so i think it will execute , and tried to execute it...
and get Output something like this
[caption id="attachment_48" align="alignnone" width="214"]with out static variable in post with out static variable in post[/caption]

as unexpected... the result didn't come it just printing the empty line...

and i didn't know what went wrong.. So I compare the code line to line to find out what went wrong....
the change is i didn't declare it as Static.

I didn't want to use the variable as Static... Why?
because i didnt know what it is.

So I googled about Static and find this in arduino site
Static

The static keyword is used to create variables that are visible to only one function. However unlike local variables that get created and destroyed every time a function is called, static variables persist beyond the function call, preserving their data between function calls.
Variables declared as static will only be created and initialized the first time a function is called.


and unsurprisingly.. I didn't understand a bit in it.
SO i gone to the arduino forum and posted it and got my reply like this:

You are still always resetting dataBufferIndex to 0... I hate repeating myself but.. you have to reset it only after you have received the '\r'.

so i thought byte dataBufferIndex = 0; this line is the wrong line But in the JHaskell's blog he declared it.
as the forum suggests i declared the variable as a global variable.
Quick note: Variables declared Above the setup() function is all global variables
and the code will look like

#define DATABUFFERSIZE 80
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
byte dataBufferIndex = 0;
String question_1 = "Enter a value";
void setup(){
Serial.begin(9600);
ask_question(question_1);
while(!getSerialString()){
//wait here
}
int a = atoi(dataBuffer);
Serial.println("the value of A is::");
Serial.println(dataBuffer);



}
void loop(){

}

void ask_question(String question){
Serial.println(question);
}

boolean getSerialString(){
while(Serial.available()>0){
char incomingbyte = Serial.read();

if(incomingbyte=='\r'){
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//Our data string is complete. return true
return true;
}
else{
dataBuffer[dataBufferIndex] = incomingbyte;
dataBufferIndex++;
}

}


return false;
}

code displays the entered value.. and End of Story.
and Today I accedently look into the STATIC variable i thought i understand something about it

so go back to our first code with variable declaration in getSerialString() function
this function will check whether the '\r' Carriage Return is received or not and return true if the '\r' is received.

Arduino will executed 100's of instruction within time of receiving One character so it will call function the many times while receiving the character and gap between receiving the character.
so the variable dataBufferIndex will initialized to 0 every time it is called. so for every character it will called 100's of times. so we get reply for the function getSerialString() when we received only '\r' character. so when we received '\r' we write the NULL in the dataBuffer in the position of dataBufferIndex which is 0 i.e we are saving a null character in the dataBuffer so program is printing the NULL character itself which is a empty string

So we need a declaration of variable which as to be declared Once and does not reinitialize its value every time it is called and. and according to our program it has to reinitialize when it received '\r' character.

if we look at the explanation of static keyword again:
Static

The static keyword is used to create variables that are visible to only one function. However unlike local variables that get created and destroyed every time a function is called, static variables persist beyond the function call, preserving their data between function calls.
Variables declared as static will only be created and initialized the first time a function is called.

so this is exactly what we need.. So if I change the code using the static variable the code will be

#define DATABUFFERSIZE 80
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
String question_1 = "Enter a value";
void setup(){
Serial.begin(9600);
ask_question(question_1);
while(!getSerialString()){
//wait here
}
int a = atoi(dataBuffer);
Serial.println("the value of A is::");
Serial.println(dataBuffer);



}
void loop(){

}

void ask_question(String question){
Serial.println(question);
}

boolean getSerialString(){
static byte dataBufferIndex = 0;
while(Serial.available()>0){
char incomingbyte = Serial.read();

if(incomingbyte=='\r'){
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//Our data string is complete. return true
return true;
}
else{
dataBuffer[dataBufferIndex] = incomingbyte;
dataBufferIndex++;
}

}


return false;
}


which gives the exact result as second code without global variable...

No comments:

Post a Comment