Thames & Kosmos Code Gamer Experiment Manual page 51

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 17
THE MATRIX
In this project, we will be using the NeoPixel and both
buttons. So we will link to the required libraries here.
We are defining three constants for reading the sensor and
the buttons, as well as the three variables
, and
button2
sensorValue
measurement readings.
To make sure the program code remains clear, it helps to
think of the program as a machine that can be operated in
various operating modes. Different things happen in the
main loop depending on the mode.
For that purpose, we are using
called enumeration type: a variable of the
only take the values written between the curly brackets —
,
, etc.
IDLE
COUNTDOWN
Next, we define the
mode
operating mode is stored. We begin in
The
DARK _ VALUE
constant specifies the lowest light
sensor reading at which the drawer should start to count
as open. A lot of drawers will let in a little light even when
they are closed. So adjust the value to what makes sense
for yours. The readings that you took in your preliminary
work should help!
The
variable stores the number of times the
counter
drawer is opened. Finally,
NeoPixel.
When the surveillance is over, we will want to return to
the starting state — as if we had just turned on the
KosmoDuino. For that, we are introducing the
function. The NeoPixel is switched off,
and
back to
IDLE
counter
output via the serial interface.
CodeGamer manual inside english.indd 49
,
button1
, which will store the current
to define a so-
enum Mode
type can
Mode
variable in which the current
mode.
IDLE
pixel
gives us access to the
reset()
is set to
mode
. As a check, "Reset!" is
0
#include <Adafruit_NeoPixel.h>
#include <KosmoBits_Pins.h>
#include <KosmoBits_Pixel.h>
const int sensorPin = KOSMOBITS_SENSOR_PIN;
const int button1Pin = KOSMOBITS_BUTTON_1_PIN;
const int button2Pin = KOSMOBITS_BUTTON_2_PIN;
int button1 = HIGH;
int button2 = HIGH;
int sensorValue = 0;
enum Mode {IDLE, COUNTDOWN, WAIT_UNTIL_DARK,
CLOSED, OPEN};
Mode mode = IDLE;
const int DARK_VALUE = 100;
unsigned long counter = 0;
KosmoBits_Pixel pixel;
void reset() {
pixel.setColor(0, 0, 0, 0);
mode = IDLE;
counter = 0;
Serial.println("Reset!");
}
CodeGamer
</>
// Pixel off
49
7/19/16 12:32 PM

Advertisement

Table of Contents
loading

Table of Contents