Programming Arduino Getting Started with Sketches


delayPeriod variable is the old value of  delayPeriod



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə23/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   19   20   21   22   23   24   25   26   ...   78
Arduino SimonMonk 2011

delayPeriod
variable is the old value of 
delayPeriod
plus
100.
Let’s test out this new mechanism to see what the Arduino is up to by entering the
following sketch, running it, and opening the Serial Monitor:


Figure 3-7
shows what you should see in the Serial Monitor after this code runs.
To take a slightly more complex example, the formula for converting a temperature in
degrees Centigrade into degrees Fahrenheit is to multiply it by 5, divide by 9, and then add
32. So you could write that in a sketch like this:
Figure 3-7
Simple arithmetic
There are a few things to notice here. First, note the following line:
When we write such a line, we are actually doing two things: We are declaring an 
int
variable called 
degC
, and we are saying that its initial value will be 20. Alternatively, you
could separate these two things and write the following:
You must declare any variable just once, essentially telling the compiler what type of
variable it is—in this case, 
int
. However, you can assign the variable a value as many
times as you want:


So, in the Centigrade to Fahrenheit example, you are defining the variable 
degC
and
giving it an initial value of 20, but when you define 
degF
, it does not get an initial value.
Its value gets assigned on the next line, according to the conversion formula, before being
sent to the Serial Monitor for you to see.
Looking at the expression, you can see that you use the asterisk (
*
) for multiplication
and the slash (
/
) for division. The arithmetic operators 
+, −, *
, and 
/
have an order of
precedence—that is, multiplications are done first, then divisions, then additions and
subtractions. This is in accordance with the usual use of arithmetic. However, sometimes it
makes it clearer to use parentheses in the expressions. So, for example, you could write
the following:
The expressions that you write can be as long and complex as you need them to be, and
in addition to the usual arithmetic operators, there are other less commonly used operators
and a big collection of various mathematical functions that are available to you. You will
learn about these later.

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   19   20   21   22   23   24   25   26   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin