Sunday, June 20, 2010

Arduino learning-4

constants
The Arduino language has a few predefined values, which are called constants. They are used to make the programs easier to read. Constants are classified in groups.
true/false
These are Boolean constants that define logic levels. FALSE is easily defined as 0 (zero) while TRUE is often defined as 1, but can also be anything else except zero. So in a Boolean sense, -1, 2, and -200 are all also defined as TRUE.
if (b == TRUE);
{
doSomething;
}
high/low
These constants define pin levels as HIGH or LOW and are used when reading or writing to digital pins. HIGH is defined as logic level 1, ON, or 5 volts while LOW is logic level 0, OFF, or 0 volts.
digitalWrite(13, HIGH);
input/output
Constants used with the pinMode() function to define the mode of a digital pin as either INPUT or OUTPUT.
pinMode(13, OUTPUT);
if
if statements test whether a certain condition has been reached, such as an analog value being above a certain number, and executes any statements inside the brackets if the statement is true. If false the program skips over the statement. The format for an if test is:
if (someVariable ?? value)
{
doSomething;
}
The above example compares someVariable to another value, which can be either a variable or constant. If the comparison, or condition in parentheses is true, the statements inside the brackets are run. If not, the program skips over them and continues on after the brackets.
Note: Beware of accidentally using ‘=’, as in if(x=10), while technically valid, defines the variable x to the value of 10 and is as a result always true. Instead use ‘==’, as in if(x==10), which only tests whether x happens to equal the value 10 or not. Think of ‘=’ as “equals” opposed to ‘==’ being “is equal to”.
flow.

if… else
if… else allows for ‘either-or’ decisions to be made. For example, if you wanted to test a digital input, and do one thing if the input went HIGH or instead do another thing if the input was LOW, you would write that this way:
if (inputPin == HIGH)
{
doThingA;
}
else
{
doThingB;
}
else can also precede another if test, so that multiple, mutually exclusive tests can be run at the same time. It is even possible to have an unlimited number of these else branches. Remember though, only one set of statements will be run depending on the condition tests:
if (inputPin < 500)
{
doThingA;
}
else if (inputPin >= 1000)
{
doThingB;
}
else
{
doThingC;
}
Note: An if statement simply tests whether the condition inside the parenthesis is true or false. This statement can be any valid C statement as in the first example, if (inputPin == HIGH). In this example, the if statement only checks to see if indeed the specified input is at logic level high, or +5v.
18 |
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following