Project tutorial

Random Number Generator with 8x8 LED Matrix and Arduino © CC BY-NC-ND

This is a random number generator using 8x8 LED matrix and Arduino. It is adjustable from 1 to 60 and includes a timer play mode.

  • 1,495 views
  • 2 comments
  • 9 respects

Components and supplies

Apps and online services

About this project

This device is an random number generator using 8x8 LED matrix and driven by Arduino.

It is fully adjustable to work in the range of 1 to 60.

Assembly is very simple and the device can be used in many applications where they require a group of random numbers.

There are two main modes of operation:

Mode 1: Random generation (N = population size and n = sample size).

Mode 2: Timer for draw (1 to 60 seconds).

Video

1. Components

2. Setup & Operation

There are 3 buttons for the setup and operation:

  • Mode: navigation for all modes

(N) population size

(n) sample size

(>) Manual Draw

(>>) Timer of Draw

  • Down: decrease number (for modes N, n or timer).
  • Up: increase number (for modes N, n or timer).

Code

Random_Number_Generator_with_8x8_LED_Matrix_V2_0.inoArduino
Code for Arduino UNO-R3
License: CC BY-NC-ND 4.0 (Attribution-NonCommercial-NoDerivatives 4.0 International)
/*
   Project:   Random Number Generator
   Author:    LAGSILVA
   Hardware:  Arduino UNO-R3, MAX72XX LED Matrix (8x8)
   Date:      20.Jul.2019
   Revision:  2.0
   License:   CC BY-NC-ND 4.0
              (Attribution-NonCommercial-NoDerivatives 4.0 International)
*/

#include <LedControl.h>                 // Library for LED Display - MAX72XX

// Global Variables
byte pDown = 10, pUp = 9, pMode = 8;
String shuffleDezenas, dezenas;
byte row, col, mode = 3;
byte btnMode, btnUp, btnDown;
byte totGrupo = 6, totDez = 60;
float timer = 1;
unsigned long ti, tf;

/*
  Pin numbers of Arduino to be connected into MAX72XX (LED Matrix controlled by MAX72XX)
  pin 5 is connected to the DataIn (DIN)
  pin 6 is connected to LOAD (CS)
  pin 7 is connected to the CLK (CLK)
*/

LedControl lc = LedControl(5, 7, 6, 1); // LedControl(int dataPin, int clkPin, int csPin, int numDevices)


void setup() {

  pinMode(pUp, INPUT_PULLUP);
  pinMode(pDown, INPUT_PULLUP);
  pinMode(pMode, INPUT_PULLUP);

  // Setup of Display "0"
  lc.shutdown(0, false);                // Wakeup Display "0"
  lc.setIntensity(0, 8);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(0);                   // Clear Display "0" (Hour)

  randomSeed(analogRead(A1));
  numbers();

}


void shuffle() {            // Shuffling routine
  byte k, n;
  String tempSeq = dezenas;
  shuffleDezenas = "";
  for (k = 0; k < totGrupo; k++) {
    n = random(tempSeq.length() / 2);
    shuffleDezenas = shuffleDezenas + tempSeq.substring(n * 2 , n * 2 + 2);
    tempSeq.remove(n * 2, 2);
  }
}


void numbers() {          // Numbers to be drawn
  dezenas = "";
  for (byte k = 1; k <= totDez; k++) {
    if (k < 10) {
      dezenas = dezenas + "0" + k;
    }
    else {
      dezenas = dezenas + k;
    }
  }
}


void plot(byte tot) {
  byte row, col;
  if (tot > 4) {
    row = (12 - tot % 8) % 8;
    col = (tot + 3) / 8;
  }
  else {
    row = 6 - tot;
    col = 0;
  }
  lc.setLed(0, row, col, true);
}


