I worked it out both ways, though the first way I did it was buggy and clumsy, but it worked.
I am quite sure I can improve upon it, and might try someday
/* this is a sketch to use a single 7 segment display as many different kinds of dice.
the choices of dice value are 2, 4, 6, 8, 10, 12, and 20, based around a complete set of D&D dice values
it uses a button press to select and display which mode of dice you are using
and a tilt switch for shake-to-roll activation of random number generator.
no external ICs were harmed (or used) in the making of this sketch */
byte Display[23][8] = //define 7seg digit patern
{
{0,1,1,0,0,0,0,0}, // "1"
{1,1,0,1,1,0,1,0}, // "2"
{1,1,1,1,0,0,1,0}, // "3"
{0,1,1,0,0,1,1,0}, // "4"
{1,0,1,1,0,1,1,0}, // "5"
{1,0,1,1,1,1,1,0}, // "6"
{1,1,1,0,0,0,0,0}, // "7"
{1,1,1,1,1,1,1,0}, // "8"
{1,1,1,0,0,1,1,0}, // "9"
{1,1,1,1,1,1,0,1}, // "10"
{0,1,1,0,0,0,0,1}, // "11"
{1,1,0,1,1,0,1,1}, // "12"
{1,1,1,1,0,0,1,1}, // "13"
{0,1,1,0,0,1,1,1}, // "14"
{1,0,1,1,0,1,1,1}, // "15"
{1,0,1,1,1,1,1,1}, // "16"
{1,1,1,0,0,0,0,0}, // "17"
{1,1,1,1,1,1,1,1}, // "18"
{1,1,1,0,0,1,1,1}, // "19"
{0,1,1,0,1,1,0,1}, // "20"
{1,1,1,1,1,1,0,0}, // "0"
{0,1,1,1,1,0,1,0}, // "d"
{0,0,0,0,0,0,0,0}, // off
};
int dValue[] = //define dice value array
{2, 4, 6, 8, 10, 12, 20};
const int Button = 12; //assign push button to pin 12
const int Tilt = 11; //assign tilt switch to pin 11
int mode = 0; //assign variable mode, and store
int oldMode = 0; //assign variable old mode, and store
int val = 0; //assign variable val, and store
int oldVal = 0; //assign variable old val and store
int state = 0; //assign variable state, and store
void setup()
{
pinMode(Button, INPUT); //assign each pin used as input or output
pinMode(Tilt, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
writeDot(0); //start with "dot" off
randomSeed(analogRead(0)); //read analog pin 0 to seed random number
}
void writeDot(byte dot) //assign function writeDot as a byte named "dot"
{
digitalWrite(9, dot); //write dot to pin 9
}
void sevenSegWrite(byte digit) //assign function sevenSegWrite to byte "digit"
{
byte pin = 2; //start writing sevenSegWrite at pin 2
for (byte segCount = 0; segCount < 8; ++segCount) // increpent segCount up by one
{
digitalWrite(pin, Display[digit][segCount]); //Write "pin" 1 or 0 according to the defined array
++pin; //advance to next pin
}
}
void loop()
{
{
mode = digitalRead(Button); //read push button
if ((mode == HIGH) && (oldMode == LOW)) //check button state
{
state++; //if button mode has changed, increment state up by one
{
sevenSegWrite(21); //if state has gone up, display "d" on 7seg
delay(500); //delay .5 second
sevenSegWrite(22); //turn 7seg off
delay(250); // delay .25 second
sevenSegWrite(dValue[state] - 1); //display the dice value currently selected
delay(500); //delay .5 second
sevenSegWrite(22); //turn off 7seg
}
}
if (state > 6) //if state is greater than 6 roll back to 0 then delay 10mS
{
state = 0;
delay(10);
}
oldMode = mode; //old mode is now the mode
}
val = digitalRead(Tilt); //read tilt switch
if ((val == HIGH) && (oldVal== LOW)) //check for switch state change
{
{
digitalWrite(9, HIGH); //displays a . . . on 7 seg
delay(250); //before showing dice result
digitalWrite(9,LOW); //did this for dice rolling effect
delay(250); //dramatic pause kind of thing
digitalWrite(9, HIGH);
delay(250);
digitalWrite(9,LOW);
delay(250);
digitalWrite(9, HIGH);
delay(250);
digitalWrite(9,LOW);
delay(250);
}
{
byte Result = random(dValue[state]); //assign variable Result and set it equal to the random seed related to the state count as a upper limit
sevenSegWrite(Result); //display the result on 7seg
delay(3000); //delay 3 seconds
sevenSegWrite(22); //turn off 7seg
}
}
digitalWrite(13, HIGH); //turn on led @ pin 13 as "on" indicator light
}