# LED Matrix Dice Roller

### Parts:

![](https://3423273789-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjRgHmD322MUgRPOyqMLh%2Fuploads%2F6aV6rjbhlbVBg0ypGyYW%2F20220622_081115.jpg?alt=media\&token=42d217a9-f1db-4e01-b976-8db68730ddca)

### Wiring

| Arduino | LED Matrix |
| ------- | ---------- |
| 5V      | VCC        |
| GND     | GND        |
| 12      | DIN        |
| 10      | CS         |
| 11      | CLK        |

Also add button to pin 8. Please see previous tutorials if you don't remember how to add a button.

### Code:

```
/* To install this library, go to
  Sketch --> Include Library--> Manage Libraries
  now search for "LedControl"
  Choose install
*/
#include "LedControl.h"

LedControl lc = LedControl(12, 11, 10, 1);
/* we always wait a bit between updates of the display */

unsigned long delaytime = 300;

//variables for the button
const int buttonPin = 8;
bool released = true;


void setup() {
  //sets up the butonPin as an input
  pinMode(buttonPin, INPUT_PULLUP);

  //this command allows the random function to work better.
  randomSeed(analogRead(0));

  /*
    The MAX72XX is in power-saving mode on startup,
    we have to do a wakeup call
  */
  lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 8);
  /* and clear the display */
  lc.clearDisplay(0);
}

void loop() {

  //check if the button is pressed and it has been released
  if (digitalRead(buttonPin) == LOW and released == true)
  {
    released = false;
    loading();
    showRandom();
    delay(50);
  }

  //check to see when the button gets released
  if (digitalRead(buttonPin) == HIGH) {
    released = true;
    delay(50);
  }
}

//function to generate a random number of the LED Matrix
void showRandom()
{

  int randomNumber = random(1, 7);
  if (randomNumber == 1) {
    lc.setRow(0, 0, B00000000);
    lc.setRow(0, 1, B00000000);
    lc.setRow(0, 2, B00000000);
    lc.setRow(0, 3, B00011000);
    lc.setRow(0, 4, B00011000);
    lc.setRow(0, 5, B00000000);
    lc.setRow(0, 6, B00000000);
    lc.setRow(0, 7, B00000000);

  }
  else if (randomNumber == 2) {
    lc.setRow(0, 0, B11000000);
    lc.setRow(0, 1, B11000000);
    lc.setRow(0, 2, B00000000);
    lc.setRow(0, 3, B00000000);
    lc.setRow(0, 4, B00000000);
    lc.setRow(0, 5, B00000000);
    lc.setRow(0, 6, B00000011);
    lc.setRow(0, 7, B00000011);
  }
  else if (randomNumber == 3) {
    lc.setRow(0, 0, B11000000);
    lc.setRow(0, 1, B11000000);
    lc.setRow(0, 2, B00000000);
    lc.setRow(0, 3, B00011000);
    lc.setRow(0, 4, B00011000);
    lc.setRow(0, 5, B00000000);
    lc.setRow(0, 6, B00000011);
    lc.setRow(0, 7, B00000011);
  }

  else if (randomNumber == 4) {
    lc.setRow(0, 0, B11000011);
    lc.setRow(0, 1, B11000011);
    lc.setRow(0, 2, B00000000);
    lc.setRow(0, 3, B00000000);
    lc.setRow(0, 4, B00000000);
    lc.setRow(0, 5, B00000000);
    lc.setRow(0, 6, B11000011);
    lc.setRow(0, 7, B11000011);
  }

  else if (randomNumber == 5) {
    lc.setRow(0, 0, B11000011);
    lc.setRow(0, 1, B11000011);
    lc.setRow(0, 2, B00000000);
    lc.setRow(0, 3, B00011000);
    lc.setRow(0, 4, B00011000);
    lc.setRow(0, 5, B00000000);
    lc.setRow(0, 6, B11000011);
    lc.setRow(0, 7, B11000011);
  }
  else {
    lc.setRow(0, 0, B11000011);
    lc.setRow(0, 1, B11000011);
    lc.setRow(0, 2, B00000000);
    lc.setRow(0, 3, B11000011);
    lc.setRow(0, 4, B11000011);
    lc.setRow(0, 5, B00000000);
    lc.setRow(0, 6, B11000011);
    lc.setRow(0, 7, B11000011);
  }

  //function to run before the numer is displayed
}
void loading() {
  lc.setRow(0, 0, B11111111);
  lc.setRow(0, 1, B10000001);
  lc.setRow(0, 2, B10000001);
  lc.setRow(0, 3, B10000001);
  lc.setRow(0, 4, B10000001);
  lc.setRow(0, 5, B10000001);
  lc.setRow(0, 6, B10000001);
  lc.setRow(0, 7, B11111111);
  delay(delaytime);
  lc.setRow(0, 0, B00000000);
  lc.setRow(0, 1, B01111110);
  lc.setRow(0, 2, B01000010);
  lc.setRow(0, 3, B01000010);
  lc.setRow(0, 4, B01000010);
  lc.setRow(0, 5, B01000010);
  lc.setRow(0, 6, B01111110);
  lc.setRow(0, 7, B00000000);
  delay(delaytime);
  lc.setRow(0, 0, B00000000);
  lc.setRow(0, 1, B00000000);
  lc.setRow(0, 2, B00111100);
  lc.setRow(0, 3, B00100100);
  lc.setRow(0, 4, B00100100);
  lc.setRow(0, 5, B00111100);
  lc.setRow(0, 6, B00000000);
  lc.setRow(0, 7, B00000000);
  delay(delaytime);
  lc.setRow(0, 0, B00000000);
  lc.setRow(0, 1, B00000000);
  
  lc.setRow(0, 2, B00000000);
  lc.setRow(0, 3, B00011000);
  lc.setRow(0, 4, B00011000);
  lc.setRow(0, 5, B00000000);
  lc.setRow(0, 6, B00000000);
  lc.setRow(0, 7, B00000000);
  delay(delaytime);
  lc.setRow(0, 0, B00000000);
  lc.setRow(0, 1, B00000000);
  lc.setRow(0, 2, B00111100);
  lc.setRow(0, 3, B00100100);
  lc.setRow(0, 4, B00100100);
  lc.setRow(0, 5, B00111100);
  lc.setRow(0, 6, B00000000);
  lc.setRow(0, 7, B00000000);
  delay(delaytime);
  lc.setRow(0, 0, B00000000);
  lc.setRow(0, 1, B01111110);
  lc.setRow(0, 2, B01000010);
  lc.setRow(0, 3, B01000010);
  lc.setRow(0, 4, B01000010);
  lc.setRow(0, 5, B01000010);
  lc.setRow(0, 6, B01111110);
  lc.setRow(0, 7, B00000000);
  delay(delaytime);
  lc.setRow(0, 0, B11111111);
  lc.setRow(0, 1, B10000001);
  lc.setRow(0, 2, B10000001);
  lc.setRow(0, 3, B10000001);
  lc.setRow(0, 4, B10000001);
  lc.setRow(0, 5, B10000001);
  lc.setRow(0, 6, B10000001);
  lc.setRow(0, 7, B11111111);
  delay(delaytime);
}
  
```

### Other Projects:

#### Snake Game

{% embed url="<https://www.youtube.com/watch?v=nXdEqbL_6jg>" %}

#### Pong Game

{% embed url="<https://www.dofbot.com/post/pong-game-using-dot-matrix-display-8x8>" %}

#### Atari Breakout

{% embed url="<http://makertech.dk/8x8-led-matrix-breakout-arduino-game/>" %}
