Complete Guide How to Make Arduino Based Electronic Dice

Creating an Electronic Die

In this project, we’ll Complete Guide How to Make Arduino Based Electronic Dice step by step complete process. but you have to need to know about Random Numbers.


≡ How to Generating Random Numbers


To generate random numbers can be really valuable in games and effects. For example, you can utilize random numbers to play a dice or lottery game utilizing Arduino, to generate lighting effects with LEDs, or to generate visual or auditory effects for a quiz game utilizing Arduino.

The Arduino can’t take a purely random number by itself. You have to support it by providing a seed, an arbitrary starting number utilized in the calculations to generate a random number.

How to Using Ambient Current to Generate a Random Number

The simplest method to generate a random number Using Arduino is to write a program that reads the voltage from a free  analog pin with this line in void setup():

randomSeed(analogRead(0));

if nothing is wired to an analog input on the Arduino, static electricity in the environment builds a tiny, measurable voltage. The value of this voltage is quite random. We can utilize this measure of ambient voltage as our seed to generate a random number and then allot it to an integer variable utilizing the random(lower, upper) function.

We can utilize the parameters lower and upper to establish the lower and upper limits of the range for the random number. For example, to generate a random number within 100 and 1,000, you would utilize the following:

int a = 0;
 a = random(100, 1001);

We’ve utilized the number 1,001 first than 1,000 because the 1,001 upper limit is exclusive, meaning it’s not included in the range.

That said, to generate a random number within 0 and some number, you can simply enter the upper limit. Here’s how you would generate a random number within 0 and 6:

a = random(7);

The example sketch in Example 1 would generate random numbers within 0 and 1,000, as well as numbers within 10 and 50:


≡  Random number generator


int r = 0;

void setup()
{
  randomSeed(analogRead(0));
  Serial.begin(9600);
}

void loop()
{
  Serial.print("Random number within zero and 1000 is: ");
  r = random(0, 1001);
  Serial.println(r);
  Serial.print("Random number within ten and fifty is: ");
  r = random(10, 51);
  Serial.println(r);
  delay(1000);
}
Random Number
Random Number

Now that you understand how to generate random numbers, let’s put that knowledge to good utilization by building an electronic dice.


≡ Arduino Based Electronic Dice


Our plan is to light one of six LEDs randomly to mimic the throw of a dice. We’ll take a random number within 1 and 6, and then turn on the corresponding LED to indicate the result. We’ll build a function to select one of six LEDs on the Arduino randomly and to keep the LED on for a certain period of time.

When the Arduino running the sketch turned on or reset, it should rapidly show random LEDs for a specified period of time and then slowly slow until the final LED is lit. The LED matching the resulting randomly chosen number will stay on until the Arduino reset or turned off.


≡ Hardware


Creating an Arduino Based Electronic Dice project, you have to need this component :

  1.  LEDs of any color (LED1 to LED6)
  2. 560 Ω resistor (R1)
  3. Various connecting wires
  4. ed breadboard
  5. Arduino with USB cable

≡ Schematic


one LED will be lit at a time, a single current-limiting resistor can go within the cathodes of the LEDs and GND. Figure 2 shows the schematic for our dice.

Creating an Electronic Die
Figure 2 Creating an Electronic Die

≡ Sketch


void setup()
  {
    randomSeed(analogRead(0));      // seed the random number generator
    for ( int z = 1 ; z < 7 ; z++ ) // LEDs on pins 1-6 are output
    {
      pinMode(z, OUTPUT);
    }
  }

  void randomLED(int del)
  {
    int r;
    r = random(1, 7);      // get a random number from 1 to 6
    digitalWrite(r, HIGH); // output to the matching LED on digital pin 1-6
    if (del > 0)
    {
1     delay(del);            // hold the LED on for the delay received
    }
2     else if (del == 0)
    {
      do                   // the delay entered was zero, hold the LED on forever
      {}
3     while (1);
    }
    digitalWrite(r, LOW);  // turn off the LED
  }

  void loop()
  {
    int a;
    // cycle the LEDs around for effect
    for ( a = 0 ; a < 100 ; a++ )
    {
      randomLED(50);
    }
    // slow down
4   for ( a = 1 ; a <= 10 ; a++ )
    {
      randomLED(a * 100);
    }
    // and stop at the final random number and LED
    randomLED(0);
  }

 we utilize a loop in void setup() to activate the digital output pins. The function randomLED() takes an integer that is utilized in the delay() function at 1 to hold the LED turned on for the selected time. If the value of the delay taken at 2 is , then the function keeps the LED turned on regularly because we utilize

do {} while (1);

at 3, which loops forever, because 1 is always 1.

To “roll the dice,” we reset the Arduino to restart the sketch. To build a decreasingly slow change in the LEDs before the final value is presented, at 4 we first display random LED 100 times for 50 milliseconds per time.

we slow it down by increasing the delay within LED flashes from 100 to 1,000 milliseconds, with each flash lasting 100 milliseconds. The purpose of this is to simulate the “slowing down” of dice before it finally settles on a value, at which point the Arduino displays the result of the roll by holding one LED lit with this last line:

randomLED(0);

All Arduino tutorial available Click here

 ARDUINO TUTORIAL 

 31 total views,  1 views today