boolean variable
i came across code lately internet. find hard understand. can please explain me each line of code.
code: [select]
boolean checkbutton()
{
char state = digitalread(2);
if(state == high && pressed == false)
{
pressed = true;
return false;
}
if(state == low && pressed == true)
{
mode++;
if(mode > 2)
mode = 0;
clearleds();
pressed = false;
return true;
}
return false;
}
code: [select]
boolean checkbutton() //declares function returns boolean , takes no arguments when called
{
char state = digitalread(2); //reads pin 2 , puts result in newly declared named state of type char (a byte)
if (state == high && pressed == false) //if value of state variable high , value of pressed boolean variable (presumably declared , givean value global variable) false
{
pressed = true; //set boolean variable true
return false; //return value of false program called function
}
if (state == low && pressed == true) //if value of state variable low , value of pressed boolean variable (presumably declared , given value global variable) true
{
mode++; //increment mode variable (presumably global variable)
if (mode > 2) //if mode greater 2
mode = 0; //set mode 2
clearleds(); //either way call clearleds() function then
pressed = false; //set pressed true then
return true; //return value of true program called function
}
return false; //otherwise return value of false program called function
}
Arduino Forum > Using Arduino > Programming Questions > boolean variable
arduino
Comments
Post a Comment