Simple First Project, Roll a Die.

So, I got my arduino in today, pretty awesome stuff... even though it was shipped in just a simple padded envelope, it works fine. Technically my first project was to put an led between 13 and ground just to test it was working, but that's neither here nor there.

My first project was to make a Die rolling program (single for of dice of course), and it worked just fine after a few tweaks, such as changing the pins to not use pin 1 and 2, cause apparently that causes uploading problems, even though those pins can be used after uploading, it crashes during the upload.

Anyway, like I said, works fine. It has 7 led's used in a grid configuration so that I could make it look like an actual die. Could I use less pins and do a matrix? Probably, but I haven't been able to grasp the process of matrixing since I didn't have anything to test with. (seems simple enough though from what I've seen)

Here is my code if anyone cares. Pin 13 is a push button, the rest are leds. (pinmid being the middle pin) OH!! I also send back serial information... that was easier then I thought too.

//cm Roll Dice program

int roll = 13;  //Button to get new roll
int die11 = 12; //led 1 for die 1
int die12 = 11; //led 2 for die 1
int die13 = 10; //led 3 for die 1
int die14 = 9; //led 4 for die 1
int die15 = 8; //led 5 for die 1
int die16 = 7; //led 6 for die 1
int diemid = 6;

//I plan to separate my physical variables (things going to and from physical world objects
//and virtual variables (values only seen by the arduino)
int value1 = 0; //die #1
int value2 = 0; //die #2 - not implemented yet.

void setup()
{
 pinMode(roll, INPUT);
 pinMode(die11,OUTPUT);
 pinMode(die12,OUTPUT);
 pinMode(die13,OUTPUT);
 pinMode(die14,OUTPUT);
 pinMode(die15,OUTPUT);
 pinMode(die16,OUTPUT);

 Serial.begin(9600);//To delete later, simply for checking results as values
}

void loop()
{
 if(digitalRead(roll) == HIGH)
 {
   randomSeed(millis()); //random based on milliseconds
                         //remember, there are 1,000 milliseconds in a second, so the odds of hitting the same number once is rare,
                         //but to hit the same number every single time would just be crazy rare, so this should work great.
                        
   value1 = random(1,7); //random value of 1 through 6 based on new seed, it seems that the high number in arduino's
                         //random is NEVER picked, hence add one to the max number. Note though, the low numnber is picked.

   digitalWrite(die11, LOW);
   digitalWrite(die12, LOW);
   digitalWrite(die13, LOW);
   digitalWrite(die14, LOW);
   digitalWrite(die15, LOW);
   digitalWrite(die16, LOW);
   digitalWrite(diemid, LOW);
   
   if(value1 == 1)
   {
     digitalWrite(diemid, HIGH);
   }
   
   if(value1 == 2)
   {
     digitalWrite(die11, HIGH);
     digitalWrite(die16, HIGH);
   }
   
   if(value1 == 3)
   {
     digitalWrite(die11, HIGH);
     digitalWrite(diemid, HIGH);
     digitalWrite(die16, HIGH);
   }   
   
   if(value1 == 4)
   {
     digitalWrite(die11, HIGH);
     digitalWrite(die13, HIGH);
     digitalWrite(die14, HIGH);
     digitalWrite(die16, HIGH);
   }
   
   if(value1 == 5)
   {
     digitalWrite(diemid, HIGH);
     digitalWrite(die11, HIGH);
     digitalWrite(die13, HIGH);
     digitalWrite(die14, HIGH);
     digitalWrite(die16, HIGH);
   }
   
   if(value1 == 6)
   {
     digitalWrite(die11, HIGH);
     digitalWrite(die12, HIGH);
     digitalWrite(die13, HIGH);
     digitalWrite(die14, HIGH);
     digitalWrite(die15, HIGH);
     digitalWrite(die16, HIGH);
   }
   
   Serial.print("Value1: ");
   Serial.print(value1);
   Serial.print("      Value2: ");
   Serial.print(value2);
   Serial.print("         Millis: ");
   Serial.print(millis());
   Serial.println();
 }
}

You know, you can make a working led die with only 4 pins, no multiplexing.

The numbers below represent dots on the die. Just connect same-numbered dots to the same pin, and it can still work.

2 3

4 1 4

3 2

light them like this for each number: 1: set 1 2: set 2 or 3 3: set 2 or 3, AND set 1 4: set 2 AND 3 5: set 2, 3, AND 1 6: set 2, 3, AND 4 ?!?! 7: set 1, 2, 3, AND 4

There you go! 3 extra pins. (useful for more dies.)

Thanks, I figured there was a simpler way XD. But it was my first project, and a learning experience. ^_^ Good point though, and I fully understand what you mean.