// DICE FOR WS2812 3x7 MATRIX xfpd
bool debug = 0; // "1" for serial monitor output
/*
PIXEL INDEXING FOR DICE PIPS
DIE MULTIPLIER (d) 0 * 4 1 * 4
ROW MULTIPLIER (r) 0 * 7 | 00 .. 02 | 03 | 04 .. 06 |
ROW MULTIPLIER (r) 1 * 7 | 07 08 09 | 10 | 11 12 13 |
ROW MULTIPLIER (r) 2 * 7 | 14 .. 16 | 17 | 18 .. 20 |
COLUMN INDEX (c) +0 +1 +2 +4 +5 +6
*/
byte dieface[] = { // binary value for each row of pips
0, 2, 0, // 1 000 010 000
1, 0, 4, // 2 001 000 100
1, 2, 4, // 3 001 010 100
5, 0, 5, // 4 101 000 101
5, 2, 5, // 5 101 010 101
5, 5, 5 // 6 101 101 101
};
#include <Adafruit_NeoPixel.h> // https://github.com/adafruit/Adafruit_NeoPixel
#define PIN 4 // NeoPixel pin
#define PIX 21 // Number of NeoPixels
Adafruit_NeoPixel led = Adafruit_NeoPixel(PIX, PIN, NEO_GRB + NEO_KHZ800);
byte buttonPin = 2;
byte die[2]; // two dice
#define WHT 128, 128, 128
#define BLK 0, 0, 0
#define ORG 255, 128, 0
#define PIP 0, 255, 0
#define COLOR ORG
void setup() {
Serial.begin(115200); // initialize serial communications
randomSeed(analogRead(A0)); // initialize pseudorandomness
pinMode(buttonPin, INPUT_PULLUP); // button to roll dice
led.begin(); // initialize Neopixel object/instance
divider(); // show dice divider
led.show(); // display pixel buffer
}
void loop() {
while (digitalRead(buttonPin)); // press button to roll dice
delay(150); // debounce button
divider(); // show dice divider
roll(); // roll the dice
led.show(); // display dice and divider
}
void divider() { // color middle column
led.fill(led.Color(BLK)); // background colr
for (int i = 0; i < 3; i++) { // three center LEDs
led.setPixelColor(i * 7 + 3, led.Color(COLOR)); // divider color
}
}
void roll() { // d = die multiplier, r = row multiplier, c = column/bit index
if (debug) Serial.println(); // separate rolls
for (int d = 0; d < 2; d++) { // roll two dice
die[d] = random(0, 6); // face value of each die from 1 to 6
for (int r = 0; r < 3; r++) { // three binary value rows per face
if (debug) if (d) Serial.print(" "); // offset second die
for (int c = 0; c < 3; c++) { // three bits per row
bool pip = bitRead(dieface[die[d] * 3 + r], 2 - c); // each bit, MSB first (2 - c)
if (debug) Serial.print(pip); // print pips
if (pip) // if bit is set...
led.setPixelColor(d * 4 + r * 7 + c, led.Color(PIP)); // ...color pixel (PIXEL INDEXING)
}
if (debug) Serial.println(); // separate each die
}
}
}ROLL
E-cap
1000uF