EasyManua.ls Logo

Thames & Kosmos Code Gamer - IN IDLE Mode; COUNTDOWN Mode

Thames & Kosmos Code Gamer
66 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
THE MATRIX
In this project, we will be using the NeoPixel and both
buttons. So we will link to the required libraries here.
#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); // Pixel off
mode = IDLE;
counter = 0;
Serial.println("Reset!");
}
</>
We are defining three constants for reading the sensor and
the buttons, as well as the three variables
button1
,
button2
, and
sensorValue
, which will store the current
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
enum Mode
to define a so-
called enumeration type: a variable of the
Mode
type can
only take the values written between the curly brackets —
IDLE
,
COUNTDOWN
, etc.
Next, we define the
mode
variable in which the current
operating mode is stored. We begin in
IDLE
mode.
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
counter
variable stores the number of times the
drawer is opened. Finally,
pixel
gives us access to the
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
r e s e t()
function. The NeoPixel is switched off,
mode
is set to
IDLE
and
counter
back to
0
. As a check, “Reset!” is
output via the serial interface.

PROJECT 17
CodeGamer
CodeGamer manual inside english.indd 49 7/19/16 12:32 PM