#include <LedControl.h>
#include <LiquidCrystal.h>
// MAX7219 (DIN, CLK, CS)
LedControl lc = LedControl(19, 18, 17, 1);
// LCD (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int button = 15;
// Dice patterns
byte six[8]={B00000000,B11011011,B11011011,B00000000,B00000000,B11011011,B11011011,B00000000};
byte five[8]={B00000000,B01100110,B01100110,B00011000,B00011000,B01100110,B01100110,B00000000};
byte four[8]={B00000000,B01100110,B01100110,B00000000,B00000000,B01100110,B01100110,B00000000};
byte three[8]={B11000000,B11000000,B00000000,B00011000,B00011000,B00000000,B00000011,B00000011};
byte two[8]={B00000000,B00000000,B00000000,B01100110,B01100110,B00000000,B00000000,B00000000};
byte one[8]={B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000};
int stats[6] = {0,0,0,0,0,0};
void setup() {
pinMode(button, INPUT_PULLUP);
lc.shutdown(0,false);
lc.setIntensity(0,10);
lc.clearDisplay(0);
lcd.begin(16,2);
lcd.print("Digital Dice");
delay(1000);
lcd.clear();
randomSeed(analogRead(26)); // Pico ADC pin
}
// Display dice
void showDice(byte pattern[8]){
for(int i=0;i<8;i++){
lc.setRow(0,i,pattern[i]);
}
}
// Roll dice
int rollOnce(){
int x = random(1,7);
switch(x){
case 1: showDice(one); break;
case 2: showDice(two); break;
case 3: showDice(three); break;
case 4: showDice(four); break;
case 5: showDice(five); break;
case 6: showDice(six); break;
}
stats[x-1]++;
return x;
}
// Stats display
void showStats(){
for(int i=0;i<6;i++){
lcd.clear();
lcd.print("Face ");
lcd.print(i+1);
lcd.setCursor(0,1);
lcd.print("Count: ");
lcd.print(stats[i]);
delay(1200);
}
}
void loop() {
if(digitalRead(button) == LOW){ // PULLUP logic
delay(50);
unsigned long pressTime = 0;
while(digitalRead(button) == LOW){
delay(10);
pressTime += 10;
if(pressTime >= 3000) break;
}
// STATS
if(pressTime >= 3000){
showStats();
}
// TWO DICE
else if(pressTime >= 1000){
int d1 = rollOnce();
delay(500);
int d2 = rollOnce();
int sum = d1 + d2;
lcd.clear();
lcd.print(d1);
lcd.print(" + ");
lcd.print(d2);
lcd.print(" =");
lcd.setCursor(0,1);
lcd.print(sum);
}
// SINGLE
else{
int d = rollOnce();
lcd.clear();
lcd.print("Dice: ");
lcd.print(d);
}
delay(500);
}
}