/*
Name: Cribbage_Board.ino
Created: 7/13/2020 9:41:14 PM
Author: Christopher Cooper
*/
// Project Cribbage Board Game - Using four Arduino Adafruit NeoPixel Matrix's 8 x 8 x 4 (RGBW Configuration)
// Adafruit NeoPixel libraries.
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
// Configure NeoPixel.
const int pixelCount = 256; // Number of NeoPixels
const byte pixelPin = 6; // Digital IO pin connected to the NeoPixels
const byte matrixHeight = 8;
const byte matrixWidth = 8;
const byte tilesX = 4;
const byte tilesY = 1;
// Declare NeoPixel matrix and strip objects.
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(matrixWidth, matrixHeight, tilesX, tilesY, pixelPin,
NEO_TILE_TOP + NEO_TILE_LEFT +
NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE,
NEO_RGBW + NEO_KHZ800);
// Argument 1 = Number of rows in NeoPixel array.
// Argument 2 = Number of columns in NeoPixel array.
// Argument 3 = Number of horizontal tiles.
// Argument 4 = Number of vertical tiles.
// Argument 5 = Arduino pin number.
// From adafruit help for tiled arrays matrixWidth, matrixHeight, tilesX, tilesY, pin, matrixType, ledType);
Adafruit_NeoPixel strip(pixelCount, pixelPin, NEO_RGBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
// Configure buttons.
const byte button2PlayerShort = 15; // A1
const byte button2PlayerLong = 16; // A2
const byte button3PlayerLong = 17; // A3
boolean button2PlayerShortState = LOW;
boolean button2PlayerLongState = LOW;
boolean button3PlayerLongState = LOW;
const byte buttonWhitePin = A0;
const byte buttonRedPin = 10;
const byte buttonBluePin = 8;
const byte buttonGreenPin = 9;
const byte buttonBounce = 20;
const byte buttonDelay = 200;
boolean oldRedState = HIGH;
boolean newRedState = LOW;
boolean oldBlueState = HIGH;
boolean newBlueState = LOW;
boolean oldGreenState = HIGH;
boolean newGreenState = LOW;
// Configure initial brightness level.
byte matrixBrightnessSet = 15;
volatile boolean brightnessSet = LOW;
// Configure charging relay.
const byte chargingRelayPin = A7;
// Configure colours for both the strip and matrix libraris, as one includes the white LED (RGBW vs GRB).
const uint32_t green = strip.Color(255, 0, 0, 0);
const uint32_t red = strip.Color(0, 255, 0, 0);
const uint32_t blue = strip.Color(0, 0, 255, 0);
const uint32_t white = strip.Color(0, 0, 0, 255);
const uint32_t greenM = matrix.Color(255, 0, 0);
const uint32_t redM = matrix.Color(0, 255, 0);
const uint32_t blueM = matrix.Color(0, 0, 255);
const uint32_t whiteM = matrix.Color(255, 255, 255);
const uint32_t blankM = matrix.Color(0, 0, 0);
// Configure variables.
const int startUpDelay = 1500;
const int scrollingTextSpeed = 35;
const int cribbageBoardLayOutSpeed = 20;
boolean startUpSequence = true;
// Configure game types.
const byte twoPlayerShort = 1;
const byte twoPlayerLong = 2;
const byte threePlayerLong = 3;
byte game;
// Winning point count.
const byte shortGame = 61;
const byte longGame = 121;
byte winningCount;
// Winning player flags.
boolean redPlayerWins = LOW;
boolean bluePlayerWins = LOW;
boolean greenPlayerWins = LOW;
// Digicrib start up sequence array.
const byte digiCribStartUpSequenceArray[256] PROGMEM = { 0,8,16,24,32,40,48,56,1,9,17,25,33,41,49,57,2,10,18,26,34,42,50,58,3,11,19,27,35,43,51,59,4,12,20,28,36,44,52,60,5,13,21,29,37,45,53,61,6,14,22,30,38,46,54,62,7,15,23,31,39,47,55,
63,64,72,80,88,96,104,112,120,65,73,81,89,97,105,113,121,66,74,82,90,98,106,114,122,67,75,83,91,99,107,115,123,68,76,84,92,100,108,116,124,69,77,85,93,101,109,117,125,70,78,86,94,102,110,118,126,71,79,87,95,103,111,119,127,
128,136,144,152,160,168,176,184,129,137,145,153,161,169,177,185,130,138,146,154,162,170,178,186,131,139,147,155,163,171,179,187,132,140,148,156,164,172,180,188,133,141,149,157,165,173,181,189,134,142,150,158,166,174,182,190,135,143,151,159,167,175,183,191,
192,200,208,216,224,232,240,248,193,201,209,217,225,233,241,249,194,202,210,218,226,234,242,250,195,203,211,219,227,235,243,251,196,204,212,220,228,236,244,252,197,205,213,221,229,237,245,253,198,206,214,222,230,238,246,254,199,207,215,223,231,239,247,255 };
// Configure matrix array player layouts - 2 player short game array.
const byte cribbageBoardLayoutMatrix2PlayerShort[138] PROGMEM = { 0, 1, 2, 3, 4, 5, 6, 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135, 192, 193, 194, 195, 196, 197, 198, 199, 207, 215, 223, 222, 221, 220, 219, 218, 217, 216, 159, 158, 157, 156, 155, 154, 153, 152, 95, 94, 93, 92, 91, 90, 89, 88, 31, 30, 29, 28, 27, 26, 25, 24, 32, 33, 34, 35, 36, 37, 38, 39, 96, 97, 98, 99, 100, 101, 102, 103, 160, 161, 162, 163, 164, 165, 166, 167, 224, 225, 226, 227, 228, 229, 230, 231, 239, 247, 255, 254, 253, 252, 251, 250, 249, 248, 191, 190, 189, 188, 187, 186, 185, 184, 127, 126, 125, 124, 123, 122, 121, 120, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8 };
const byte matrixRedArray2PlayerShort[60] PROGMEM = { 9, 10, 11, 12, 13, 14, 15, 72, 73, 74, 75, 76, 77, 78, 79, 136, 137, 138, 139, 140, 141, 142, 143, 200, 201, 202, 203, 204, 205, 206, 214, 213, 212, 211, 210, 209, 208, 151, 150, 149, 148, 147, 146, 145, 144, 87, 86, 85, 84, 83, 82, 81, 80, 23, 22, 21, 20, 19, 18, 17 };
const byte matrixBlueArray2PlayerShort[60] PROGMEM = { 246, 245, 244, 243, 242, 241, 240, 183, 182, 181, 180, 179, 178, 177, 176, 119, 118, 117, 116, 115, 114, 113, 112, 55, 54, 53, 52, 51, 50, 49, 41, 42, 43, 44, 45, 46, 47, 104, 105, 106, 107, 108, 109, 110, 111, 168, 169, 170, 171, 172, 173, 174, 175, 232, 233, 234, 235, 236, 237, 238 };
// Configure matrix array player layouts - 2 player long game array.
const byte cribbageBoardLayoutMatrix2PlayerLong[16] PROGMEM = { 0, 8, 16, 24, 32, 40, 48, 56, 255, 247, 239, 231, 223, 215, 207, 199 };
const byte matrixRedArray2PlayerLong[120] PROGMEM = { 1, 2, 3, 4, 5, 6, 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135, 192, 193, 194, 195, 196, 197, 198, 206, 205, 204, 203, 202, 201, 200, 143, 142, 141, 140, 139, 138, 137, 136, 79, 78, 77, 76, 75, 74, 73, 72, 15, 14, 13, 12, 11, 10, 9, 17, 18, 19, 20, 21, 22, 23, 80, 81, 82, 83, 84, 85, 86, 87, 144, 145, 146, 147, 148, 149, 150, 151, 208, 209, 210, 211, 212, 213, 214, 222, 221, 220, 219, 218, 217, 216, 159, 158, 157, 156, 155, 154, 153, 152, 95, 94, 93, 92, 91, 90, 89, 88, 31, 30, 29, 28, 27, 26, 25 };
const byte matrixBlueArray2PlayerLong[120] PROGMEM = { 254, 253, 252, 251, 250, 249, 248, 191, 190, 189, 188, 187, 186, 185, 184, 127, 126, 125, 124, 123, 122, 121, 120, 63, 62, 61, 60, 59, 58, 57, 49, 50, 51, 52, 53, 54, 55, 112, 113, 114, 115, 116, 117, 118, 119, 176, 177, 178, 179, 180, 181, 182, 183, 240, 241, 242, 243, 244, 245, 246, 238, 237, 236, 235, 234, 233, 232, 175, 174, 173, 172, 171, 170, 169, 168, 111, 110, 109, 108, 107, 106, 105, 104, 47, 46, 45, 44, 43, 42, 41, 33, 34, 35, 36, 37, 38, 39, 96, 97, 98, 99, 100, 101, 102, 103, 160, 161, 162, 163, 164, 165, 166, 167, 224, 225, 226, 227, 228, 229, 230 };
// Configure matrix array player layouts - 3 player long game array.
const byte cribbageBoardLayoutMatrix3PlayerLong[76] PROGMEM = { 0, 8, 16, 24, 32, 40, 48, 56, 255, 247, 239, 231, 223, 215, 207, 199, 214, 213, 212, 211, 210, 209, 208, 151, 150, 149, 148, 147, 146, 145, 144, 87, 86, 85, 84, 83, 82, 81, 80, 23, 22, 21, 20, 19, 18, 17, 41, 42, 43, 44, 45, 46, 47, 104, 105, 106, 107, 108, 109, 110, 111, 168, 169, 170, 171, 172, 173, 174, 175, 232, 233, 234, 235, 236, 237, 238 };
const byte matrixRedArray3PlayerLong[60] PROGMEM = { 1, 2, 3, 4, 5, 6, 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135, 192, 193, 194, 195, 196, 197, 198, 206, 205, 204, 203, 202, 201, 200, 143, 142, 141, 140, 139, 138, 137, 136, 79, 78, 77, 76, 75, 74, 73, 72, 15, 14, 13, 12, 11, 10, 9 };
const byte matrixGreenArray3PlayerLong[60] PROGMEM = { 25, 26, 27, 28, 29, 30, 31, 88, 89, 90, 91, 92, 93, 94, 95, 152, 153, 154, 155, 156, 157, 158, 159, 216, 217, 218, 219, 220, 221, 222, 230, 229, 228, 227, 226, 225, 224, 167, 166, 165, 164, 163, 162, 161, 160, 103, 102, 101, 100, 99, 98, 97, 96, 39, 38, 37, 36, 35, 34, 33 };
const byte matrixBlueArray3PlayerLong[60] PROGMEM = { 49, 50, 51, 52, 53, 54, 55, 112, 113, 114, 115, 116, 117, 118, 119, 176, 177, 178, 179, 180, 181, 182, 183, 240, 241, 242, 243, 244, 245, 246, 254, 253, 252, 251, 250, 249, 248, 191, 190, 189, 188, 187, 186, 185, 184, 127, 126, 125, 124, 123, 122, 121, 120, 63, 62, 61, 60, 59, 58, 57 };
// Configure matrix, red, green & blue starting positions.
byte cribbageBoardLayoutMatrixPosition = 0;
byte currentRedArrayPosition = 0;
byte currentGreenArrayPosition = 0;
byte currentBlueArrayPosition = 0;
// Configure 3 Player reset options due to pixel layout, once round, this is used to recolour pixels for second round.
boolean redResetFlag = 0;
boolean blueResetFlag = 0;
boolean greenResetFlag = 0;
/*-----------------------------------------------------------------*/
void setup() {
// NeoPixel start up sequence.
strip.begin(); // Initialize NeoPixel strip object
strip.setBrightness(matrixBrightnessSet); // Manually set the brightness to avoid power problems at start up
//Function name ((color), delay).
delay(startUpDelay);
if (startUpSequence == true) {
startUpDigiCribSequence((white), 35);
} // Close if.
// Set pin modes to inputs.
pinMode(chargingRelayPin, OUTPUT); // Digital pin to enable relay for charging mode
pinMode(button2PlayerShort, INPUT);
pinMode(button2PlayerLong, INPUT);
pinMode(button3PlayerLong, INPUT);
pinMode(buttonWhitePin, INPUT_PULLUP);
pinMode(buttonRedPin, INPUT);
pinMode(buttonBluePin, INPUT);
pinMode(buttonGreenPin, INPUT);
// Read game buttons.
button2PlayerShortState = digitalRead(button2PlayerShort);
button2PlayerLongState = digitalRead(button2PlayerLong);
button3PlayerLongState = digitalRead(button3PlayerLong);
// Configure interupts.
attachInterrupt(digitalPinToInterrupt(buttonWhitePin), ISR_function, FALLING);
// Configure game option.
if (button2PlayerShortState == LOW && button2PlayerLongState == LOW && button3PlayerLongState == LOW) {
batteryCharging(red, 1000);
} // Close if.
else if (button2PlayerShortState == HIGH) {
game = twoPlayerShort;
winningCount = shortGame;
startUpMessage();
cribbageBoardLayout2PlayerShort(white);
} // Close if.
else if (button2PlayerLongState == HIGH) {
game = twoPlayerLong;
winningCount = longGame;
startUpMessage();
cribbageBoardLayout2PlayerLong(white);
} // Close if.
else if (button3PlayerLongState == HIGH) {
game = threePlayerLong;
winningCount = longGame;
startUpMessage();
cribbageBoardLayout3PlayerLong(white);
} // Close if.
} // Close setup.
/*-----------------------------------------------------------------*/
void loop() {
// Red Player - Get current button state.
newRedState = digitalRead(buttonRedPin);
// Check if state changed from high to low (button press).
if ((newRedState == LOW) && (oldRedState == HIGH)) {
// Short delay to debounce button.
delay(buttonBounce);
// Check if button is still low after debounce.
newRedState = digitalRead(buttonRedPin);
if (newRedState == HIGH) { // Yes, still low
if (game == twoPlayerShort) {
cribbageMoveRedGameOne();
oldRedState = newRedState;
} // Close if.
else if (game == twoPlayerLong) {
cribbageMoveRedGameTwo();
oldRedState = newRedState;
} // Close if.
else if (game == threePlayerLong) {
cribbageMoveRedGameThree();
oldRedState = newRedState;
} // Close if.
} // Close if.
} // Close if.
// Blue Player - Get current button state.
newBlueState = digitalRead(buttonBluePin);
// Check if state changed from high to low (button press).
if ((newBlueState == LOW) && (oldBlueState == HIGH)) {
// Short delay to debounce button.
delay(buttonBounce);
// Check if button is still low after debounce.
newBlueState = digitalRead(buttonBluePin);
if (newBlueState == HIGH) { // Yes, still low
if (game == twoPlayerShort) {
cribbageMoveBlueGameOne();
oldBlueState = newBlueState;
} // Close if.
else if (game == twoPlayerLong) {
cribbageMoveBlueGameTwo();
oldBlueState = newBlueState;
} // Close if.
else if (game == threePlayerLong) {
cribbageMoveBlueGameThree();
oldBlueState = newBlueState;
} // Close if.
} // Close if.
} // Close if.
// Green Player - Get current button state.
newGreenState = digitalRead(buttonGreenPin);
// Check if state changed from high to low (button press).
if ((newGreenState == LOW) && (oldGreenState == HIGH)) {
// Short delay to debounce button.
delay(buttonBounce);
// Check if button is still low after debounce.
newGreenState = digitalRead(buttonGreenPin);
if (newGreenState == HIGH) { // Yes, still low
if (game == twoPlayerShort) {
oldGreenState = newGreenState;
} // Close if.
else if (game == twoPlayerLong) {
oldGreenState = newGreenState;
} // Close if.
else if (game == threePlayerLong) {
cribbageMoveGreenGameThree();
oldGreenState = newGreenState;
} // Close if.
} // Close if.
} // Close if.
// Check for a winner.
if (currentRedArrayPosition == winningCount) {
redPlayerWins = HIGH;
bluePlayerWins = LOW;
greenPlayerWins = LOW;
celebrateWinner();
} // Close if.
else if (currentBlueArrayPosition == winningCount) {
redPlayerWins = LOW;
bluePlayerWins = HIGH;
greenPlayerWins = LOW;
celebrateWinner();
} // Close if.
else if (currentGreenArrayPosition == winningCount) {
redPlayerWins = LOW;
bluePlayerWins = LOW;
greenPlayerWins = HIGH;
celebrateWinner();
} // Close if.
while (brightnessSet == HIGH) {
matrixBrightness(); // Set brightness
} // Close if.
} // Close loop.
/*-----------------------------------------------------------------*/
void ISR_function() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{
if (brightnessSet == LOW) {
brightnessSet = HIGH;
}
else if (brightnessSet == HIGH) {
brightnessSet = LOW;
}
}
last_interrupt_time = interrupt_time;
} // Close function.
/*-----------------------------------------------------------------*/
void startUpDigiCribSequence(uint32_t color, int wait) {
matrix.clear();
matrix.show();
delay(startUpDelay);
// Matrix sweap left across.
for (int s = 0; s < 256; s++) {
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
} // Close for.
// Matrix sweap right across.
for (int s = 255; s > 0; s--) {
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
s--;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
} // Close for.
// Matrix sweap left across once more.
for (int s = 0; s < 256; s++) {
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
s++;
strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
} // Close for.
// Matrix increase brightness.
for (int i = 15; i < 95; i++) {
matrix.setBrightness(i);
matrix.fill(white, 0, pixelCount);
matrix.show();
delay(20);
}
// Clear matrix.
matrix.clear();
matrix.show();
delay(startUpDelay / 2);
// Digi Crib splash name.
matrix.fillScreen(0);
matrix.setCursor(5, 0);
matrix.setBrightness(60);
matrix.setTextColor(whiteM);
matrix.print(F("Digi"));
matrix.show();
delay(startUpDelay);
matrix.clear();
matrix.show();
matrix.fillScreen(0);
matrix.setCursor(5, 0);
matrix.setBrightness(60);
matrix.setTextColor(whiteM);
matrix.print(F("Crib"));
matrix.show();
delay(startUpDelay * 2);
strip.clear();
strip.show();
// Stop sequence from repeating after game played until power off.
startUpSequence = false;
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageBoardLayout2PlayerShort(uint32_t white) {
for (byte i = 0; i < 138; i++) { // For each pixel in strip...
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix2PlayerShort[i]))), white); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(cribbageBoardLayOutSpeed); // Pause for a moment
} // Close for.
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageBoardLayout2PlayerLong(uint32_t white) {
for (byte i = 0; i < 16; i++) { // For each pixel in strip...
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix2PlayerLong[i]))), white); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(cribbageBoardLayOutSpeed); // Pause for a moment
} // Close for.
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageBoardLayout3PlayerLong(uint32_t white) {
for (byte i = 0; i < 76; i++) { // For each pixel in strip...
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix3PlayerLong[i]))), white); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(cribbageBoardLayOutSpeed); // Pause for a moment
} // Close for.
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveRedGameOne(void) {
byte currentRedLED = (pgm_read_byte(&(matrixRedArray2PlayerShort[currentRedArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentRedLED, red);
strip.show();
// Count red position.
currentRedArrayPosition++;
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveBlueGameOne(void) {
byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray2PlayerShort[currentBlueArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentBlueLED, blue);
strip.show();
// Count blue position.
currentBlueArrayPosition++;
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveRedGameTwo(void) {
byte currentRedLED = (pgm_read_byte(&(matrixRedArray2PlayerLong[currentRedArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentRedLED, red);
strip.show();
// Count red position.
currentRedArrayPosition++;
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveBlueGameTwo(void) {
byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray2PlayerLong[currentBlueArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentBlueLED, blue);
strip.show();
// Count blue position.
currentBlueArrayPosition++;
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveRedGameThree(void) {
if (currentRedArrayPosition <= 59) {
byte currentRedLED = (pgm_read_byte(&(matrixRedArray3PlayerLong[currentRedArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentRedLED, red);
strip.show();
// Count red position.
currentRedArrayPosition++;
} // Close if.
else if (redResetFlag == 0) {
threePlayerRedReset();
redResetFlag = 1;
currentRedArrayPosition = 0;
} // Close else if.
else if (currentRedArrayPosition == 60 && redResetFlag == 1) {
redPlayerWins = HIGH;
bluePlayerWins = LOW;
greenPlayerWins = LOW;
celebrateWinner();
redResetFlag = 0;
} // Close else if.
} // Close function.
/*-----------------------------------------------------------------*/
void threePlayerRedReset(void) {
for (byte i = 0; i < 60; i++) { // For each pixel in strip...
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor((pgm_read_byte(&(matrixRedArray3PlayerLong[i]))), white); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(cribbageBoardLayOutSpeed); // Pause for a moment
} // Close for.
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveBlueGameThree(void) {
if (currentBlueArrayPosition <= 59) {
byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray3PlayerLong[currentBlueArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentBlueLED, blue);
strip.show();
// Count blue position.
currentBlueArrayPosition++;
} // Close if.
else if (blueResetFlag == 0) {
threePlayerBlueReset();
blueResetFlag = 1;
currentBlueArrayPosition = 0;
} // Close else if.
else if (currentBlueArrayPosition == 60 && blueResetFlag == 1) {
redPlayerWins = LOW;
bluePlayerWins = HIGH;
greenPlayerWins = LOW;
celebrateWinner();
blueResetFlag = 0;
} // Close else if.
} // Close function.
/*-----------------------------------------------------------------*/
void threePlayerBlueReset(void) {
for (byte i = 0; i < 60; i++) { // For each pixel in strip...
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor((pgm_read_byte(&(matrixBlueArray3PlayerLong[i]))), white); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(cribbageBoardLayOutSpeed); // Pause for a moment
} // Close for.
} // Close function.
/*-----------------------------------------------------------------*/
void cribbageMoveGreenGameThree(void) {
if (currentGreenArrayPosition <= 59) {
byte currentGreenLED = (pgm_read_byte(&(matrixGreenArray3PlayerLong[currentGreenArrayPosition])));
// Set pixel colour and start.
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor(currentGreenLED, green);
strip.show();
// Count green position.
currentGreenArrayPosition++;
} // Close if.
else if (greenResetFlag == 0) {
threePlayerGreenReset();
greenResetFlag = 1;
currentGreenArrayPosition = 0;
} // Close else if.
else if (currentGreenArrayPosition == 60 && greenResetFlag == 1) {
redPlayerWins = LOW;
bluePlayerWins = LOW;
greenPlayerWins = HIGH;
celebrateWinner();
greenResetFlag = 0;
} // Close else if.
} // Close function.
/*-----------------------------------------------------------------*/
void threePlayerGreenReset(void) {
for (byte i = 0; i < 60; i++) { // For each pixel in strip...
strip.setBrightness(matrixBrightnessSet);
strip.setPixelColor((pgm_read_byte(&(matrixGreenArray3PlayerLong[i]))), white); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(cribbageBoardLayOutSpeed); // Pause for a moment
} // Close for.
} // Close function.
/*-----------------------------------------------------------------*/
void celebrateWinner(void) {
// Start matrix for winning player text.
matrix.begin();
matrix.setTextWrap(false);
// Check if red player has won?
if (redPlayerWins == HIGH) {
// Configure and display text.
int x = matrix.width();
while (x > -145) { // Only display text for one pass
matrix.fillScreen(0);
matrix.setBrightness(matrixBrightnessSet);
matrix.setCursor(x, 0);
matrix.setTextColor(redM);
matrix.print(F("Red Player Wins!!!"));
if (--x < -145) {
x = matrix.width();
}
matrix.show();
delay(scrollingTextSpeed);
} // Close while.
// Reset width and flags.
x = matrix.width();
redPlayerWins = LOW;
} // Close if.
// Check if blue player has won?
else if (bluePlayerWins == HIGH) {
// Configure and display text.
int x = matrix.width();
while (x > -145) { // Only display text for one pass
matrix.fillScreen(0);
matrix.setBrightness(matrixBrightnessSet);
matrix.setCursor(x, 0);
matrix.setTextColor(blueM);
matrix.print(F("Blue Player Wins!!!"));
if (--x < -145) {
x = matrix.width();
}
matrix.show();
delay(scrollingTextSpeed);
} // Close while.
// Reset width and flags.
x = matrix.width();
bluePlayerWins = LOW;
} // Close if.
// Check if green player has won?
else if (greenPlayerWins == HIGH) {
// Configure and display text.
int x = matrix.width();
while (x > -145) { // Only display text for one pass
matrix.fillScreen(0);
matrix.setBrightness(matrixBrightnessSet);
matrix.setCursor(x, 0);
matrix.setTextColor(greenM);
matrix.print(F("Green Player Wins!!!"));
if (--x < -145) {
x = matrix.width();
}
matrix.show();
delay(scrollingTextSpeed);
} // Close while.
// Reset width and flags.
x = matrix.width();
greenPlayerWins = LOW;
} // Close if.
// Reset player array positions.
currentRedArrayPosition = 0;
currentBlueArrayPosition = 0;
currentGreenArrayPosition = 0;
redResetFlag = 0;
blueResetFlag = 0;
greenResetFlag = 0;
redPlayerWins = LOW;
bluePlayerWins = LOW;
greenPlayerWins = LOW;
// Clear down display.
strip.clear();
strip.show();
// Restart game.
setup();
} // Close function.
/*-----------------------------------------------------------------*/
int matrixBrightness(void) {
while (brightnessSet == HIGH) { // Brightness menu flag set by interupt request
// Setting the display brightness
if (digitalRead(buttonBluePin) == HIGH)
{
if (matrixBrightnessSet == 60)
{
matrixBrightnessSet = 5;
}
else
{
matrixBrightnessSet = matrixBrightnessSet + 5;
}
}
if (digitalRead(buttonGreenPin) == HIGH)
{
if (matrixBrightnessSet == 5)
{
matrixBrightnessSet = 60;
}
else
{
matrixBrightnessSet = matrixBrightnessSet - 5;
}
}
matrix.clear();
matrix.setCursor(1, 0);
matrix.setTextColor(whiteM);
matrix.setBrightness(matrixBrightnessSet);
matrix.print(F("Br:"));
matrix.show();
matrix.print(matrixBrightnessSet);
matrix.show();
delay(buttonDelay);
} // Close while.
saveBrightness();
return;
} // Close function.
/*-----------------------------------------------------------------*/
void startUpMessage(void) {
// Configure matrix.
matrix.setBrightness(matrixBrightnessSet);
matrix.setTextWrap(false);
// Check game type.
if (game == twoPlayerShort) {
// Configure and display text.
int x = matrix.width();
while (x > -145) { // Only display text for one pass
matrix.fillScreen(0);
matrix.setCursor(x, 0);
matrix.setTextColor(whiteM);
matrix.print(F("Game: 2 Player Short"));
if (--x < -145) {
x = matrix.width();
}
matrix.show();
delay(scrollingTextSpeed);
} // Close while.
// Reset width and flags.
x = matrix.width(); // This resets the test to the beginning again
} // Close if.
// Check game type.
else if (game == twoPlayerLong) {
// Configure and display text.
int x = matrix.width();
while (x > -145) { // Only display text for one pass
matrix.fillScreen(0);
matrix.setCursor(x, 0);
matrix.setTextColor(whiteM);
matrix.print(F("Game: 2 Player Long"));
if (--x < -145) {
x = matrix.width();
}
matrix.show();
delay(scrollingTextSpeed);
} // Close while.
// Reset width and flags.
x = matrix.width(); // This resets the test to the beginning again
} // Close if.
// Check game type.
else if (game == threePlayerLong) {
// Configure and display text.
int x = matrix.width();
while (x > -145) { // Only display text for one pass
matrix.fillScreen(0);
matrix.setCursor(x, 0);
matrix.setTextColor(whiteM);
matrix.print(F("Game: 3 Player Long"));
if (--x < -145) {
x = matrix.width();
}
matrix.show();
delay(scrollingTextSpeed);
} // Close while.
// Reset width and flags.
x = matrix.width(); // This resets the test to the beginning again
} // Close if.
} // Close function.
/*-----------------------------------------------------------------*/
void batteryCharging(uint32_t color, int wait) {
while (button2PlayerShortState == LOW && button2PlayerLongState == LOW && button3PlayerLongState == LOW) {
// Read game buttons.
button2PlayerShortState = digitalRead(button2PlayerShort);
button2PlayerLongState = digitalRead(button2PlayerLong);
button3PlayerLongState = digitalRead(button3PlayerLong);
// Blink two leds red to indicate charging mode enabled.
strip.setBrightness(5);
strip.setPixelColor(0, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to matc
delay(wait); // Pause for a moment
strip.clear();
strip.show();
strip.setPixelColor(1, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to matc
delay(wait); // Pause for a moment
strip.clear();
strip.show();
// Enable charging relay pin to switch on, onboard NiCd charging circuit.
digitalWrite(chargingRelayPin, HIGH);
} // Close while.
// Disable charging relay pin to switch off, onboard NiCd charging circuit.
digitalWrite(chargingRelayPin, LOW);
// Restart game.
setup();
} // Close function.
/*-----------------------------------------------------------------*/
void saveBrightness() {
// Display simple set message.
matrix.clear();
matrix.setCursor(7, 0);
matrix.print(F("Set"));
matrix.show();
delay(1000);
// Re-running game layout options to force update the display from brightness screen to game screen.
if (button2PlayerShortState == HIGH) {
cribbageBoardLayout2PlayerShort(white);
} // Close if.
else if (button2PlayerLongState == HIGH) {
cribbageBoardLayout2PlayerLong(white);
} // Close if.
else if (button3PlayerLongState == HIGH) {
cribbageBoardLayout3PlayerLong(white);
} // Close if.
}
/*-----------------------------------------------------------------*/
Comments