Project 6: A Blinking Die - Thames & Kosmos Code Gamer Experiment Manual

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 6
A blinking die
With this project, you will be turning your KosmoDuino
into a simple die. At the push of a button, it will generate a
random number from 1 to 6 and signal the result by
blinking the NeoPixel.
YOU WILL NEED
› KosmoDuino in the interaction board
THE PROGRAM
You start by including the familiar libraries and add a
KosmoBits_Pixel to address the NeoPixel on the
interaction board.
Then, you add a few constants to make the code legible.
In
setup()
, you prepare your controller for the upcoming
tasks in the usual manner: Use
pinMode(buttonPin, INPUT)
button 1 as the input pin.
Later, you will be generating a random number. This will
be handled by a random generator, which will need as
random a value as possible to start with. To get that, read
out pin 12 with
analogRead(12)
connected there, any values that you get will be random.
You will learn more about the
little later. Then
randomSeed(...)
to the random generator itself as a starting value.
Finally,
pixel.setColor(0, 0, 255, brightness);
makes the NeoPixel glow blue.
In the main
loop()
, you read out the
. When it is pushed, the result is
digitalRead()
the help of the
instruction the
if
invoked, which calculates and outputs the result of the die
roll. Otherwise,
loop()
26
CodeGamer manual inside english.indd 26
to set the pin connected to
. Since nothing is
analogRead()
function a
delivers the read value
with
buttonPin
. With
LOW
roll()
function is
does nothing.
THE PLAN:
• If the controller is ready, the NeoPixel will glow blue.
• If you press button 1, the NeoPixel will begin to blink
green in accordance with the die result, i.e., once if the
rolled result was one, twice if it was two, etc.
• After a brief pause, the NeoPixel will return to blue. That
means that it's ready to roll again.
#include <KosmoBits_Pins.h>
#include <Adafruit_NeoPixel.h>
#include <KosmoBits_Pixel.h>
KosmoBits_Pixel pixel;
const int buttonPin = KOSMOBITS_BUTTON_1_PIN;
const int blinkDuration = 500;
const int blinkPause = 250;
const int brightness = 50;
void setup() {
pinMode(buttonPin, INPUT);
randomSeed(analogRead(12));
// for random generator
pixel.setColor(0, 0, 255, brightness);
// signals operational readiness
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
roll();
}
}
</>
// Milliseconds
// Milliseconds
// Starting value
// Blue
7/19/16 12:32 PM

Advertisement

Table of Contents
loading

Table of Contents