Thames & Kosmos Code Gamer Experiment Manual page 29

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 6
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)
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
Now the random number is to be output by blinking (3).
For that, you make use of a so-called
learn all about the composition of 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
the value of 1, it will be 1 time. If
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
the length of time indicated by
NeoPixel blinks green a single time. But since the
loop is executed
number
times altogether. So the result of the roll is
number
output by blinking.
Finally (4), after another short pause
(
delay(blinkDuration)
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.
CodeGamer manual inside english.indd 27
(2). That lets you ask the
variable.
loop. You will
for
loop on the
for
of times. If
has
number
number
has the value
number
and then switches off for
. In brief: The
blinkPause
for
times, the NeoPixel blinks
) the NeoPixel returns to blue to
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);
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);
delay(blinkPause);
}
// (4) Signal conclusion of rolling.
delay(blinkDuration);
// somewhat longer
pixel.setColor(0, 0, 255, brightness);
// signals state of readiness
}
loop on pages 30/31
Have fun rolling!
You will find more information about the
pages 30 and 31.
CodeGamer
</>
// Light off
// Light off
// Pause at end
// Blue
loop on
for
27
7/19/16 12:32 PM

Advertisement

Table of Contents
loading

Table of Contents