void loop() {

  btnMode = digitalRead(pMode);
  btnUp = digitalRead(pUp);
  btnDown = digitalRead(pDown);

  if (btnMode == LOW) {
    mode = (mode + 1) % 4;            // Modes: 0 (N = Population Size)) / 1 (n = Sample Size) / 2 (Manual Draw)) / 3 (Timer of Draw)
    lc.clearDisplay(0);
    if (mode <= 1) {
      lc.setLed(0, 7 - mode, 0, true);
    } else {
      lc.setLed(0, 3 - mode, 0, true);
    }
    delay(150);
    if (mode == 0) {
      plot(totDez);
    }
    if (mode == 1) {
      plot(totGrupo);
    }
  }

  if (mode == 0 && btnUp == LOW) {    // Mode 0: Setup of Population (Increase mode)
    lc.clearDisplay(0);
    lc.setLed(0, 7, 0, true);
    totDez += 1;
    if (totDez > 60) {
      totDez = 1;
    }
    plot(totDez);
    numbers();
    delay(150);
  }

  if (mode == 0 && btnDown == LOW) {  // Mode 0: Setup of Population Size (Decrease mode)
    lc.clearDisplay(0);
    lc.setLed(0, 7, 0, true);
    totDez -= 1;
    if (totDez < 1) {
      totDez = 60;
    }
    plot(totDez);
    numbers();
    delay(150);
  }

  if (mode == 1 && btnUp == LOW) {    // Mode 1: Setup of Sample Size (Increase mode)
    lc.clearDisplay(0);
    lc.setLed(0, 6, 0, true);
    totGrupo += 1;
    if (totGrupo > 60) {
      totGrupo = 1;
    }
    plot(totGrupo);
    delay(150);
  }

  if (mode == 1 && btnDown == LOW) {  // Mode 1: Setup of Sample Size (Decrease mode)
    lc.clearDisplay(0);
    lc.setLed(0, 6, 0, true);
    totGrupo -= 1;
    if (totGrupo < 1) {
      totGrupo = 60;
    }
    plot(totGrupo);
    delay(150);
  }

  if (mode == 2 && (btnUp == LOW || btnDown == LOW)) {    // Mode 2: Manual Draw of Number
    for (byte s = 1; s <= 20; s++) {
      lc.clearDisplay(0);
      lc.setLed(0, 1, 0, true);
      shuffle();
      for (byte k = 0; k < totGrupo; k++) {
        byte dez = (shuffleDezenas.substring(k * 2, k * 2 + 2)).toInt();
        plot(dez);
      }
      delay(25);
    }
  }

  if (mode == 3 && btnUp == LOW) {      // Mode 3: Increase the time lapse
    lc.clearDisplay(0);
    lc.setLed(0, 0, 0, true);
    timer += 1;
    if (timer > 60) {
      timer = 0;
    }
    plot(timer);
    delay(150);
    ti = millis();
  }

  if (mode == 3 && btnDown == LOW) {    // Mode 3: Decrease the time lapse
    lc.clearDisplay(0);
    lc.setLed(0, 0, 0, true);
    timer -= 1;
    if (timer < 0) {
      timer = 60;
    }
    plot(timer);
    delay(150);
    ti = millis();
  }

  if (mode == 3) {                      // Automatic draw of numbers by timer
    if (timer == 0) {
      timer = 0.050;
    }
    tf = millis() - ti;
    if (tf >= timer * 1000) {
      lc.clearDisplay(0);
      lc.setLed(0, 0, 0, true);
      shuffle();
      for (byte k = 0; k < totGrupo; k++) {
        byte dez = (shuffleDezenas.substring(k * 2, k * 2 + 2)).toInt();
        plot(dez);
        ti = millis();
      }
    }
  }

}

Custom parts and enclosures

Paper Template for 8x8 LED Matrix
Paper Template for 8x8 LED Matrix

Schematics

Schematic - Random Number Generator
Schematic for Bread Board

Comments

Similar projects you might like

Analog Clock with LED Matrix and Arduino

Project in progress by LAGSILVA

  • 15,049 views
  • 13 comments
  • 53 respects

Simple Clock with LED Matrix

Project in progress by LAGSILVA

  • 3,753 views
  • 8 comments
  • 11 respects

Stopwatch and Lap Timer with Arduino

Project in progress by LAGSILVA

  • 19,107 views
  • 15 comments
  • 38 respects

Digital And Binary Clock With Two LED Matrix And RTC

Project in progress by LAGSILVA

  • 9,350 views
  • 29 comments
  • 39 respects

Reaction Timer - F1 Style

Project in progress by LAGSILVA

  • 1,798 views
  • 1 comment
  • 10 respects

Amazing Binary Clock using LED Matrix Module

Project in progress by LAGSILVA

  • 10,786 views
  • 12 comments
  • 60 respects
Add projectSign up / Login