Modified a simle project from gcduino.com and made it a random dice with roll down feature.
Get a random value from pin A5 (note pin in mid air in photos) of Atmega and uses that as the an offset for the random number generator.
Here is the code
/*
* SevenSegment sketch with dice roll down and button press
* Shows numerals ranging from 1 through 6 on a single-digit display
*/
boolean numArray[10][8] = {
// C,E,D,dp,B,F,G,A segments
{1,1,1,0,1,1,0,1}, //Zero ===A(17)===
{1,0,0,0,1,0,0,0}, //One | |
{0,1,1,0,1,0,1,1}, //Two F(15) B(14)
{1,0,1,0,1,0,1,1}, //Three | |
{1,0,0,0,1,1,1,0}, //Four ===G(16)===
{1,0,1,0,0,1,1,1}, //Five | |
{1,1,1,0,0,1,1,1}, //Six E(11) C(10)
{1,0,0,0,1,0,0,1}, //Seven | |
{1,1,1,0,1,1,1,1}, //Eight ====D(12)== DP-13
{1,0,1,0,1,1,1,1} //Nine
};
const int button = 8; //set button to input 8
int randNummer = 0;
void setup(){
for(int i = 10; i <= 17; i++) { //Setup the pins controlling the 7 segment display
pinMode(i, OUTPUT);
digitalWrite(i, HIGH); //Turn all segments off
pinMode(button, INPUT); //Set button as input
}
}
void loop()
{
boolean btnState = digitalRead(button); // Read the state of the button, either 5v or 0v and store it in the variable btnState
if(btnState == LOW){ // if the button state is HIGH run the code between the "{}" brackets
randomSeed(analogRead(5)); // used A5 as random offset for random generator
randNummer = random(1,7); // Random number between 1 and 6 (7max-1)
digitalWrite(16, HIGH); // Clear the middles segment
for(int w = 5; w < 12; w = w + 3){ //This is the slow down loop
for(int a = 6; a >= 0; a--){ //Count down through the Numbers
for(int j = 0; j <= 6; j++){ //Loop through the d6 outer segment
int myArray[6]={11,15,17,14,10,12}; //sequence of segments for the roll display
delay(w); //variable delay
digitalWrite((myArray[j]), LOW); //set segment on
delay(w); //variable delay
digitalWrite((myArray[j]), HIGH); //set segment off
}
}
}
for(int j = 0; j <= 8; j++){ //Loop through the segments to make up number display
digitalWrite(j+10, !numArray[randNummer][j]); //look up array for the random number
}
}
else {
digitalWrite(13,LOW); //flash the dot on and off
delay(100);
digitalWrite(13,HIGH);
delay(100);
}
}