Electronics Champ
Published

Electronic Dice using Touch Sensor, LED Matrix, and Arduino

When the Touch Sensor is tapped, the Arduino is programmed to generate a random number between 1 and 6. This number is shown on the display.

BeginnerFull instructions provided1,257
Electronic Dice using Touch Sensor, LED Matrix, and Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Touch Sensor
×1
8x8 LED Matrix
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

Code

Arduino
/*
   This program simulates a dice by generating a randome number between
   1-6. The electronic dice can
   be used with some board games that require a dice to play.
   A touch sensor is interfaced with arduino, and on every touch,
   a random number between 1-6 is generated and
   displayed on an LED Matrix.
   This program is made by Shreyas for
   Electronics Champ YouTube Channel.
   Please subscribe to this channel
   Thank You
*/

//Including the library
#include <LedControl.h>

int DIN = 10;
int CS =  9;
int CLK = 8;
int sensor = 7;
int randomNumber;
String patternNumber = "0";

/*
   Set the DIN (Data in) pin
   Set the CLK (Clock) pin
   Set the CS (Load pulse) pin
   Set the number of displays being used
*/
LedControl matrix = LedControl(DIN, CLK, CS, 1);

void setup() {

  /*
    The Matrix is in power-saving mode on startup,
    we have to do a wakeup call
  */
  matrix.shutdown(0, false);
  matrix.setIntensity(0, 8);      //Adjust the brightness; maximum is 15
  randomSeed(analogRead(A0));     //Read from unconnected analog pin A0
  pinMode(sensor, INPUT);         //Set pin 7 as input

  while (digitalRead(sensor) == LOW) { //This loop is used to display the pattern on startup

    displayPattern();    //function to dispaly a pattern on the display
    continue;

  }

}

void loop() {

  if (digitalRead(sensor) == HIGH) {      //If the sensor is touched

    randomNumber = random(1, 7); //Chooses a random number between 1 and 6
    displayNumber();             //A function to display the number on the display

  }

}

//This function is used to set the position of the individual LEDs in the matrix
void setBit(byte pos[]) {

  int i = 0;

  for (i = 0; i < 8; i++) {

    matrix.setRow(0, i, pos[i]);

  }

}

void displayNumber() {
  //binary codes for individual LEDs to represent 1 to 6 of the dice
  byte one[8] = {   B00000000,
                    B00000000,
                    B00000000,
                    B00011000,
                    B00011000,
                    B00000000,
                    B00000000,
                    B00000000
                };

  byte two[8] = {   B00000000,
                    B00000000,
                    B00110000,
                    B00110000,
                    B00001100,
                    B00001100,
                    B00000000,
                    B00000000
                };

  byte three[8] = {   B00000000,
                      B01100000,
                      B01100000,
                      B00011000,
                      B00011000,
                      B00000110,
                      B00000110,
                      B00000000
                  };

  byte four[8] = {   B00000000,
                     B01100110,
                     B01100110,
                     B00000000,
                     B00000000,
                     B01100110,
                     B01100110,
                     B00000000
                 };

  byte five[8] = {   B00000000,
                     B01100110,
                     B01100110,
                     B00011000,
                     B00011000,
                     B01100110,
                     B01100110,
                     B00000000
                 };

  byte six[8] = {   B00000000,
                    B11011011,
                    B11011011,
                    B00000000,
                    B00000000,
                    B11011011,
                    B11011011,
                    B00000000
                };

  if (randomNumber == 1) {       //If the number is 1...

    displayPattern();            //Displays the pattern
    setBit(one);                 //Displays the number

  }

  else if (randomNumber == 2) {  //If the number is 2...

    displayPattern();            //Displays the pattern
    setBit(two);                 //Displays the number

  }

  else if (randomNumber == 3) {  //If the number is 3...

    displayPattern();            //Displays the pattern
    setBit(three);               //Displays the number

  }

  else if (randomNumber == 4) {  //If the number is 4...

    displayPattern();            //Displays the pattern
    setBit(four);                //Displays the number

  }

  else if (randomNumber == 5) {  //If the number is 5...

    displayPattern();            //Displays the pattern
    setBit(five);                //Displays the number

  }

  else if (randomNumber == 6) {  //If the number is 6...

    displayPattern();            //Displays the pattern
    setBit(six);                 //Displays the number

  }

}

void displayPattern() {
  //binary code for a pattern
  byte pattern1[8] {   B11111111,
                       B10000001,
                       B10000001,
                       B10000001,
                       B10000001,
                       B10000001,
                       B10000001,
                       B11111111

                   };

  byte pattern2[8] {   B00000000,
                       B01111110,
                       B01000010,
                       B01000010,
                       B01000010,
                       B01000010,
                       B01111110,
                       B00000000

                   };

  byte pattern3[8] {   B00000000,
                       B00000000,
                       B00111100,
                       B00100100,
                       B00100100,
                       B00111100,
                       B00000000,
                       B00000000

                   };

  byte pattern4[8] {   B00000000,
                       B00000000,
                       B00000000,
                       B00011000,
                       B00011000,
                       B00000000,
                       B00000000,
                       B00000000

                   };

  //To display the pattern
  for (int i = 0; i < 3; i++) {

    setBit(pattern1);
    delay(50);
    setBit(pattern2);
    delay(50);
    setBit(pattern3);
    delay(50);
    setBit(pattern4);
    delay(50);
    setBit(pattern4);
    delay(50);
    setBit(pattern3);
    delay(50);
    setBit(pattern2);
    delay(50);
    setBit(pattern1);
    delay(50);

  }

}

Credits

Electronics Champ
5 projects • 11 followers
Projects based on breadboard electronics and Arduino with clear step-by-step instructions, circuit diagrams, schematics, and source code.

Comments