Components and supplies
M3 Hex Male x Female Nylon Screws Nut Standoff Spacers
5V One Channel Relay Module Board Shield
5.5 x 2.1 MM DC Power Jack Socket DC Threaded Female Mount Connector Adapter
Resistor 330 ohm
300v 15A 5mm Pitch 2 Pin/3 Pin PCB Mount Screw Terminal Block Blue
AA 12V Battery Holder Case Plastic Battery Storage Box
LRFs (Little Rubber Feet)
Rectifier Diode IN5404
8S NiMH NiCd Rechargeable Battery Charger Charging Module Board
DC 12V 20A Car Boat Truck Trailer Auto Illuminated Round Rocker Switch
Arduino Nano Every
Adafruit NeoPixel NeoMatrix - 64 RGBW - Cool White
Capacitor 1000 µF
LM2596 DC-DC Buck Converter Step Down Module High Efficiency Voltage Regulator
M3 Screw Nuts Assortment
Waterproof Momentary Push Button SPST ON/OFF
Resistor 10k ohm
90'' Left Angle Micro USB 5pin Male to USB B Female Panel Mount Printer Cable
M3 304 Stainless Steel Cross Recessed Countersunk
Ultra Small MP1584EN DC-DC Buck Converter
Universal Switch Rotary Switch 4 Pin Positions
Tools and machines
Preciva Crimper Plier Set, 0.25-10mm²/AWG23-10
Solder Wire, Lead Free
Soldering iron (generic)
KNIPEX 12 42 195 MultiStrip 10 Automatic Insulation Stripper
Preciva Dupont Crimping Tool AWG26-18
KNIPEX 78 61 125 Electronic Super Knips
Multitool, Screwdriver
Desoldering Pump, Deluxe SOLDAPULLT®
Project description
Code
DigiCrib - Digital Cribbage Board
arduino
It is a digital scoring board for those who play Cribbage
1/* 2 Name: Cribbage_Board.ino 3 Created: 7/13/2020 9:41:14 PM 4 Author: Christopher Cooper 5*/ 6 7// Project Cribbage Board Game - Using four Arduino Adafruit NeoPixel Matrix's 8 x 8 x 4 (RGBW Configuration) 8 9// Adafruit NeoPixel libraries. 10 11#include <Adafruit_GFX.h> 12#include <Adafruit_NeoMatrix.h> 13#include <Adafruit_NeoPixel.h> 14 15// Configure NeoPixel. 16 17const int pixelCount = 256; // Number of NeoPixels 18const byte pixelPin = 6; // Digital IO pin connected to the NeoPixels 19const byte matrixHeight = 8; 20const byte matrixWidth = 8; 21const byte tilesX = 4; 22const byte tilesY = 1; 23 24// Declare NeoPixel matrix and strip objects. 25 26Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(matrixWidth, matrixHeight, tilesX, tilesY, pixelPin, 27 28 NEO_TILE_TOP + NEO_TILE_LEFT + 29 NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE, 30 NEO_RGBW + NEO_KHZ800); 31 32// Argument 1 = Number of rows in NeoPixel array. 33// Argument 2 = Number of columns in NeoPixel array. 34// Argument 3 = Number of horizontal tiles. 35// Argument 4 = Number of vertical tiles. 36// Argument 5 = Arduino pin number. 37// From adafruit help for tiled arrays matrixWidth, matrixHeight, tilesX, tilesY, pin, matrixType, ledType); 38 39Adafruit_NeoPixel strip(pixelCount, pixelPin, NEO_RGBW + NEO_KHZ800); 40 41// Argument 1 = Number of pixels in NeoPixel strip 42// Argument 2 = Arduino pin number (most are valid) 43// Argument 3 = Pixel type flags, add together as needed: 44 45// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) 46// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) 47// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) 48// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) 49// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) 50 51// Configure buttons. 52 53const byte button2PlayerShort = 15; // A1 54const byte button2PlayerLong = 16; // A2 55const byte button3PlayerLong = 17; // A3 56 57boolean button2PlayerShortState = LOW; 58boolean button2PlayerLongState = LOW; 59boolean button3PlayerLongState = LOW; 60 61const byte buttonWhitePin = A0; 62const byte buttonRedPin = 10; 63const byte buttonBluePin = 8; 64const byte buttonGreenPin = 9; 65 66const byte buttonBounce = 20; 67const byte buttonDelay = 200; 68 69boolean oldRedState = HIGH; 70boolean newRedState = LOW; 71boolean oldBlueState = HIGH; 72boolean newBlueState = LOW; 73boolean oldGreenState = HIGH; 74boolean newGreenState = LOW; 75 76// Configure initial brightness level. 77 78byte matrixBrightnessSet = 15; 79volatile boolean brightnessSet = LOW; 80 81// Configure charging relay. 82 83const byte chargingRelayPin = A7; 84 85// Configure colours for both the strip and matrix libraris, as one includes the white LED (RGBW vs GRB). 86 87const uint32_t green = strip.Color(255, 0, 0, 0); 88const uint32_t red = strip.Color(0, 255, 0, 0); 89const uint32_t blue = strip.Color(0, 0, 255, 0); 90const uint32_t white = strip.Color(0, 0, 0, 255); 91 92const uint32_t greenM = matrix.Color(255, 0, 0); 93const uint32_t redM = matrix.Color(0, 255, 0); 94const uint32_t blueM = matrix.Color(0, 0, 255); 95const uint32_t whiteM = matrix.Color(255, 255, 255); 96const uint32_t blankM = matrix.Color(0, 0, 0); 97 98// Configure variables. 99 100const int startUpDelay = 1500; 101const int scrollingTextSpeed = 35; 102const int cribbageBoardLayOutSpeed = 20; 103boolean startUpSequence = true; 104 105// Configure game types. 106 107const byte twoPlayerShort = 1; 108const byte twoPlayerLong = 2; 109const byte threePlayerLong = 3; 110 111byte game; 112 113// Winning point count. 114 115const byte shortGame = 61; 116const byte longGame = 121; 117 118byte winningCount; 119 120// Winning player flags. 121 122boolean redPlayerWins = LOW; 123boolean bluePlayerWins = LOW; 124boolean greenPlayerWins = LOW; 125 126// Digicrib start up sequence array. 127 128const 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, 129 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, 130 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, 131 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 }; 132 133// Configure matrix array player layouts - 2 player short game array. 134 135const 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 }; 136 137const 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 }; 138const 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 }; 139 140// Configure matrix array player layouts - 2 player long game array. 141 142const byte cribbageBoardLayoutMatrix2PlayerLong[16] PROGMEM = { 0, 8, 16, 24, 32, 40, 48, 56, 255, 247, 239, 231, 223, 215, 207, 199 }; 143 144const 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 }; 145const 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 }; 146 147// Configure matrix array player layouts - 3 player long game array. 148 149const 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 }; 150 151const 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 }; 152const 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 }; 153const 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 }; 154 155// Configure matrix, red, green & blue starting positions. 156 157byte cribbageBoardLayoutMatrixPosition = 0; 158byte currentRedArrayPosition = 0; 159byte currentGreenArrayPosition = 0; 160byte currentBlueArrayPosition = 0; 161 162// Configure 3 Player reset options due to pixel layout, once round, this is used to recolour pixels for second round. 163 164boolean redResetFlag = 0; 165boolean blueResetFlag = 0; 166boolean greenResetFlag = 0; 167 168/*-----------------------------------------------------------------*/ 169 170void setup() { 171 172 // NeoPixel start up sequence. 173 174 strip.begin(); // Initialize NeoPixel strip object 175 strip.setBrightness(matrixBrightnessSet); // Manually set the brightness to avoid power problems at start up 176 177 //Function name ((color), delay). 178 179 delay(startUpDelay); 180 181 if (startUpSequence == true) { 182 183 startUpDigiCribSequence((white), 35); 184 185 } // Close if. 186 187 // Set pin modes to inputs. 188 189 pinMode(chargingRelayPin, OUTPUT); // Digital pin to enable relay for charging mode 190 pinMode(button2PlayerShort, INPUT); 191 pinMode(button2PlayerLong, INPUT); 192 pinMode(button3PlayerLong, INPUT); 193 194 pinMode(buttonWhitePin, INPUT_PULLUP); 195 pinMode(buttonRedPin, INPUT); 196 pinMode(buttonBluePin, INPUT); 197 pinMode(buttonGreenPin, INPUT); 198 199 // Read game buttons. 200 201 button2PlayerShortState = digitalRead(button2PlayerShort); 202 button2PlayerLongState = digitalRead(button2PlayerLong); 203 button3PlayerLongState = digitalRead(button3PlayerLong); 204 205 // Configure interupts. 206 207 attachInterrupt(digitalPinToInterrupt(buttonWhitePin), ISR_function, FALLING); 208 209 // Configure game option. 210 211 if (button2PlayerShortState == LOW && button2PlayerLongState == LOW && button3PlayerLongState == LOW) { 212 213 batteryCharging(red, 1000); 214 215 } // Close if. 216 217 else if (button2PlayerShortState == HIGH) { 218 219 game = twoPlayerShort; 220 winningCount = shortGame; 221 startUpMessage(); 222 cribbageBoardLayout2PlayerShort(white); 223 224 } // Close if. 225 226 else if (button2PlayerLongState == HIGH) { 227 228 game = twoPlayerLong; 229 winningCount = longGame; 230 startUpMessage(); 231 cribbageBoardLayout2PlayerLong(white); 232 233 } // Close if. 234 235 else if (button3PlayerLongState == HIGH) { 236 237 game = threePlayerLong; 238 winningCount = longGame; 239 startUpMessage(); 240 cribbageBoardLayout3PlayerLong(white); 241 242 } // Close if. 243 244} // Close setup. 245 246/*-----------------------------------------------------------------*/ 247 248void loop() { 249 250 // Red Player - Get current button state. 251 252 newRedState = digitalRead(buttonRedPin); 253 254 // Check if state changed from high to low (button press). 255 256 if ((newRedState == LOW) && (oldRedState == HIGH)) { 257 258 // Short delay to debounce button. 259 260 delay(buttonBounce); 261 262 // Check if button is still low after debounce. 263 264 newRedState = digitalRead(buttonRedPin); 265 266 if (newRedState == HIGH) { // Yes, still low 267 268 if (game == twoPlayerShort) { 269 270 cribbageMoveRedGameOne(); 271 oldRedState = newRedState; 272 273 } // Close if. 274 275 else if (game == twoPlayerLong) { 276 277 cribbageMoveRedGameTwo(); 278 oldRedState = newRedState; 279 280 } // Close if. 281 282 else if (game == threePlayerLong) { 283 284 cribbageMoveRedGameThree(); 285 oldRedState = newRedState; 286 287 } // Close if. 288 289 } // Close if. 290 291 } // Close if. 292 293 // Blue Player - Get current button state. 294 295 newBlueState = digitalRead(buttonBluePin); 296 297 // Check if state changed from high to low (button press). 298 299 if ((newBlueState == LOW) && (oldBlueState == HIGH)) { 300 301 // Short delay to debounce button. 302 303 delay(buttonBounce); 304 305 // Check if button is still low after debounce. 306 307 newBlueState = digitalRead(buttonBluePin); 308 309 if (newBlueState == HIGH) { // Yes, still low 310 311 if (game == twoPlayerShort) { 312 313 cribbageMoveBlueGameOne(); 314 oldBlueState = newBlueState; 315 316 } // Close if. 317 318 else if (game == twoPlayerLong) { 319 320 cribbageMoveBlueGameTwo(); 321 oldBlueState = newBlueState; 322 323 } // Close if. 324 325 else if (game == threePlayerLong) { 326 327 cribbageMoveBlueGameThree(); 328 oldBlueState = newBlueState; 329 330 } // Close if. 331 332 } // Close if. 333 334 } // Close if. 335 336 // Green Player - Get current button state. 337 338 newGreenState = digitalRead(buttonGreenPin); 339 340 // Check if state changed from high to low (button press). 341 342 if ((newGreenState == LOW) && (oldGreenState == HIGH)) { 343 344 // Short delay to debounce button. 345 346 delay(buttonBounce); 347 348 // Check if button is still low after debounce. 349 350 newGreenState = digitalRead(buttonGreenPin); 351 352 if (newGreenState == HIGH) { // Yes, still low 353 354 if (game == twoPlayerShort) { 355 356 oldGreenState = newGreenState; 357 358 } // Close if. 359 360 else if (game == twoPlayerLong) { 361 362 oldGreenState = newGreenState; 363 364 } // Close if. 365 366 else if (game == threePlayerLong) { 367 368 cribbageMoveGreenGameThree(); 369 oldGreenState = newGreenState; 370 371 } // Close if. 372 373 } // Close if. 374 375 } // Close if. 376 377 // Check for a winner. 378 379 if (currentRedArrayPosition == winningCount) { 380 381 redPlayerWins = HIGH; 382 bluePlayerWins = LOW; 383 greenPlayerWins = LOW; 384 celebrateWinner(); 385 386 } // Close if. 387 388 else if (currentBlueArrayPosition == winningCount) { 389 390 redPlayerWins = LOW; 391 bluePlayerWins = HIGH; 392 greenPlayerWins = LOW; 393 celebrateWinner(); 394 395 } // Close if. 396 397 else if (currentGreenArrayPosition == winningCount) { 398 399 redPlayerWins = LOW; 400 bluePlayerWins = LOW; 401 greenPlayerWins = HIGH; 402 celebrateWinner(); 403 404 } // Close if. 405 406 while (brightnessSet == HIGH) { 407 408 matrixBrightness(); // Set brightness 409 410 } // Close if. 411 412} // Close loop. 413 414/*-----------------------------------------------------------------*/ 415 416void ISR_function() { 417 418 static unsigned long last_interrupt_time = 0; 419 unsigned long interrupt_time = millis(); 420 421 if (interrupt_time - last_interrupt_time > 200) 422 { 423 424 if (brightnessSet == LOW) { 425 426 brightnessSet = HIGH; 427 } 428 429 else if (brightnessSet == HIGH) { 430 431 brightnessSet = LOW; 432 } 433 434 } 435 436 last_interrupt_time = interrupt_time; 437 438} // Close function. 439 440/*-----------------------------------------------------------------*/ 441 442void startUpDigiCribSequence(uint32_t color, int wait) { 443 444 matrix.clear(); 445 matrix.show(); 446 447 delay(startUpDelay); 448 449 // Matrix sweap left across. 450 451 for (int s = 0; s < 256; s++) { 452 453 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 454 s++; 455 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 456 s++; 457 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 458 s++; 459 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 460 s++; 461 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 462 s++; 463 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 464 s++; 465 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 466 s++; 467 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 468 469 strip.show(); // Update strip to match 470 delay(wait); // Pause for a moment 471 472 } // Close for. 473 474 // Matrix sweap right across. 475 476 for (int s = 255; s > 0; s--) { 477 478 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 479 s--; 480 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 481 s--; 482 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 483 s--; 484 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 485 s--; 486 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 487 s--; 488 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 489 s--; 490 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 491 s--; 492 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), blankM); // Set pixel's color (in RAM) 493 494 strip.show(); // Update strip to match 495 delay(wait); // Pause for a moment 496 497 } // Close for. 498 499 // Matrix sweap left across once more. 500 501 for (int s = 0; s < 256; s++) { 502 503 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 504 s++; 505 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 506 s++; 507 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 508 s++; 509 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 510 s++; 511 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 512 s++; 513 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 514 s++; 515 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 516 s++; 517 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), color); // Set pixel's color (in RAM) 518 519 strip.show(); // Update strip to match 520 delay(wait); // Pause for a moment 521 522 } // Close for. 523 524 // Matrix increase brightness. 525 526 for (int i = 15; i < 95; i++) { 527 528 matrix.setBrightness(i); 529 matrix.fill(white, 0, pixelCount); 530 matrix.show(); 531 delay(20); 532 } 533 534 // Clear matrix. 535 536 matrix.clear(); 537 matrix.show(); 538 delay(startUpDelay / 2); 539 540 // Digi Crib splash name. 541 542 matrix.fillScreen(0); 543 matrix.setCursor(5, 0); 544 matrix.setBrightness(60); 545 matrix.setTextColor(whiteM); 546 matrix.print(F("Digi")); 547 matrix.show(); 548 delay(startUpDelay); 549 550 matrix.clear(); 551 matrix.show(); 552 553 matrix.fillScreen(0); 554 matrix.setCursor(5, 0); 555 matrix.setBrightness(60); 556 matrix.setTextColor(whiteM); 557 matrix.print(F("Crib")); 558 matrix.show(); 559 delay(startUpDelay * 2); 560 561 strip.clear(); 562 strip.show(); 563 564 // Stop sequence from repeating after game played until power off. 565 566 startUpSequence = false; 567 568} // Close function. 569 570/*-----------------------------------------------------------------*/ 571 572void cribbageBoardLayout2PlayerShort(uint32_t white) { 573 574 for (byte i = 0; i < 138; i++) { // For each pixel in strip... 575 576 strip.setBrightness(matrixBrightnessSet); 577 strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix2PlayerShort[i]))), white); // Set pixel's color (in RAM) 578 strip.show(); // Update strip to match 579 delay(cribbageBoardLayOutSpeed); // Pause for a moment 580 581 } // Close for. 582 583} // Close function. 584 585/*-----------------------------------------------------------------*/ 586 587void cribbageBoardLayout2PlayerLong(uint32_t white) { 588 589 for (byte i = 0; i < 16; i++) { // For each pixel in strip... 590 591 strip.setBrightness(matrixBrightnessSet); 592 strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix2PlayerLong[i]))), white); // Set pixel's color (in RAM) 593 strip.show(); // Update strip to match 594 delay(cribbageBoardLayOutSpeed); // Pause for a moment 595 596 } // Close for. 597 598} // Close function. 599 600/*-----------------------------------------------------------------*/ 601 602void cribbageBoardLayout3PlayerLong(uint32_t white) { 603 604 for (byte i = 0; i < 76; i++) { // For each pixel in strip... 605 606 strip.setBrightness(matrixBrightnessSet); 607 strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix3PlayerLong[i]))), white); // Set pixel's color (in RAM) 608 strip.show(); // Update strip to match 609 delay(cribbageBoardLayOutSpeed); // Pause for a moment 610 611 } // Close for. 612 613} // Close function. 614 615/*-----------------------------------------------------------------*/ 616 617void cribbageMoveRedGameOne(void) { 618 619 byte currentRedLED = (pgm_read_byte(&(matrixRedArray2PlayerShort[currentRedArrayPosition]))); 620 621 // Set pixel colour and start. 622 623 strip.setBrightness(matrixBrightnessSet); 624 strip.setPixelColor(currentRedLED, red); 625 strip.show(); 626 627 // Count red position. 628 629 currentRedArrayPosition++; 630 631} // Close function. 632 633/*-----------------------------------------------------------------*/ 634 635void cribbageMoveBlueGameOne(void) { 636 637 byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray2PlayerShort[currentBlueArrayPosition]))); 638 639 // Set pixel colour and start. 640 641 strip.setBrightness(matrixBrightnessSet); 642 strip.setPixelColor(currentBlueLED, blue); 643 strip.show(); 644 645 // Count blue position. 646 647 currentBlueArrayPosition++; 648 649} // Close function. 650 651/*-----------------------------------------------------------------*/ 652 653void cribbageMoveRedGameTwo(void) { 654 655 byte currentRedLED = (pgm_read_byte(&(matrixRedArray2PlayerLong[currentRedArrayPosition]))); 656 657 // Set pixel colour and start. 658 659 strip.setBrightness(matrixBrightnessSet); 660 strip.setPixelColor(currentRedLED, red); 661 strip.show(); 662 663 // Count red position. 664 665 currentRedArrayPosition++; 666 667} // Close function. 668 669/*-----------------------------------------------------------------*/ 670 671void cribbageMoveBlueGameTwo(void) { 672 673 byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray2PlayerLong[currentBlueArrayPosition]))); 674 675 // Set pixel colour and start. 676 677 strip.setBrightness(matrixBrightnessSet); 678 strip.setPixelColor(currentBlueLED, blue); 679 strip.show(); 680 681 // Count blue position. 682 683 currentBlueArrayPosition++; 684 685} // Close function. 686 687/*-----------------------------------------------------------------*/ 688 689void cribbageMoveRedGameThree(void) { 690 691 if (currentRedArrayPosition <= 59) { 692 693 byte currentRedLED = (pgm_read_byte(&(matrixRedArray3PlayerLong[currentRedArrayPosition]))); 694 695 // Set pixel colour and start. 696 697 strip.setBrightness(matrixBrightnessSet); 698 strip.setPixelColor(currentRedLED, red); 699 strip.show(); 700 701 // Count red position. 702 703 currentRedArrayPosition++; 704 705 } // Close if. 706 707 else if (redResetFlag == 0) { 708 709 threePlayerRedReset(); 710 redResetFlag = 1; 711 currentRedArrayPosition = 0; 712 713 } // Close else if. 714 715 else if (currentRedArrayPosition == 60 && redResetFlag == 1) { 716 717 redPlayerWins = HIGH; 718 bluePlayerWins = LOW; 719 greenPlayerWins = LOW; 720 celebrateWinner(); 721 redResetFlag = 0; 722 723 } // Close else if. 724 725} // Close function. 726 727/*-----------------------------------------------------------------*/ 728 729void threePlayerRedReset(void) { 730 731 for (byte i = 0; i < 60; i++) { // For each pixel in strip... 732 733 strip.setBrightness(matrixBrightnessSet); 734 strip.setPixelColor((pgm_read_byte(&(matrixRedArray3PlayerLong[i]))), white); // Set pixel's color (in RAM) 735 strip.show(); // Update strip to match 736 delay(cribbageBoardLayOutSpeed); // Pause for a moment 737 738 } // Close for. 739 740} // Close function. 741 742 743/*-----------------------------------------------------------------*/ 744 745void cribbageMoveBlueGameThree(void) { 746 747 if (currentBlueArrayPosition <= 59) { 748 749 byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray3PlayerLong[currentBlueArrayPosition]))); 750 751 // Set pixel colour and start. 752 753 strip.setBrightness(matrixBrightnessSet); 754 strip.setPixelColor(currentBlueLED, blue); 755 strip.show(); 756 757 // Count blue position. 758 759 currentBlueArrayPosition++; 760 761 } // Close if. 762 763 else if (blueResetFlag == 0) { 764 765 threePlayerBlueReset(); 766 blueResetFlag = 1; 767 currentBlueArrayPosition = 0; 768 769 } // Close else if. 770 771 else if (currentBlueArrayPosition == 60 && blueResetFlag == 1) { 772 773 redPlayerWins = LOW; 774 bluePlayerWins = HIGH; 775 greenPlayerWins = LOW; 776 celebrateWinner(); 777 blueResetFlag = 0; 778 779 } // Close else if. 780 781} // Close function. 782 783/*-----------------------------------------------------------------*/ 784 785void threePlayerBlueReset(void) { 786 787 for (byte i = 0; i < 60; i++) { // For each pixel in strip... 788 789 strip.setBrightness(matrixBrightnessSet); 790 strip.setPixelColor((pgm_read_byte(&(matrixBlueArray3PlayerLong[i]))), white); // Set pixel's color (in RAM) 791 strip.show(); // Update strip to match 792 delay(cribbageBoardLayOutSpeed); // Pause for a moment 793 794 } // Close for. 795 796} // Close function. 797 798/*-----------------------------------------------------------------*/ 799 800void cribbageMoveGreenGameThree(void) { 801 802 if (currentGreenArrayPosition <= 59) { 803 804 byte currentGreenLED = (pgm_read_byte(&(matrixGreenArray3PlayerLong[currentGreenArrayPosition]))); 805 806 // Set pixel colour and start. 807 808 strip.setBrightness(matrixBrightnessSet); 809 strip.setPixelColor(currentGreenLED, green); 810 strip.show(); 811 812 // Count green position. 813 814 currentGreenArrayPosition++; 815 816 } // Close if. 817 818 else if (greenResetFlag == 0) { 819 820 threePlayerGreenReset(); 821 greenResetFlag = 1; 822 currentGreenArrayPosition = 0; 823 824 } // Close else if. 825 826 else if (currentGreenArrayPosition == 60 && greenResetFlag == 1) { 827 828 redPlayerWins = LOW; 829 bluePlayerWins = LOW; 830 greenPlayerWins = HIGH; 831 celebrateWinner(); 832 greenResetFlag = 0; 833 834 } // Close else if. 835 836} // Close function. 837 838/*-----------------------------------------------------------------*/ 839 840void threePlayerGreenReset(void) { 841 842 for (byte i = 0; i < 60; i++) { // For each pixel in strip... 843 844 strip.setBrightness(matrixBrightnessSet); 845 strip.setPixelColor((pgm_read_byte(&(matrixGreenArray3PlayerLong[i]))), white); // Set pixel's color (in RAM) 846 strip.show(); // Update strip to match 847 delay(cribbageBoardLayOutSpeed); // Pause for a moment 848 849 } // Close for. 850 851} // Close function. 852 853/*-----------------------------------------------------------------*/ 854 855void celebrateWinner(void) { 856 857 // Start matrix for winning player text. 858 859 matrix.begin(); 860 matrix.setTextWrap(false); 861 862 // Check if red player has won? 863 864 if (redPlayerWins == HIGH) { 865 866 // Configure and display text. 867 868 int x = matrix.width(); 869 870 while (x > -145) { // Only display text for one pass 871 872 matrix.fillScreen(0); 873 matrix.setBrightness(matrixBrightnessSet); 874 matrix.setCursor(x, 0); 875 matrix.setTextColor(redM); 876 matrix.print(F("Red Player Wins!!!")); 877 if (--x < -145) { 878 x = matrix.width(); 879 } 880 881 matrix.show(); 882 delay(scrollingTextSpeed); 883 884 } // Close while. 885 886 // Reset width and flags. 887 888 x = matrix.width(); 889 redPlayerWins = LOW; 890 891 } // Close if. 892 893 // Check if blue player has won? 894 895 else if (bluePlayerWins == HIGH) { 896 897 // Configure and display text. 898 899 int x = matrix.width(); 900 901 while (x > -145) { // Only display text for one pass 902 903 matrix.fillScreen(0); 904 matrix.setBrightness(matrixBrightnessSet); 905 matrix.setCursor(x, 0); 906 matrix.setTextColor(blueM); 907 matrix.print(F("Blue Player Wins!!!")); 908 if (--x < -145) { 909 x = matrix.width(); 910 } 911 912 matrix.show(); 913 delay(scrollingTextSpeed); 914 915 } // Close while. 916 917 // Reset width and flags. 918 919 x = matrix.width(); 920 bluePlayerWins = LOW; 921 922 } // Close if. 923 924 // Check if green player has won? 925 926 else if (greenPlayerWins == HIGH) { 927 928 // Configure and display text. 929 930 int x = matrix.width(); 931 932 while (x > -145) { // Only display text for one pass 933 934 matrix.fillScreen(0); 935 matrix.setBrightness(matrixBrightnessSet); 936 matrix.setCursor(x, 0); 937 matrix.setTextColor(greenM); 938 matrix.print(F("Green Player Wins!!!")); 939 if (--x < -145) { 940 x = matrix.width(); 941 } 942 943 matrix.show(); 944 delay(scrollingTextSpeed); 945 946 } // Close while. 947 948 // Reset width and flags. 949 950 x = matrix.width(); 951 greenPlayerWins = LOW; 952 953 } // Close if. 954 955 // Reset player array positions. 956 957 currentRedArrayPosition = 0; 958 currentBlueArrayPosition = 0; 959 currentGreenArrayPosition = 0; 960 961 redResetFlag = 0; 962 blueResetFlag = 0; 963 greenResetFlag = 0; 964 965 redPlayerWins = LOW; 966 bluePlayerWins = LOW; 967 greenPlayerWins = LOW; 968 969 // Clear down display. 970 971 strip.clear(); 972 strip.show(); 973 974 // Restart game. 975 976 setup(); 977 978} // Close function. 979 980/*-----------------------------------------------------------------*/ 981 982int matrixBrightness(void) { 983 984 while (brightnessSet == HIGH) { // Brightness menu flag set by interupt request 985 986 // Setting the display brightness 987 988 if (digitalRead(buttonBluePin) == HIGH) 989 { 990 if (matrixBrightnessSet == 60) 991 { 992 matrixBrightnessSet = 5; 993 } 994 else 995 { 996 matrixBrightnessSet = matrixBrightnessSet + 5; 997 } 998 } 999 if (digitalRead(buttonGreenPin) == HIGH) 1000 { 1001 if (matrixBrightnessSet == 5) 1002 { 1003 matrixBrightnessSet = 60; 1004 } 1005 else 1006 { 1007 matrixBrightnessSet = matrixBrightnessSet - 5; 1008 } 1009 } 1010 1011 matrix.clear(); 1012 matrix.setCursor(1, 0); 1013 matrix.setTextColor(whiteM); 1014 matrix.setBrightness(matrixBrightnessSet); 1015 matrix.print(F("Br:")); 1016 matrix.show(); 1017 1018 matrix.print(matrixBrightnessSet); 1019 matrix.show(); 1020 1021 delay(buttonDelay); 1022 1023 } // Close while. 1024 1025 saveBrightness(); 1026 return; 1027 1028} // Close function. 1029 1030/*-----------------------------------------------------------------*/ 1031 1032void startUpMessage(void) { 1033 1034 // Configure matrix. 1035 1036 matrix.setBrightness(matrixBrightnessSet); 1037 matrix.setTextWrap(false); 1038 1039 // Check game type. 1040 1041 if (game == twoPlayerShort) { 1042 1043 // Configure and display text. 1044 1045 int x = matrix.width(); 1046 1047 while (x > -145) { // Only display text for one pass 1048 1049 matrix.fillScreen(0); 1050 matrix.setCursor(x, 0); 1051 matrix.setTextColor(whiteM); 1052 matrix.print(F("Game: 2 Player Short")); 1053 if (--x < -145) { 1054 x = matrix.width(); 1055 } 1056 matrix.show(); 1057 delay(scrollingTextSpeed); 1058 1059 } // Close while. 1060 1061 // Reset width and flags. 1062 1063 x = matrix.width(); // This resets the test to the beginning again 1064 1065 } // Close if. 1066 1067 // Check game type. 1068 1069 else if (game == twoPlayerLong) { 1070 1071 // Configure and display text. 1072 1073 int x = matrix.width(); 1074 1075 while (x > -145) { // Only display text for one pass 1076 1077 matrix.fillScreen(0); 1078 matrix.setCursor(x, 0); 1079 matrix.setTextColor(whiteM); 1080 matrix.print(F("Game: 2 Player Long")); 1081 if (--x < -145) { 1082 x = matrix.width(); 1083 } 1084 1085 matrix.show(); 1086 delay(scrollingTextSpeed); 1087 1088 } // Close while. 1089 1090 // Reset width and flags. 1091 1092 x = matrix.width(); // This resets the test to the beginning again 1093 1094 } // Close if. 1095 1096 // Check game type. 1097 1098 else if (game == threePlayerLong) { 1099 1100 // Configure and display text. 1101 1102 int x = matrix.width(); 1103 1104 while (x > -145) { // Only display text for one pass 1105 1106 matrix.fillScreen(0); 1107 matrix.setCursor(x, 0); 1108 matrix.setTextColor(whiteM); 1109 matrix.print(F("Game: 3 Player Long")); 1110 if (--x < -145) { 1111 x = matrix.width(); 1112 } 1113 1114 matrix.show(); 1115 delay(scrollingTextSpeed); 1116 1117 } // Close while. 1118 1119 // Reset width and flags. 1120 1121 x = matrix.width(); // This resets the test to the beginning again 1122 1123 } // Close if. 1124 1125} // Close function. 1126 1127/*-----------------------------------------------------------------*/ 1128 1129void batteryCharging(uint32_t color, int wait) { 1130 1131 while (button2PlayerShortState == LOW && button2PlayerLongState == LOW && button3PlayerLongState == LOW) { 1132 1133 // Read game buttons. 1134 1135 button2PlayerShortState = digitalRead(button2PlayerShort); 1136 button2PlayerLongState = digitalRead(button2PlayerLong); 1137 button3PlayerLongState = digitalRead(button3PlayerLong); 1138 1139 // Blink two leds red to indicate charging mode enabled. 1140 1141 strip.setBrightness(5); 1142 strip.setPixelColor(0, color); // Set pixel's color (in RAM) 1143 strip.show(); // Update strip to matc 1144 delay(wait); // Pause for a moment 1145 1146 strip.clear(); 1147 strip.show(); 1148 1149 strip.setPixelColor(1, color); // Set pixel's color (in RAM) 1150 strip.show(); // Update strip to matc 1151 delay(wait); // Pause for a moment 1152 1153 strip.clear(); 1154 strip.show(); 1155 1156 // Enable charging relay pin to switch on, onboard NiCd charging circuit. 1157 1158 digitalWrite(chargingRelayPin, HIGH); 1159 1160 } // Close while. 1161 1162 // Disable charging relay pin to switch off, onboard NiCd charging circuit. 1163 1164 digitalWrite(chargingRelayPin, LOW); 1165 1166 // Restart game. 1167 1168 setup(); 1169 1170} // Close function. 1171 1172/*-----------------------------------------------------------------*/ 1173 1174void saveBrightness() { 1175 1176 // Display simple set message. 1177 1178 matrix.clear(); 1179 matrix.setCursor(7, 0); 1180 matrix.print(F("Set")); 1181 matrix.show(); 1182 delay(1000); 1183 1184 // Re-running game layout options to force update the display from brightness screen to game screen. 1185 1186 if (button2PlayerShortState == HIGH) { 1187 1188 cribbageBoardLayout2PlayerShort(white); 1189 1190 } // Close if. 1191 1192 else if (button2PlayerLongState == HIGH) { 1193 1194 cribbageBoardLayout2PlayerLong(white); 1195 1196 } // Close if. 1197 1198 else if (button3PlayerLongState == HIGH) { 1199 1200 cribbageBoardLayout3PlayerLong(white); 1201 1202 } // Close if. 1203 1204} 1205 1206/*-----------------------------------------------------------------*/ 1207
DigiCrib - Digital Cribbage Board
arduino
It is a digital scoring board for those who play Cribbage
1/* 2 Name: Cribbage_Board.ino 3 Created: 7/13/2020 9:41:14 PM 4 5 Author: Christopher Cooper 6*/ 7 8// Project Cribbage Board Game - Using 9 four Arduino Adafruit NeoPixel Matrix's 8 x 8 x 4 (RGBW Configuration) 10 11// 12 Adafruit NeoPixel libraries. 13 14#include <Adafruit_GFX.h> 15#include <Adafruit_NeoMatrix.h> 16#include 17 <Adafruit_NeoPixel.h> 18 19// Configure NeoPixel. 20 21const int pixelCount 22 = 256; // Number of NeoPixels 23const byte pixelPin = 6; // Digital IO pin 24 connected to the NeoPixels 25const byte matrixHeight = 8; 26const byte matrixWidth 27 = 8; 28const byte tilesX = 4; 29const byte tilesY = 1; 30 31// Declare NeoPixel 32 matrix and strip objects. 33 34Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(matrixWidth, 35 matrixHeight, tilesX, tilesY, pixelPin, 36 37 NEO_TILE_TOP + NEO_TILE_LEFT 38 + 39 NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE, 40 NEO_RGBW + NEO_KHZ800); 41 42// 43 Argument 1 = Number of rows in NeoPixel array. 44// Argument 2 = Number of columns 45 in NeoPixel array. 46// Argument 3 = Number of horizontal tiles. 47// Argument 48 4 = Number of vertical tiles. 49// Argument 5 = Arduino pin number. 50// From 51 adafruit help for tiled arrays matrixWidth, matrixHeight, tilesX, tilesY, pin, matrixType, 52 ledType); 53 54Adafruit_NeoPixel strip(pixelCount, pixelPin, NEO_RGBW + NEO_KHZ800); 55 56// 57 Argument 1 = Number of pixels in NeoPixel strip 58// Argument 2 = Arduino pin number 59 (most are valid) 60// Argument 3 = Pixel type flags, add together as needed: 61 62// 63 NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) 64// NEO_KHZ400 65 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) 66// NEO_GRB Pixels 67 are wired for GRB bitstream (most NeoPixel products) 68// NEO_RGB Pixels are 69 wired for RGB bitstream (v1 FLORA pixels, not v2) 70// NEO_RGBW Pixels are wired 71 for RGBW bitstream (NeoPixel RGBW products) 72 73// Configure buttons. 74 75const 76 byte button2PlayerShort = 15; // A1 77const byte button2PlayerLong = 16; // A2 78const 79 byte button3PlayerLong = 17; // A3 80 81boolean button2PlayerShortState = 82 LOW; 83boolean button2PlayerLongState = LOW; 84boolean button3PlayerLongState 85 = LOW; 86 87const byte buttonWhitePin = A0; 88const byte buttonRedPin = 10; 89const 90 byte buttonBluePin = 8; 91const byte buttonGreenPin = 9; 92 93const byte buttonBounce 94 = 20; 95const byte buttonDelay = 200; 96 97boolean oldRedState = HIGH; 98boolean 99 newRedState = LOW; 100boolean oldBlueState = HIGH; 101boolean newBlueState 102 = LOW; 103boolean oldGreenState = HIGH; 104boolean newGreenState = LOW; 105 106// 107 Configure initial brightness level. 108 109byte matrixBrightnessSet = 15; 110volatile 111 boolean brightnessSet = LOW; 112 113// Configure charging relay. 114 115const 116 byte chargingRelayPin = A7; 117 118// Configure colours for both the strip and 119 matrix libraris, as one includes the white LED (RGBW vs GRB). 120 121const uint32_t 122 green = strip.Color(255, 0, 0, 0); 123const uint32_t red = strip.Color(0, 255, 0, 124 0); 125const uint32_t blue = strip.Color(0, 0, 255, 0); 126const uint32_t white 127 = strip.Color(0, 0, 0, 255); 128 129const uint32_t greenM = matrix.Color(255, 0, 130 0); 131const uint32_t redM = matrix.Color(0, 255, 0); 132const uint32_t blueM = 133 matrix.Color(0, 0, 255); 134const uint32_t whiteM = matrix.Color(255, 255, 255); 135const 136 uint32_t blankM = matrix.Color(0, 0, 0); 137 138// Configure variables. 139 140const 141 int startUpDelay = 1500; 142const int scrollingTextSpeed = 35; 143const int 144 cribbageBoardLayOutSpeed = 20; 145boolean startUpSequence = true; 146 147// 148 Configure game types. 149 150const byte twoPlayerShort = 1; 151const byte twoPlayerLong 152 = 2; 153const byte threePlayerLong = 3; 154 155byte game; 156 157// Winning 158 point count. 159 160const byte shortGame = 61; 161const byte longGame = 121; 162 163byte 164 winningCount; 165 166// Winning player flags. 167 168boolean redPlayerWins 169 = LOW; 170boolean bluePlayerWins = LOW; 171boolean greenPlayerWins = LOW; 172 173// 174 Digicrib start up sequence array. 175 176const byte digiCribStartUpSequenceArray[256] 177 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, 178 179 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, 180 181 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, 182 183 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 184 }; 185 186// Configure matrix array player layouts - 2 player short game array. 187 188const 189 byte cribbageBoardLayoutMatrix2PlayerShort[138] PROGMEM = { 0, 1, 2, 3, 4, 5, 6, 190 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135, 192, 191 193, 194, 195, 196, 197, 198, 199, 207, 215, 223, 222, 221, 220, 219, 218, 217, 192 216, 159, 158, 157, 156, 155, 154, 153, 152, 95, 94, 93, 92, 91, 90, 89, 88, 31, 193 30, 29, 28, 27, 26, 25, 24, 32, 33, 34, 35, 36, 37, 38, 39, 96, 97, 98, 99, 100, 194 101, 102, 103, 160, 161, 162, 163, 164, 165, 166, 167, 224, 225, 226, 227, 228, 195 229, 230, 231, 239, 247, 255, 254, 253, 252, 251, 250, 249, 248, 191, 190, 189, 196 188, 187, 186, 185, 184, 127, 126, 125, 124, 123, 122, 121, 120, 63, 62, 61, 60, 197 59, 58, 57, 56, 48, 40, 32, 24, 16, 8 }; 198 199const byte matrixRedArray2PlayerShort[60] 200 PROGMEM = { 9, 10, 11, 12, 13, 14, 15, 72, 73, 74, 75, 76, 77, 78, 79, 136, 137, 201 138, 139, 140, 141, 142, 143, 200, 201, 202, 203, 204, 205, 206, 214, 213, 212, 202 211, 210, 209, 208, 151, 150, 149, 148, 147, 146, 145, 144, 87, 86, 85, 84, 83, 203 82, 81, 80, 23, 22, 21, 20, 19, 18, 17 }; 204const byte matrixBlueArray2PlayerShort[60] 205 PROGMEM = { 246, 245, 244, 243, 242, 241, 240, 183, 182, 181, 180, 179, 178, 177, 206 176, 119, 118, 117, 116, 115, 114, 113, 112, 55, 54, 53, 52, 51, 50, 49, 41, 42, 207 43, 44, 45, 46, 47, 104, 105, 106, 107, 108, 109, 110, 111, 168, 169, 170, 171, 208 172, 173, 174, 175, 232, 233, 234, 235, 236, 237, 238 }; 209 210// Configure matrix 211 array player layouts - 2 player long game array. 212 213const byte cribbageBoardLayoutMatrix2PlayerLong[16] 214 PROGMEM = { 0, 8, 16, 24, 32, 40, 48, 56, 255, 247, 239, 231, 223, 215, 207, 199 215 }; 216 217const byte matrixRedArray2PlayerLong[120] PROGMEM = { 1, 2, 3, 4, 5, 218 6, 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135, 192, 219 193, 194, 195, 196, 197, 198, 206, 205, 204, 203, 202, 201, 200, 143, 142, 141, 220 140, 139, 138, 137, 136, 79, 78, 77, 76, 75, 74, 73, 72, 15, 14, 13, 12, 11, 10, 221 9, 17, 18, 19, 20, 21, 22, 23, 80, 81, 82, 83, 84, 85, 86, 87, 144, 145, 146, 147, 222 148, 149, 150, 151, 208, 209, 210, 211, 212, 213, 214, 222, 221, 220, 219, 218, 223 217, 216, 159, 158, 157, 156, 155, 154, 153, 152, 95, 94, 93, 92, 91, 90, 89, 88, 224 31, 30, 29, 28, 27, 26, 25 }; 225const byte matrixBlueArray2PlayerLong[120] PROGMEM 226 = { 254, 253, 252, 251, 250, 249, 248, 191, 190, 189, 188, 187, 186, 185, 184, 127, 227 126, 125, 124, 123, 122, 121, 120, 63, 62, 61, 60, 59, 58, 57, 49, 50, 51, 52, 53, 228 54, 55, 112, 113, 114, 115, 116, 117, 118, 119, 176, 177, 178, 179, 180, 181, 182, 229 183, 240, 241, 242, 243, 244, 245, 246, 238, 237, 236, 235, 234, 233, 232, 175, 230 174, 173, 172, 171, 170, 169, 168, 111, 110, 109, 108, 107, 106, 105, 104, 47, 46, 231 45, 44, 43, 42, 41, 33, 34, 35, 36, 37, 38, 39, 96, 97, 98, 99, 100, 101, 102, 103, 232 160, 161, 162, 163, 164, 165, 166, 167, 224, 225, 226, 227, 228, 229, 230 }; 233 234// 235 Configure matrix array player layouts - 3 player long game array. 236 237const byte 238 cribbageBoardLayoutMatrix3PlayerLong[76] PROGMEM = { 0, 8, 16, 24, 32, 40, 48, 56, 239 255, 247, 239, 231, 223, 215, 207, 199, 214, 213, 212, 211, 210, 209, 208, 151, 240 150, 149, 148, 147, 146, 145, 144, 87, 86, 85, 84, 83, 82, 81, 80, 23, 22, 21, 20, 241 19, 18, 17, 41, 42, 43, 44, 45, 46, 47, 104, 105, 106, 107, 108, 109, 110, 111, 242 168, 169, 170, 171, 172, 173, 174, 175, 232, 233, 234, 235, 236, 237, 238 }; 243 244const 245 byte matrixRedArray3PlayerLong[60] PROGMEM = { 1, 2, 3, 4, 5, 6, 7, 64, 65, 66, 246 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135, 192, 193, 194, 195, 247 196, 197, 198, 206, 205, 204, 203, 202, 201, 200, 143, 142, 141, 140, 139, 138, 248 137, 136, 79, 78, 77, 76, 75, 74, 73, 72, 15, 14, 13, 12, 11, 10, 9 }; 249const 250 byte matrixGreenArray3PlayerLong[60] PROGMEM = { 25, 26, 27, 28, 29, 30, 31, 88, 251 89, 90, 91, 92, 93, 94, 95, 152, 153, 154, 155, 156, 157, 158, 159, 216, 217, 218, 252 219, 220, 221, 222, 230, 229, 228, 227, 226, 225, 224, 167, 166, 165, 164, 163, 253 162, 161, 160, 103, 102, 101, 100, 99, 98, 97, 96, 39, 38, 37, 36, 35, 34, 33 }; 254const 255 byte matrixBlueArray3PlayerLong[60] PROGMEM = { 49, 50, 51, 52, 53, 54, 55, 112, 256 113, 114, 115, 116, 117, 118, 119, 176, 177, 178, 179, 180, 181, 182, 183, 240, 257 241, 242, 243, 244, 245, 246, 254, 253, 252, 251, 250, 249, 248, 191, 190, 189, 258 188, 187, 186, 185, 184, 127, 126, 125, 124, 123, 122, 121, 120, 63, 62, 61, 60, 259 59, 58, 57 }; 260 261// Configure matrix, red, green & blue starting positions. 262 263byte 264 cribbageBoardLayoutMatrixPosition = 0; 265byte currentRedArrayPosition = 0; 266byte 267 currentGreenArrayPosition = 0; 268byte currentBlueArrayPosition = 0; 269 270// Configure 271 3 Player reset options due to pixel layout, once round, this is used to recolour 272 pixels for second round. 273 274boolean redResetFlag = 0; 275boolean blueResetFlag 276 = 0; 277boolean greenResetFlag = 0; 278 279/*-----------------------------------------------------------------*/ 280 281void 282 setup() { 283 284 // NeoPixel start up sequence. 285 286 strip.begin(); // 287 Initialize NeoPixel strip object 288 strip.setBrightness(matrixBrightnessSet); 289 // Manually set the brightness to avoid power problems at start up 290 291 //Function 292 name ((color), delay). 293 294 delay(startUpDelay); 295 296 if (startUpSequence 297 == true) { 298 299 startUpDigiCribSequence((white), 35); 300 301 } // 302 Close if. 303 304 // Set pin modes to inputs. 305 306 pinMode(chargingRelayPin, 307 OUTPUT); // Digital pin to enable relay for charging mode 308 pinMode(button2PlayerShort, 309 INPUT); 310 pinMode(button2PlayerLong, INPUT); 311 pinMode(button3PlayerLong, 312 INPUT); 313 314 pinMode(buttonWhitePin, INPUT_PULLUP); 315 pinMode(buttonRedPin, 316 INPUT); 317 pinMode(buttonBluePin, INPUT); 318 pinMode(buttonGreenPin, INPUT); 319 320 321 // Read game buttons. 322 323 button2PlayerShortState = digitalRead(button2PlayerShort); 324 325 button2PlayerLongState = digitalRead(button2PlayerLong); 326 button3PlayerLongState 327 = digitalRead(button3PlayerLong); 328 329 // Configure interupts. 330 331 attachInterrupt(digitalPinToInterrupt(buttonWhitePin), 332 ISR_function, FALLING); 333 334 // Configure game option. 335 336 if (button2PlayerShortState 337 == LOW && button2PlayerLongState == LOW && button3PlayerLongState == LOW) { 338 339 340 batteryCharging(red, 1000); 341 342 } // Close if. 343 344 else if 345 (button2PlayerShortState == HIGH) { 346 347 game = twoPlayerShort; 348 winningCount 349 = shortGame; 350 startUpMessage(); 351 cribbageBoardLayout2PlayerShort(white); 352 353 354 } // Close if. 355 356 else if (button2PlayerLongState == HIGH) { 357 358 359 game = twoPlayerLong; 360 winningCount = longGame; 361 startUpMessage(); 362 363 cribbageBoardLayout2PlayerLong(white); 364 365 } // Close if. 366 367 368 else if (button3PlayerLongState == HIGH) { 369 370 game = threePlayerLong; 371 372 winningCount = longGame; 373 startUpMessage(); 374 cribbageBoardLayout3PlayerLong(white); 375 376 377 } // Close if. 378 379} // Close setup. 380 381/*-----------------------------------------------------------------*/ 382 383void 384 loop() { 385 386 // Red Player - Get current button state. 387 388 newRedState 389 = digitalRead(buttonRedPin); 390 391 // Check if state changed from high to low 392 (button press). 393 394 if ((newRedState == LOW) && (oldRedState == HIGH)) { 395 396 397 // Short delay to debounce button. 398 399 delay(buttonBounce); 400 401 402 // Check if button is still low after debounce. 403 404 newRedState 405 = digitalRead(buttonRedPin); 406 407 if (newRedState == HIGH) { // Yes, 408 still low 409 410 if (game == twoPlayerShort) { 411 412 cribbageMoveRedGameOne(); 413 414 oldRedState = newRedState; 415 416 } // Close if. 417 418 419 else if (game == twoPlayerLong) { 420 421 cribbageMoveRedGameTwo(); 422 423 oldRedState = newRedState; 424 425 } // Close if. 426 427 428 else if (game == threePlayerLong) { 429 430 cribbageMoveRedGameThree(); 431 432 oldRedState = newRedState; 433 434 } // Close if. 435 436 437 } // Close if. 438 439 } // Close if. 440 441 // Blue Player - Get 442 current button state. 443 444 newBlueState = digitalRead(buttonBluePin); 445 446 447 // Check if state changed from high to low (button press). 448 449 if ((newBlueState 450 == LOW) && (oldBlueState == HIGH)) { 451 452 // Short delay to debounce button. 453 454 455 delay(buttonBounce); 456 457 // Check if button is still low after 458 debounce. 459 460 newBlueState = digitalRead(buttonBluePin); 461 462 if 463 (newBlueState == HIGH) { // Yes, still low 464 465 if (game == twoPlayerShort) 466 { 467 468 cribbageMoveBlueGameOne(); 469 oldBlueState 470 = newBlueState; 471 472 } // Close if. 473 474 else if (game 475 == twoPlayerLong) { 476 477 cribbageMoveBlueGameTwo(); 478 oldBlueState 479 = newBlueState; 480 481 } // Close if. 482 483 else if (game 484 == threePlayerLong) { 485 486 cribbageMoveBlueGameThree(); 487 oldBlueState 488 = newBlueState; 489 490 } // Close if. 491 492 } // Close if. 493 494 495 } // Close if. 496 497 // Green Player - Get current button state. 498 499 500 newGreenState = digitalRead(buttonGreenPin); 501 502 // Check if state changed 503 from high to low (button press). 504 505 if ((newGreenState == LOW) && (oldGreenState 506 == HIGH)) { 507 508 // Short delay to debounce button. 509 510 delay(buttonBounce); 511 512 513 // Check if button is still low after debounce. 514 515 newGreenState 516 = digitalRead(buttonGreenPin); 517 518 if (newGreenState == HIGH) { // 519 Yes, still low 520 521 if (game == twoPlayerShort) { 522 523 oldGreenState 524 = newGreenState; 525 526 } // Close if. 527 528 else if (game 529 == twoPlayerLong) { 530 531 oldGreenState = newGreenState; 532 533 534 } // Close if. 535 536 else if (game == threePlayerLong) 537 { 538 539 cribbageMoveGreenGameThree(); 540 oldGreenState 541 = newGreenState; 542 543 } // Close if. 544 545 } // Close if. 546 547 548 } // Close if. 549 550 // Check for a winner. 551 552 if (currentRedArrayPosition 553 == winningCount) { 554 555 redPlayerWins = HIGH; 556 bluePlayerWins 557 = LOW; 558 greenPlayerWins = LOW; 559 celebrateWinner(); 560 561 } 562 // Close if. 563 564 else if (currentBlueArrayPosition == winningCount) { 565 566 567 redPlayerWins = LOW; 568 bluePlayerWins = HIGH; 569 greenPlayerWins 570 = LOW; 571 celebrateWinner(); 572 573 } // Close if. 574 575 else if 576 (currentGreenArrayPosition == winningCount) { 577 578 redPlayerWins = LOW; 579 580 bluePlayerWins = LOW; 581 greenPlayerWins = HIGH; 582 celebrateWinner(); 583 584 585 } // Close if. 586 587 while (brightnessSet == HIGH) { 588 589 matrixBrightness(); 590 // Set brightness 591 592 } // Close if. 593 594} // 595 Close loop. 596 597/*-----------------------------------------------------------------*/ 598 599void 600 ISR_function() { 601 602 static unsigned long last_interrupt_time = 0; 603 unsigned 604 long interrupt_time = millis(); 605 606 if (interrupt_time - last_interrupt_time 607 > 200) 608 { 609 610 if (brightnessSet == LOW) { 611 612 brightnessSet 613 = HIGH; 614 } 615 616 else if (brightnessSet == HIGH) { 617 618 brightnessSet 619 = LOW; 620 } 621 622 } 623 624 last_interrupt_time = interrupt_time; 625 626} 627 // Close function. 628 629/*-----------------------------------------------------------------*/ 630 631void 632 startUpDigiCribSequence(uint32_t color, int wait) { 633 634 matrix.clear(); 635 636 matrix.show(); 637 638 delay(startUpDelay); 639 640 // Matrix sweap left 641 across. 642 643 for (int s = 0; s < 256; s++) { 644 645 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 646 color); // Set pixel's color (in RAM) 647 s++; 648 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 649 color); // Set pixel's color (in RAM) 650 s++; 651 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 652 color); // Set pixel's color (in RAM) 653 s++; 654 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 655 color); // Set pixel's color (in RAM) 656 s++; 657 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 658 color); // Set pixel's color (in RAM) 659 s++; 660 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 661 color); // Set pixel's color (in RAM) 662 s++; 663 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 664 color); // Set pixel's color (in RAM) 665 s++; 666 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 667 color); // Set pixel's color (in RAM) 668 669 strip.show(); // 670 Update strip to match 671 delay(wait); // Pause for a moment 672 673 674 } // Close for. 675 676 // Matrix sweap right across. 677 678 for (int 679 s = 255; s > 0; s--) { 680 681 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 682 blankM); // Set pixel's color (in RAM) 683 s--; 684 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 685 blankM); // Set pixel's color (in RAM) 686 s--; 687 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 688 blankM); // Set pixel's color (in RAM) 689 s--; 690 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 691 blankM); // Set pixel's color (in RAM) 692 s--; 693 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 694 blankM); // Set pixel's color (in RAM) 695 s--; 696 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 697 blankM); // Set pixel's color (in RAM) 698 s--; 699 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 700 blankM); // Set pixel's color (in RAM) 701 s--; 702 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 703 blankM); // Set pixel's color (in RAM) 704 705 strip.show(); // 706 Update strip to match 707 delay(wait); // Pause for a moment 708 709 710 } // Close for. 711 712 // Matrix sweap left across once more. 713 714 for 715 (int s = 0; s < 256; s++) { 716 717 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 718 color); // Set pixel's color (in RAM) 719 s++; 720 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 721 color); // Set pixel's color (in RAM) 722 s++; 723 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 724 color); // Set pixel's color (in RAM) 725 s++; 726 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 727 color); // Set pixel's color (in RAM) 728 s++; 729 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 730 color); // Set pixel's color (in RAM) 731 s++; 732 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 733 color); // Set pixel's color (in RAM) 734 s++; 735 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 736 color); // Set pixel's color (in RAM) 737 s++; 738 strip.setPixelColor((pgm_read_byte(&(digiCribStartUpSequenceArray[s]))), 739 color); // Set pixel's color (in RAM) 740 741 strip.show(); // 742 Update strip to match 743 delay(wait); // Pause for a moment 744 745 746 } // Close for. 747 748 // Matrix increase brightness. 749 750 for (int 751 i = 15; i < 95; i++) { 752 753 matrix.setBrightness(i); 754 matrix.fill(white, 755 0, pixelCount); 756 matrix.show(); 757 delay(20); 758 } 759 760 761 // Clear matrix. 762 763 matrix.clear(); 764 matrix.show(); 765 delay(startUpDelay 766 / 2); 767 768 // Digi Crib splash name. 769 770 matrix.fillScreen(0); 771 772 matrix.setCursor(5, 0); 773 matrix.setBrightness(60); 774 matrix.setTextColor(whiteM); 775 776 matrix.print(F("Digi")); 777 matrix.show(); 778 delay(startUpDelay); 779 780 781 matrix.clear(); 782 matrix.show(); 783 784 matrix.fillScreen(0); 785 matrix.setCursor(5, 786 0); 787 matrix.setBrightness(60); 788 matrix.setTextColor(whiteM); 789 matrix.print(F("Crib")); 790 791 matrix.show(); 792 delay(startUpDelay * 2); 793 794 strip.clear(); 795 796 strip.show(); 797 798 // Stop sequence from repeating after game played until 799 power off. 800 801 startUpSequence = false; 802 803} // Close function. 804 805/*-----------------------------------------------------------------*/ 806 807void 808 cribbageBoardLayout2PlayerShort(uint32_t white) { 809 810 for (byte i = 0; i 811 < 138; i++) { // For each pixel in strip... 812 813 strip.setBrightness(matrixBrightnessSet); 814 815 strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix2PlayerShort[i]))), 816 white); // Set pixel's color (in RAM) 817 strip.show(); // 818 Update strip to match 819 delay(cribbageBoardLayOutSpeed); // Pause 820 for a moment 821 822 } // Close for. 823 824} // Close function. 825 826/*-----------------------------------------------------------------*/ 827 828void 829 cribbageBoardLayout2PlayerLong(uint32_t white) { 830 831 for (byte i = 0; i < 832 16; i++) { // For each pixel in strip... 833 834 strip.setBrightness(matrixBrightnessSet); 835 836 strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix2PlayerLong[i]))), 837 white); // Set pixel's color (in RAM) 838 strip.show(); // 839 Update strip to match 840 delay(cribbageBoardLayOutSpeed); // Pause 841 for a moment 842 843 } // Close for. 844 845} // Close function. 846 847/*-----------------------------------------------------------------*/ 848 849void 850 cribbageBoardLayout3PlayerLong(uint32_t white) { 851 852 for (byte i = 0; i < 853 76; i++) { // For each pixel in strip... 854 855 strip.setBrightness(matrixBrightnessSet); 856 857 strip.setPixelColor((pgm_read_byte(&(cribbageBoardLayoutMatrix3PlayerLong[i]))), 858 white); // Set pixel's color (in RAM) 859 strip.show(); // 860 Update strip to match 861 delay(cribbageBoardLayOutSpeed); // Pause 862 for a moment 863 864 } // Close for. 865 866} // Close function. 867 868/*-----------------------------------------------------------------*/ 869 870void 871 cribbageMoveRedGameOne(void) { 872 873 byte currentRedLED = (pgm_read_byte(&(matrixRedArray2PlayerShort[currentRedArrayPosition]))); 874 875 876 // Set pixel colour and start. 877 878 strip.setBrightness(matrixBrightnessSet); 879 880 strip.setPixelColor(currentRedLED, red); 881 strip.show(); 882 883 // 884 Count red position. 885 886 currentRedArrayPosition++; 887 888} // Close function. 889 890/*-----------------------------------------------------------------*/ 891 892void 893 cribbageMoveBlueGameOne(void) { 894 895 byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray2PlayerShort[currentBlueArrayPosition]))); 896 897 898 // Set pixel colour and start. 899 900 strip.setBrightness(matrixBrightnessSet); 901 902 strip.setPixelColor(currentBlueLED, blue); 903 strip.show(); 904 905 // 906 Count blue position. 907 908 currentBlueArrayPosition++; 909 910} // Close function. 911 912/*-----------------------------------------------------------------*/ 913 914void 915 cribbageMoveRedGameTwo(void) { 916 917 byte currentRedLED = (pgm_read_byte(&(matrixRedArray2PlayerLong[currentRedArrayPosition]))); 918 919 920 // Set pixel colour and start. 921 922 strip.setBrightness(matrixBrightnessSet); 923 924 strip.setPixelColor(currentRedLED, red); 925 strip.show(); 926 927 // 928 Count red position. 929 930 currentRedArrayPosition++; 931 932} // Close function. 933 934/*-----------------------------------------------------------------*/ 935 936void 937 cribbageMoveBlueGameTwo(void) { 938 939 byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray2PlayerLong[currentBlueArrayPosition]))); 940 941 942 // Set pixel colour and start. 943 944 strip.setBrightness(matrixBrightnessSet); 945 946 strip.setPixelColor(currentBlueLED, blue); 947 strip.show(); 948 949 // 950 Count blue position. 951 952 currentBlueArrayPosition++; 953 954} // Close function. 955 956/*-----------------------------------------------------------------*/ 957 958void 959 cribbageMoveRedGameThree(void) { 960 961 if (currentRedArrayPosition <= 59) { 962 963 964 byte currentRedLED = (pgm_read_byte(&(matrixRedArray3PlayerLong[currentRedArrayPosition]))); 965 966 967 // Set pixel colour and start. 968 969 strip.setBrightness(matrixBrightnessSet); 970 971 strip.setPixelColor(currentRedLED, red); 972 strip.show(); 973 974 975 // Count red position. 976 977 currentRedArrayPosition++; 978 979 980 } // Close if. 981 982 else if (redResetFlag == 0) { 983 984 threePlayerRedReset(); 985 986 redResetFlag = 1; 987 currentRedArrayPosition = 0; 988 989 } // 990 Close else if. 991 992 else if (currentRedArrayPosition == 60 && redResetFlag 993 == 1) { 994 995 redPlayerWins = HIGH; 996 bluePlayerWins = LOW; 997 998 greenPlayerWins = LOW; 999 celebrateWinner(); 1000 redResetFlag 1001 = 0; 1002 1003 } // Close else if. 1004 1005} // Close function. 1006 1007/*-----------------------------------------------------------------*/ 1008 1009void 1010 threePlayerRedReset(void) { 1011 1012 for (byte i = 0; i < 60; i++) { // 1013 For each pixel in strip... 1014 1015 strip.setBrightness(matrixBrightnessSet); 1016 1017 strip.setPixelColor((pgm_read_byte(&(matrixRedArray3PlayerLong[i]))), white); 1018 // Set pixel's color (in RAM) 1019 strip.show(); // 1020 Update strip to match 1021 delay(cribbageBoardLayOutSpeed); // Pause 1022 for a moment 1023 1024 } // Close for. 1025 1026} // Close function. 1027 1028 1029/*-----------------------------------------------------------------*/ 1030 1031void 1032 cribbageMoveBlueGameThree(void) { 1033 1034 if (currentBlueArrayPosition <= 59) 1035 { 1036 1037 byte currentBlueLED = (pgm_read_byte(&(matrixBlueArray3PlayerLong[currentBlueArrayPosition]))); 1038 1039 1040 // Set pixel colour and start. 1041 1042 strip.setBrightness(matrixBrightnessSet); 1043 1044 strip.setPixelColor(currentBlueLED, blue); 1045 strip.show(); 1046 1047 1048 // Count blue position. 1049 1050 currentBlueArrayPosition++; 1051 1052 1053 } // Close if. 1054 1055 else if (blueResetFlag == 0) { 1056 1057 threePlayerBlueReset(); 1058 1059 blueResetFlag = 1; 1060 currentBlueArrayPosition = 0; 1061 1062 } 1063 // Close else if. 1064 1065 else if (currentBlueArrayPosition == 60 && blueResetFlag 1066 == 1) { 1067 1068 redPlayerWins = LOW; 1069 bluePlayerWins = HIGH; 1070 1071 greenPlayerWins = LOW; 1072 celebrateWinner(); 1073 blueResetFlag 1074 = 0; 1075 1076 } // Close else if. 1077 1078} // Close function. 1079 1080/*-----------------------------------------------------------------*/ 1081 1082void 1083 threePlayerBlueReset(void) { 1084 1085 for (byte i = 0; i < 60; i++) { // 1086 For each pixel in strip... 1087 1088 strip.setBrightness(matrixBrightnessSet); 1089 1090 strip.setPixelColor((pgm_read_byte(&(matrixBlueArray3PlayerLong[i]))), white); 1091 // Set pixel's color (in RAM) 1092 strip.show(); // 1093 Update strip to match 1094 delay(cribbageBoardLayOutSpeed); // Pause 1095 for a moment 1096 1097 } // Close for. 1098 1099} // Close function. 1100 1101/*-----------------------------------------------------------------*/ 1102 1103void 1104 cribbageMoveGreenGameThree(void) { 1105 1106 if (currentGreenArrayPosition <= 59) 1107 { 1108 1109 byte currentGreenLED = (pgm_read_byte(&(matrixGreenArray3PlayerLong[currentGreenArrayPosition]))); 1110 1111 1112 // Set pixel colour and start. 1113 1114 strip.setBrightness(matrixBrightnessSet); 1115 1116 strip.setPixelColor(currentGreenLED, green); 1117 strip.show(); 1118 1119 1120 // Count green position. 1121 1122 currentGreenArrayPosition++; 1123 1124 1125 } // Close if. 1126 1127 else if (greenResetFlag == 0) { 1128 1129 threePlayerGreenReset(); 1130 1131 greenResetFlag = 1; 1132 currentGreenArrayPosition = 0; 1133 1134 } 1135 // Close else if. 1136 1137 else if (currentGreenArrayPosition == 60 && greenResetFlag 1138 == 1) { 1139 1140 redPlayerWins = LOW; 1141 bluePlayerWins = LOW; 1142 1143 greenPlayerWins = HIGH; 1144 celebrateWinner(); 1145 greenResetFlag 1146 = 0; 1147 1148 } // Close else if. 1149 1150} // Close function. 1151 1152/*-----------------------------------------------------------------*/ 1153 1154void 1155 threePlayerGreenReset(void) { 1156 1157 for (byte i = 0; i < 60; i++) { // 1158 For each pixel in strip... 1159 1160 strip.setBrightness(matrixBrightnessSet); 1161 1162 strip.setPixelColor((pgm_read_byte(&(matrixGreenArray3PlayerLong[i]))), 1163 white); // Set pixel's color (in RAM) 1164 strip.show(); // 1165 Update strip to match 1166 delay(cribbageBoardLayOutSpeed); // Pause 1167 for a moment 1168 1169 } // Close for. 1170 1171} // Close function. 1172 1173/*-----------------------------------------------------------------*/ 1174 1175void 1176 celebrateWinner(void) { 1177 1178 // Start matrix for winning player text. 1179 1180 1181 matrix.begin(); 1182 matrix.setTextWrap(false); 1183 1184 // Check if red 1185 player has won? 1186 1187 if (redPlayerWins == HIGH) { 1188 1189 // Configure 1190 and display text. 1191 1192 int x = matrix.width(); 1193 1194 while (x 1195 > -145) { // Only display text for one pass 1196 1197 matrix.fillScreen(0); 1198 1199 matrix.setBrightness(matrixBrightnessSet); 1200 matrix.setCursor(x, 1201 0); 1202 matrix.setTextColor(redM); 1203 matrix.print(F("Red 1204 Player Wins!!!")); 1205 if (--x < -145) { 1206 x = matrix.width(); 1207 1208 } 1209 1210 matrix.show(); 1211 delay(scrollingTextSpeed); 1212 1213 1214 } // Close while. 1215 1216 // Reset width and flags. 1217 1218 x 1219 = matrix.width(); 1220 redPlayerWins = LOW; 1221 1222 } // Close if. 1223 1224 1225 // Check if blue player has won? 1226 1227 else if (bluePlayerWins == HIGH) 1228 { 1229 1230 // Configure and display text. 1231 1232 int x = matrix.width(); 1233 1234 1235 while (x > -145) { // Only display text for one pass 1236 1237 matrix.fillScreen(0); 1238 1239 matrix.setBrightness(matrixBrightnessSet); 1240 matrix.setCursor(x, 1241 0); 1242 matrix.setTextColor(blueM); 1243 matrix.print(F("Blue 1244 Player Wins!!!")); 1245 if (--x < -145) { 1246 x = matrix.width(); 1247 1248 } 1249 1250 matrix.show(); 1251 delay(scrollingTextSpeed); 1252 1253 1254 } // Close while. 1255 1256 // Reset width and flags. 1257 1258 x 1259 = matrix.width(); 1260 bluePlayerWins = LOW; 1261 1262 } // Close if. 1263 1264 1265 // Check if green player has won? 1266 1267 else if (greenPlayerWins == HIGH) 1268 { 1269 1270 // Configure and display text. 1271 1272 int x = matrix.width(); 1273 1274 1275 while (x > -145) { // Only display text for one pass 1276 1277 matrix.fillScreen(0); 1278 1279 matrix.setBrightness(matrixBrightnessSet); 1280 matrix.setCursor(x, 1281 0); 1282 matrix.setTextColor(greenM); 1283 matrix.print(F("Green 1284 Player Wins!!!")); 1285 if (--x < -145) { 1286 x = matrix.width(); 1287 1288 } 1289 1290 matrix.show(); 1291 delay(scrollingTextSpeed); 1292 1293 1294 } // Close while. 1295 1296 // Reset width and flags. 1297 1298 x 1299 = matrix.width(); 1300 greenPlayerWins = LOW; 1301 1302 } // Close if. 1303 1304 1305 // Reset player array positions. 1306 1307 currentRedArrayPosition = 0; 1308 1309 currentBlueArrayPosition = 0; 1310 currentGreenArrayPosition = 0; 1311 1312 1313 redResetFlag = 0; 1314 blueResetFlag = 0; 1315 greenResetFlag = 0; 1316 1317 1318 redPlayerWins = LOW; 1319 bluePlayerWins = LOW; 1320 greenPlayerWins = LOW; 1321 1322 1323 // Clear down display. 1324 1325 strip.clear(); 1326 strip.show(); 1327 1328 1329 // Restart game. 1330 1331 setup(); 1332 1333} // Close function. 1334 1335/*-----------------------------------------------------------------*/ 1336 1337int 1338 matrixBrightness(void) { 1339 1340 while (brightnessSet == HIGH) { // 1341 Brightness menu flag set by interupt request 1342 1343 // Setting the display 1344 brightness 1345 1346 if (digitalRead(buttonBluePin) == HIGH) 1347 { 1348 1349 if (matrixBrightnessSet == 60) 1350 { 1351 matrixBrightnessSet 1352 = 5; 1353 } 1354 else 1355 { 1356 matrixBrightnessSet 1357 = matrixBrightnessSet + 5; 1358 } 1359 } 1360 if (digitalRead(buttonGreenPin) 1361 == HIGH) 1362 { 1363 if (matrixBrightnessSet == 5) 1364 { 1365 1366 matrixBrightnessSet = 60; 1367 } 1368 else 1369 1370 { 1371 matrixBrightnessSet = matrixBrightnessSet - 5; 1372 1373 } 1374 } 1375 1376 matrix.clear(); 1377 matrix.setCursor(1, 1378 0); 1379 matrix.setTextColor(whiteM); 1380 matrix.setBrightness(matrixBrightnessSet); 1381 1382 matrix.print(F("Br:")); 1383 matrix.show(); 1384 1385 matrix.print(matrixBrightnessSet); 1386 1387 matrix.show(); 1388 1389 delay(buttonDelay); 1390 1391 } // Close 1392 while. 1393 1394 saveBrightness(); 1395 return; 1396 1397} // Close function. 1398 1399/*-----------------------------------------------------------------*/ 1400 1401void 1402 startUpMessage(void) { 1403 1404 // Configure matrix. 1405 1406 matrix.setBrightness(matrixBrightnessSet); 1407 1408 matrix.setTextWrap(false); 1409 1410 // Check game type. 1411 1412 if (game 1413 == twoPlayerShort) { 1414 1415 // Configure and display text. 1416 1417 int 1418 x = matrix.width(); 1419 1420 while (x > -145) { // Only display text 1421 for one pass 1422 1423 matrix.fillScreen(0); 1424 matrix.setCursor(x, 1425 0); 1426 matrix.setTextColor(whiteM); 1427 matrix.print(F("Game: 1428 2 Player Short")); 1429 if (--x < -145) { 1430 x = matrix.width(); 1431 1432 } 1433 matrix.show(); 1434 delay(scrollingTextSpeed); 1435 1436 1437 } // Close while. 1438 1439 // Reset width and flags. 1440 1441 x 1442 = matrix.width(); // This resets the test to the beginning again 1443 1444 } // 1445 Close if. 1446 1447 // Check game type. 1448 1449 else if (game == twoPlayerLong) 1450 { 1451 1452 // Configure and display text. 1453 1454 int x = matrix.width(); 1455 1456 1457 while (x > -145) { // Only display text for one pass 1458 1459 matrix.fillScreen(0); 1460 1461 matrix.setCursor(x, 0); 1462 matrix.setTextColor(whiteM); 1463 1464 matrix.print(F("Game: 2 Player Long")); 1465 if (--x < -145) 1466 { 1467 x = matrix.width(); 1468 } 1469 1470 matrix.show(); 1471 1472 delay(scrollingTextSpeed); 1473 1474 } // Close while. 1475 1476 1477 // Reset width and flags. 1478 1479 x = matrix.width(); // This resets 1480 the test to the beginning again 1481 1482 } // Close if. 1483 1484 // Check game 1485 type. 1486 1487 else if (game == threePlayerLong) { 1488 1489 // Configure 1490 and display text. 1491 1492 int x = matrix.width(); 1493 1494 while (x 1495 > -145) { // Only display text for one pass 1496 1497 matrix.fillScreen(0); 1498 1499 matrix.setCursor(x, 0); 1500 matrix.setTextColor(whiteM); 1501 1502 matrix.print(F("Game: 3 Player Long")); 1503 if (--x < -145) 1504 { 1505 x = matrix.width(); 1506 } 1507 1508 matrix.show(); 1509 1510 delay(scrollingTextSpeed); 1511 1512 } // Close while. 1513 1514 1515 // Reset width and flags. 1516 1517 x = matrix.width(); // This resets 1518 the test to the beginning again 1519 1520 } // Close if. 1521 1522} // Close function. 1523 1524/*-----------------------------------------------------------------*/ 1525 1526void 1527 batteryCharging(uint32_t color, int wait) { 1528 1529 while (button2PlayerShortState 1530 == LOW && button2PlayerLongState == LOW && button3PlayerLongState == LOW) { 1531 1532 1533 // Read game buttons. 1534 1535 button2PlayerShortState = digitalRead(button2PlayerShort); 1536 1537 button2PlayerLongState = digitalRead(button2PlayerLong); 1538 button3PlayerLongState 1539 = digitalRead(button3PlayerLong); 1540 1541 // Blink two leds red to indicate 1542 charging mode enabled. 1543 1544 strip.setBrightness(5); 1545 strip.setPixelColor(0, 1546 color); // Set pixel's color (in RAM) 1547 strip.show(); // 1548 Update strip to matc 1549 delay(wait); // Pause 1550 for a moment 1551 1552 strip.clear(); 1553 strip.show(); 1554 1555 strip.setPixelColor(1, 1556 color); // Set pixel's color (in RAM) 1557 strip.show(); // 1558 Update strip to matc 1559 delay(wait); // Pause 1560 for a moment 1561 1562 strip.clear(); 1563 strip.show(); 1564 1565 // 1566 Enable charging relay pin to switch on, onboard NiCd charging circuit. 1567 1568 digitalWrite(chargingRelayPin, 1569 HIGH); 1570 1571 } // Close while. 1572 1573 // Disable charging relay pin to 1574 switch off, onboard NiCd charging circuit. 1575 1576 digitalWrite(chargingRelayPin, 1577 LOW); 1578 1579 // Restart game. 1580 1581 setup(); 1582 1583} // Close function. 1584 1585/*-----------------------------------------------------------------*/ 1586 1587void 1588 saveBrightness() { 1589 1590 // Display simple set message. 1591 1592 matrix.clear(); 1593 1594 matrix.setCursor(7, 0); 1595 matrix.print(F("Set")); 1596 matrix.show(); 1597 1598 delay(1000); 1599 1600 // Re-running game layout options to force update the 1601 display from brightness screen to game screen. 1602 1603 if (button2PlayerShortState 1604 == HIGH) { 1605 1606 cribbageBoardLayout2PlayerShort(white); 1607 1608 } // 1609 Close if. 1610 1611 else if (button2PlayerLongState == HIGH) { 1612 1613 cribbageBoardLayout2PlayerLong(white); 1614 1615 1616 } // Close if. 1617 1618 else if (button3PlayerLongState == HIGH) { 1619 1620 1621 cribbageBoardLayout3PlayerLong(white); 1622 1623 } // Close if. 1624 1625} 1626 1627/*-----------------------------------------------------------------*/ 1628
Downloadable files
DigiCrib PCB Layout Image
Reference to Design Spark File also attached.
DigiCrib PCB Layout Image
DigiCrib PCB Layout Image
Reference to Design Spark File also attached.
DigiCrib PCB Layout Image
Circuit Board
It is the main circuit board (PCB) for the cribbage board. It has been designed to allow for both 3.3v Arduino's as well as 5v Arduino's. To edit the file, I have been using Design Spark by RS.
Circuit Board
Comments
Only logged in users can leave comments