Electronic Dice Using Arduino

Description

It is Electronic Dice Game Using Arduino. It is basic arduino learning project that shows how we access arduino pins for writing. Like in basic arduino LED flasher

Circuit Diagram

In circuit diagram 7 LEDs are connected each with 220 Ohm resistor in series to arduino.

Detail

It is a game in which we press button connected to pin 6 of arduino. When button is pressed different patterns of dice game are produces in fast speed. And when we release this button a random pattern is selected and shown on LEDs. This is simple example in which we generate random number from 1 to 6 range and display this number on LEDs.

Software

Arduino program is simple but lengthy. It first set pin mode for button/key as input with internal pullup resistor enabled and for LEDs as outputs. Initially displays 1 on LEDs. Calls srand() function for random number generation. This function is called once. Then in loop() function when key is pressed it generates random numbers from 1 to 6 range and display on LEDs. When key is released random number generation is stopped and final number is displayed on LEDs. Here is program


#include <stdlib.h>

// Define macro or formulae for random number
// generation from 1 to 6.
#define GetRandomNumber() ((rand() % 6) + 1)

void Display(char Value);
void turnOffAllLeds();

const int Switch = 6;

const int Led1 = 7;
const int Led2 = 8;
const int Led3 = 9;
const int Led4 = 10;
const int Led5 = 11;
const int Led6 = 12;
const int Led7 = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(Switch, INPUT_PULLUP);
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  pinMode(Led4, OUTPUT);
  pinMode(Led5, OUTPUT);
  pinMode(Led6, OUTPUT);
  pinMode(Led7, OUTPUT);

  // Turn off all Leds.
  //turnOffAllLeds();
  Display(1);
  srand(50);
}

void loop() {
  char randomNumber;
  // put your main code here, to run repeatedly:
  while(digitalRead(Switch));
  while(!digitalRead(Switch))
  {
    // Generate random number.
    randomNumber = GetRandomNumber();

    // Display this random number
    // on Leds.
    Display(randomNumber);
    // Give some delay.
    delay(10);
  }
    // Generate random number last time.
    randomNumber = GetRandomNumber();
    // Finally display this number on LEDs.
    Display(randomNumber);
}

void turnOffAllLeds()
{
  digitalWrite(Led1, HIGH);
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, HIGH);
  digitalWrite(Led4, HIGH);
  digitalWrite(Led5, HIGH);
  digitalWrite(Led6, HIGH);
  digitalWrite(Led7, HIGH);
}

// This function displays an integer value from
// 0 to 6 on LEDs.
void Display(char Value)
{
  // Switch off all LEDs.
  //Led1 = Led2 = Led3 = Led4 = Led5 = Led6 = Led7 = 1;
  turnOffAllLeds();
  switch(Value)
  {
  case 1:
    digitalWrite(Led4, LOW);
    break;
  case 2:
    digitalWrite(Led1, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 3:
    digitalWrite(Led1, LOW);
    digitalWrite(Led4, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 4:
    digitalWrite(Led1, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 5:
    digitalWrite(Led1, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led4, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 6:
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led6, LOW);
    digitalWrite(Led7, LOW);
    break;
  }
}