Hardware Reference
In-Depth Information
String gatekeeperString = "There is no Dana, only Zuul!";
There are many other ways to create
String
objects and to work with them.
Check out the
Arduino Reference
for more information.
millis()
Keeping track of time can be very helpful in an Arduino sketch. In
Example 3-1
, you tried out the
delay()
function, which stops a sketch for the
specified number of milliseconds. But what if you just want to set up a few
things to happen on different intervals? That's just one way that
millis()
comes in handy.
The
millis()
function returns the number of milliseconds since the sketch
started. You can then check that time against the time you expect something
to execute. If the current time is past the time you expected it to execute,
then you execute the code and set the next time you expect it to execute.
To explore this concept further, read through the Arduino example BlinkWi-
thoutDelay. From within the IDE, click File
→
Examples
→
02.Digital
→
Blink-
WithoutDelay.
Other Loops
In
Chapter 3
you first encountered the
loop
function, which executes its block
of code repeatedly until the board is reset or power is disconnected. But what
if you want something to happen repeatedly
within
the main
loop
function?
Or what if you want to loop through a bunch of pins within the
setup
function?
There are a few different types of loops available to you for just this purpose.
while
A
while
loop simply executes a block of code over and over again as long as
the condition is true. The syntax is:
while (condition) {
execute this code repeatedly as long as condition is true
}
If the condition isn't true when Galileo gets to that point in the code, it simply
won't execute the block of code at all.
do… while
A
do... while
loop, on the other hand, executes a block of code and
then
checks for a condition. If that condition is true, it executes the block of code
again. This continues until the condition evaluates as false.