Monday, June 8, 2015

Arduino Controlled Electronic Dice

Hi guys! Today I would like to share another fun project with you.An electronic dice.With this you don't need any real boring dice anymore.Unfortunately I don't have pictures of my circuit because I did it a time ago.But I have a video :) First here is a video of my dice.



Well I have to accept I have a bad luck :(
So lets gather the parts
-Arduino Uno
-Breadboard
-7xLed 
-7x470 Ohm Resistor
-1x10k Resistor
-Momentary Button
-Jumper Wires
This will be the layout of your leds and pin numbers.

And here is our diagram.


After preparing the circuit upload this code to your arduino.


Arduino Code
---------------------------


const int ledPins[] = { 2 ,3, 4, 5, 6, 7, 8};
const int ButtonPin = 9;
void setup() {
 pinMode(ledPins[0], OUTPUT);
 pinMode(ledPins[1], OUTPUT);
 pinMode(ledPins[2], OUTPUT);
 pinMode(ledPins[3], OUTPUT);
 pinMode(ledPins[4], OUTPUT);
 pinMode(ledPins[5], OUTPUT);
 pinMode(ledPins[6], OUTPUT);
 pinMode(ButtonPin,INPUT);
}

void loop() {
  if(ReadButton(ButtonPin) == true)
  {
    RollDice();
    }
    delay(1);
}

void ShowDice(int number)
{
  int i;
  for(i = 0; i<7; i++)
  {
    digitalWrite(ledPins[i],LOW);
    }
    switch(number)
    {
      case 1:
       digitalWrite(ledPins[6],HIGH);
       break;
      case 2:
       digitalWrite(ledPins[1],HIGH);         
       digitalWrite(ledPins[4],HIGH);
       break;
      case 3:
       digitalWrite(ledPins[1],HIGH);
       digitalWrite(ledPins[6],HIGH);
       digitalWrite(ledPins[4],HIGH);
       break;
      case 4:
       digitalWrite(ledPins[0],HIGH);
       digitalWrite(ledPins[2],HIGH);
       digitalWrite(ledPins[3],HIGH);
       digitalWrite(ledPins[5],HIGH);
       break; 
      case 5:
       digitalWrite(ledPins[2],HIGH);
       digitalWrite(ledPins[3],HIGH);
       digitalWrite(ledPins[5],HIGH);
       digitalWrite(ledPins[6],HIGH);
       digitalWrite(ledPins[0],HIGH);
       break; 
      case 6: 
       digitalWrite(ledPins[0],HIGH);
       digitalWrite(ledPins[1],HIGH);
       digitalWrite(ledPins[2],HIGH);
       digitalWrite(ledPins[3],HIGH);
       digitalWrite(ledPins[4],HIGH);
       digitalWrite(ledPins[5],HIGH);
       break; 
       }
      }
      void RollDice()
      {
        int dice;
        int i;
        randomSeed(analogRead(0));
        dice = random(1,7);
     for(i=0; i<250; i=i+50)
     {
       ShowDice(1);
       delay(i);
       ShowDice(2);
       delay(i);
       ShowDice(3);
       delay(i);
       ShowDice(4);
       delay(i);
       ShowDice(5);
       delay(i);
       ShowDice(6);
       delay(i);
       }
       ShowDice(dice);
       }
      boolean ReadButton(int ButtonPin)
      {
        int okunan = digitalRead(ButtonPin);
        delay(25);
        if ( okunan == digitalRead(ButtonPin))
        {
          return okunan;
          }
          else
          {
            return false;
            }
            }


---------Code Ends Here---------


Now you can enjoy with your dice.By the way it's a fair dice. You can use it while playing backgammon if you don't trust your friend :D or you guys can play Risk and ruin your friendship like us :(. Please write comments about project and the blog I will be happy to improve my blog and tutorials.



















No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...