making Digital Dice using Arduino for Kniffel game
Description:
Digital Dice using Arduino- in this article, I am going to show you how to make a simple digital dice for kniffel game
Digital Dice using Arduino:
In the last articles, you already have some programming basics of the Arduino board. You will of course suspect or hope that this cannot be all, and therefore we will be based on a couple of interesting circuits that apply, deepen and expand our knowledge. It is always exciting to build an Arduino digital dice. A few years ago, when microprocessors didn’t exist or were unaffordable, you have the circuit with several integrated circuits, also called ICs. On the Internet, there are countless handicraft instructions for this. We want the digital dice alone with the Arduino board head. Everyone knows at least one game of dice, be it Kniffel. We want digital dice with our next circuit realized. It consists of a display unit made up of 7 LEDs and a button that starts the dice is composed.
First of all, I’ll show you the arrangement of the LEDs that make up the Points of a real dice is modeled, with the individual points are numbered so that we can the overview later when controlling the individual LEDs keep. The led 1 is in the top left corner and the numbering is then down and then after right until you finally come to led 7 on the far right ends below.
Digital Dice numbers pattern:
For number one all LEDs will be low except led number4
For number two led1 and led7 will be high.
For number three led2, led4 and led7 will be high.
For number four led1, led3,led5, and led7 will be high.
For number five led1, led3, led4,led5, and led7 will be high.
For number six led1, led2,led3,led5, led6, and led7 will be high.
Components Required for Digital Dice:
For this example, we need the following components:
- Seven red LEDs
- Seven 330 ohm resistors
- One 10k resistor
- One button
- Jumper wires
Amazon purchase links:
digital dice Circuit diagram:
The circuit diagram shows us the 7 dice LEDs with their 330 ohms Series resistors and the dice button with its pull-down Resistance.
Digital dice Arduino program:
Here is the sketch code for controlling the Arduino digital dice:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | int digits[6][7] = {{0, 0, 0, 1, 0, 0, 0}, // dice number 1 binary code {1, 0, 0, 0, 0, 0, 1}, // dice number 2 binary code {1, 0, 0, 1, 0, 0, 1}, // dice number 3 binary code {1, 0, 1, 0, 1, 0, 1}, // dice number 4 binary code {1, 0, 1, 1, 1, 0, 1}, // dice number 5 binary code {1, 1, 1, 0, 1, 1, 1}}; // dice number 6 binary code int Ledpin[] = {2, 3, 4, 5, 6, 7, 8}; int pinOffset = 2; int ButtonPin = 13; // button is connected with pin 13 void setup(){ for(int i = 0; i < 7; i++) pinMode(Ledpin[i], OUTPUT); pinMode(ButtonPin, INPUT); } void loop(){ if(digitalRead(ButtonPin) == HIGH) GenrateDigits(random(1, 7)); //Generate a random number between 1 and 6 } void GenrateDigits(int value){ for(int i = 0; i < 7; i++) digitalWrite(i + pinOffset, (digits[value - 1][i] == 1)?HIGH:LOW); delay(20); } |
Program explanation:
1 2 3 4 5 6 7 8 9 10 11 | int digits[6][7] = {{0, 0, 0, 1, 0, 0, 0}, // dice number 1 binary code {1, 0, 0, 0, 0, 0, 1}, // dice number 2 binary code {1, 0, 0, 1, 0, 0, 1}, // dice number 3 binary code {1, 0, 1, 0, 1, 0, 1}, // dice number 4 binary code {1, 0, 1, 1, 1, 0, 1}, // dice number 5 binary code {1, 1, 1, 0, 1, 1, 1}}; // dice number 6 binary code |
The first value [6] in the pair of square brackets indicates the number of Rows, the second [7] that of the columns. Access to an item is also done using the double pair of brackets:
digits [row] [column]
The setup function takes over the task again the initialization of the individual pins:
1 2 3 4 5 6 7 8 9 | void setup(){ for(int i = 0; i < 7; i++) pinMode(Ledpin[i], OUTPUT); pinMode(ButtonPin, INPUT); } |
The pins for controlling the LEDs were also put into an array which is programmed as OUTPUT in the setup function. Only the button that is connected to a digital input a normal variable is assigned. The  main task is again taken over by the loop function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | void loop(){ if(digitalRead(ButtonPin) == HIGH) GenrateDigits(random(1, 7));Â //Generate a random number between 1 and 6 } void GenrateDigits(int value){ for(int i = 0; i < 7; i++) digitalWrite(i + pinOffset, (digits[value - 1][i] == 1)?HIGH:LOW); delay(20); } |
When the button has been pressed, the function GenrateDigits will be called. It takes a random value between 1 and 6 passed. The working of the function, we should a little inspect. It actually only consists of a for loop() that controls the individual LEDs for a number that is rolled. Let’s assume that a 4 was rolled, with the Function this value is supplied as an argument. Now begins for loop with their work. It controls the pins and determines the required HIGH / LOW level for the respective LED:
1 2 3 | for(int i = 0; i < 7; i++) digitalWrite(i + pinOffset, (digits[value - 1][i] == 1)?HIGH:LOW); |