![Thames & Kosmos Code Gamer Experiment Manual Download Page 29](http://html1.mh-extra.com/html/thames-and-kosmos/code-gamer/code-gamer_experiment-manual_1099224029.webp)
loop on pages 30/31
</>
Here’s where you define your own function by the name
of
roll()
. It will calculate and then output a random
number in the range of 1 to 6. Let’s go through it step by
step:
First (1), you turn off the NeoPixel light and wait briefly.
You know the corresponding commands by now.
Now we come to the actual roll. Since your KosmoDuino
cannot literally roll dice, you have to get the result by
some other means: You query a
random number
generator.
You do that with
random(1, 7)
(2). That lets you ask the
random generator for a randomly selected number. The
first parameter, 1 in this case, indicates that the smallest
possible number is to be 1. The second parameter, 7 in this
case, means that the randomly selected number should be
less than 7.
Important: The second parameter must always be 1
greater than the largest possible number to be produced
by the random generator. That may be a little confusing,
but it’s just how it is. The random number produced in this
way is saved in the
number
variable.
Now the random number is to be output by blinking (3).
For that, you make use of a so-called
for
loop. You will
learn all about the composition of the
for
loop on the
following pages. For now, the only important thing to
know is that the code in the curly brackets is carried out
exactly the indicated
number
of times. If
number
has
the value of 1, it will be 1 time. If
number
has the value
of 2, then 2 times, and so on.
What’s happening inside the curly brackets? Exactly —
The NeoPixel lights up in green for the length of time
indicated by
blinkDuration
and then switches off for
the length of time indicated by
blinkPause
. In brief: The
NeoPixel blinks green a single time. But since the
for
loop is executed
number
times, the NeoPixel blinks
number
times altogether. So the result of the roll is
output by blinking.
Finally (4), after another short pause
(
delay(blinkDuration)
) the NeoPixel returns to blue to
signal that you can roll again.
Since
roll()
is only invoked from the main loop
loop()
in your program, it then returns to the main
loop. So the next thing that happens is another check in
loop()
to see whether the button is pressed or not.
Have fun rolling!
You will find more information about the
for
loop on
pages 30 and 31.
void roll() {
// (1)
// Light off at first, to show that it is
// ready to respond to the push of the button.
pixel.setColor(0, 0, 0, 0);
// Light off
delay(500);
// (2) Generate random number
int number = random(1, 7);
// (3) Blink number times
for (int i = 0; i < number; ++i) {
pixel.setColor(0, 255, 0, brightness);
// Green
delay(blinkDuration);
pixel.setColor(0, 0, 0, 0);
// Light off
delay(blinkPause);
}
// (4) Signal conclusion of rolling.
delay(blinkDuration);
// Pause at end
// somewhat longer
pixel.setColor(0, 0, 255, brightness);
// Blue
// signals state of readiness
}
27
PROJECT 6
CodeGamer
CodeGamer manual inside english.indd 27
7/19/16 12:32 PM