# LED Matrix Dice Roller

### Parts:

![](/files/uvdMKJot7IBM9I3xgNax)

### 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/>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guides.mccaskeyrobotics.org/arduino/led-matrix-dice-roller.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
