Hardware Reference
In-Depth Information
Set fadeAmount to its opposite (make it negative if it's positive; make
it positive if it's negative).
Wait 30 milliseconds before executing the loop function again.
Code and Syntax Notes
Besides analogWrite() , the code in Example 3-2 introduces a few concepts
that you haven't already encountered in this topic.
Variable Assignment
Up until now, you've stored values inside variables at the time that you cre-
ated the variable. In Example 3-2 , you'll see that in order to assign a new value,
you'll use the equal sign:
brightness = brightness + fadeAmount;
Galileo will look at the right side of the equal sign first, complete any opera-
tions, and then assign that value to the variable on the left side of the equal
sign. In this case, the initial value of brightness is added to the value of
fadeAmount . The result of that addition operation is then assigned as the new
value of brightness .
When I see an equal sign used in this way, I don't think of it as “equals” but
rather as “gets the value of.” When reading that line in the example, I would
think of it as " brightness gets the value of brightness plus fadeAmount .” This
makes it a little easier to understand what's happening in that statement.
The purpose of this line in Example 3-2 is to change the brightness every time
the loop is executed.
Another variable assignment that happens in the loop function is:
fadeAmount = -fadeAmount;
Simply put, this sets fadeAmount to its opposite. If it's a negative number, it
will make it positive. If it's a positive number, it will make it negative. When
the fadeAmount is positive, it's adding to the PWM value in every loop, making
it get brighter. Conversely, when it's negative, it's dimming the LED because
summing the brightness and -5 will decrease the brightness by 5 steps.
Math Operators
Of course, you're not limited to only adding values with Arduino. You can also
do subtraction (-), multiplication (*), division (/), and modulus (%), which
gives the remainder when dividing two numbers.
Search WWH ::




Custom Search