# Dice Simulation on a 3x3 LED Matrix

Hi there. So for an assignment for Grade 12 Comp. Engineering, my teacher made us solder a bunch of LEDs together to make a 3x3 matrix. Then he told us to create a dice simulation so that whenever you press the push button, the numbers on the dice change to a random one. I’m really new to coding so this is going to be pretty basic but here is my code so far:

``````int pin [2][3]={ {2, 3, 4}, {8, 9, 10} };
int power [3] = {2, 3, 4};
int ground [3] = {8, 9, 10};
int pushButton= 13;
int randm = 6;

void setup() {

for (int i=2;i<11;i++){
pinMode (i,OUTPUT);
}
pinMode(pushButton, INPUT);

}

void loop() {
off ();
if (randm = 1) {
one();
}
else if (randm == 2) {
two();
}
else if(randm == 2) {
three();
}

else if(randm == 4) {
four();
}
else if(randm == 5) {
five ();
}

else if(randm == 6) {
six ();
}

if (digitalRead(13) == HIGH) {
randm = random (1, 6);
}
Serial.print(2);
}
void one () {
digitalWrite (power [1], HIGH);
digitalWrite (ground [1], LOW);
delay(100);
}
void two (){
digitalWrite (power[0],HIGH);
digitalWrite (ground[2],LOW);
delay(100);
off();
digitalWrite (power[2],HIGH);
digitalWrite (ground[0],LOW);
delay(100);
}

void three (){
two();
off();
one();
off(); //take off if doesn't work.
}

void four(){
digitalWrite (power[0],HIGH);
digitalWrite (ground[0],LOW);
digitalWrite (power[2],HIGH);
digitalWrite (ground[2],LOW);
delay(100);
}
void five(){
one();
delay(100);
off();
four ();
}
void six(){
digitalWrite (power[0],HIGH);
digitalWrite (power[2],HIGH);
digitalWrite (ground[0],LOW);
digitalWrite (ground[1],LOW);
digitalWrite (ground[2],LOW);

delay(100);
}

void off (){
for (int j=0;j<3;j++){
digitalWrite (power[j],LOW);
}
for (int i=0;i<3;i++){
digitalWrite (ground[i],HIGH);
}
}
``````

I checked to see if it worked with the hardware this morning but it didn’t. If you see anything wrong or missing in this code, please point it out. Thank you.

`if (randm = 1)`always true.

When you've fixed that one, this one will bite you.

``````else if (randm == 2) {
two();
}
else if(randm == 2) {
three();
``````

oh so it was just a couple careless mistakes? The rest is fine?

The code looks like you have the LED connected between two pins. Did your teacher tell you to do this? It is not the normal way of doing things. I hope there is an resistor in line with each LED as well.

To be honest the code is a bit pedestrian, by using arrays to hold the bit patterns it could be made about 10 times shorter.

Grumpy_Mike: The code looks like you have the LED connected between two pins. Did your teacher tell you to do this? It is not the normal way of doing things. I hope there is an resistor in line with each LED as well.

To be honest the code is a bit pedestrian, by using arrays to hold the bit patterns it could be made about 10 times shorter.

Well he said that he recommended it since he personally found it easier and yes, we used resistors.

could you explain how to shorten the code?

Well it is an un necessary waste of a pin.

Each dice pattern is a 9 bit pattern of zeros and ones. Therefore can be put in an int. an array of 6 of these ints define all possible patterns. Then all your code has to do is to read the bits in the int and light up the appropriate LEDs. So you are left with one function that reads the bits in the int, you just pass to it the number you want to display. About ten lines tops.