If they are actually equal, then the comparison is said
to be true. If they aren’t equal, the comparison is said to
be false (giving the operation a value of zero). So, if we
want to set one variable to equal the value contained by
another variable, we use the assignment operator like this:
This makes sense if the buttonState variable is
initialized to zero, but may have been pressed at
sometime after initialization but before this bit of code
runs. If we had used while(buttonState == HIGH) but
hadn’t yet checked the button state, then it would never
run the subsequent block of code.
[Note that this uses the == operator to determine if the
item on the left is equal to the item on the right. We’ll
discuss this operation in detail in the next section.]
if (pushButtonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); }
One Way to Time Events
Later, we’ll see many other examples for using if-else
for making decisions based on the statement within the if
parentheses. For instance, we might ask if one variable is
larger than another using the ‘<’ (less than) operator as
follows:
The Arduino keeps track of the time for up to about
50 days (when the number rolls over). If you get the
number of milliseconds when an event starts and then get
the number of milliseconds after the event ends, you can
subtract the start milliseconds from the end milliseconds
to get the elapsed time.
We will see this used in the reaction time tester lab at
the end of this chapter where we will use the do while()
(discussed above) and the Arduino millis() function to get a
start and end millisecond value to report the reaction time:
if(firstVar < secondVar) { doThis(); } else { doThat(); }
do{ // get the state of the pushbutton buttonState = digitalRead(buttonPin); }while(buttonState == HIGH);
// get the start time as soon as the LED // goes off startTime = millis(); // wait until the subjects gets his finger off // the button // read the button state until it is equal // to HIGH do{// get the state of the pushbutton buttonState = digitalRead(buttonPin); }while(buttonState == HIGH); // get the end time as soon as the finger is // off the switch endTime = millis(); // Tell the world your reaction time Serial.print(“You took: “); Serial.print(endTime-startTime,DEC); Serial.println(“ milliseconds.”);
We can combine if-else to ask several questions about
the operator:
if(firstVar < secondVar) { doThis(); else if(firstVar > secondVar) { doThat(); else { doTheOther(); }
Another Control Loop: do while()
In Chapter 3, we learned about the while() control
structure that runs the block of code that follows the
while(condition) each time it checks the condition and
finds it true. A variant on this is the do while() control
structure which is similar to while() except that it — at least
once — runs the code in the block that follows the do,
regardless of the condition in the while. In a regular
while(condition) loop, the following block will not be run
the first time through if the condition is false; in do while()
the block runs once regardless. In the lab exercises, we
will see a situation where this makes more sense:
Some Equals Are More Equal
Than Others
We learned that operators in C are used for
arithmetic-like operations and include such things as +, -,
and <. Of all the operators we will see, the = and == seem
to give most folks trouble. The = operator is the arithmetic
assignment operator; it will assign the value from the right
side of the = sign to the variable on the left side. The ==
operator is the comparison operator; it is used to
compare the values on either side of the operator.
// First assignment char firstVar = 5; char secondVar = 10; //Second assignment firstVar = secondVar;
April 2014 57