Shopping Cart
Cool Arduino Projects Arduino Dice Simulation

Cool Arduino Projects: Arduino Dice Simulation

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

 

Cool Arduino Projects is a new tutorial series we’ve started on the Learn Robotics blog. The goal is to show you how to create application-based projects using Arduino.  If you’re a teacher looking for a STEM project, you can use the full lesson plans in your class.

The Application: Ties to Math & Comp Sci

For today’s lesson, we’re going to create an Arduino dice. We’ll focus on wiring up LED’s and a push button as well as simple logic to “roll the dice.” In the next part of the lesson, we’ll connect an LCD and display information to our user. This project ties in nicely with topics in computer science, math/probability, and game design. Let’s get started!

Cool Arduino Projects Arduino Dice Simulation

Create a Prototype Arduino Dice

First, we’ll wire up the prototype circuit. You’ll need a breadboard, six LED’s, six 220Ω resistors, an assortment of jumper wires, an Arduino Uno, a push button, one 10KΩ resistor, and a USB printer cable. I recommend this Arduino kit because it includes a lot of components for the price.

Here’s the wiring diagram for the circuit:

Cool Arduino Projects Arduino Dice Simulation

You can also use the simulated model on Tinkercad. This is a great way for each student to complete the lesson without the need for 1:1 kits.

Connect the LED’s

Cool Arduino Projects Arduino Dice SimulationTo start off, connect the LED’s to the breadboard. Remember that LED’s are polarized, so be sure to plug the positive (curved) side into a resistor and the negative (flat) side into ground. Each LED will be connected to a digital pin on the Arduino. In the wiring diagram, we’ve used digital pins 3-8 for LED’s.

Wire up the Push Button

Next, connect the button to the breadboard. One leg is connected to the 10KΩ resistor and to ground. Another leg will be connected to 5V, and a third leg will be connected to digital pin 12 on the Arduino.

Power the Circuit

Finally, assemble the power rails. Connect both power rails of the breadboard to each other. Then run jumper wires from 5V on the Arduino to the red power rail and GND on the Arduino to the black power rail. We’ll power the circuit using the USB printer cable and a USB port on our computer. Verify that the components are correctly wired before moving to the next step.

Cool Arduino Projects Arduino Dice Simulation

Program the Arduino for the Simulated Dice

Now, we’ve reached the fun part! Let’s program the Arduino to simulate a dice roll. You’ll need to have a copy of the Arduino IDE downloaded on your computer.

If you’re teaching this lesson as part of a class, students can create a “flowchart” or sequence of events outlining their code.

The Setup() method

The setup() method runs once on start-up. Define all the LED pins as OUTPUTS inside this method. Then initialize the Serial Monitor using the command Serial.begin(9600);

 

Your code should look something like this:

int button = 12;
//led's are on pins 3-8

void setup()
{
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(button, INPUT);

    Serial.begin (9600);
    Serial.println("Welcome to Arduino Dice");
    Serial.println("Press the button to roll a number");
}

You’ll notice I’ve included a couple of print statements to show the user we’ve initialized the dice. This can be accessed in the Serial Monitor of the Arduino IDE.

Code to “roll a dice”

To simulate a dice roll, we’ll use the built-in Arduino method, random().

First, we’ll create our random number, then we’ll use that number to illuminate the correct pattern of LED’s. Here’s an example of the code you can use.

Next, we’ll call the displayNum() method in loop(). For now, we’ll run this method every 5 seconds (5000 milliseconds). Then, we’ll trigger the dice roll using a button. Verify that this works before moving to the next step.

Trigger Dice Roll with a Button

Congrats on making it this far! Now, let’s get the simulated dice to roll on a button press. In other words, when a user presses the button, the displayNum() method is triggered.

We’ll use a concept called “debouncing.” Push buttons often generate open/close states in a short amount of time. This is because of mechanical and wiring limitations. Therefore, in software we can account for this by checking twice in a short period. The code will ensure that the push button is definitely pressed.

Cool Arduino Projects Arduino Dice Simulation

Let’s modify the demo debouncing code to trigger the displayNum() method. I’ve created another method called rollDice() to store this code. Here’s what it looks like:

//additional global variables
int buttonState;           
int lastButtonState = LOW;    
unsigned long lastDebounceTime = 0;   
unsigned long debounceDelay = 50;
 
void rollDice(){
  int reading = digitalRead(button);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        displayNum();
        delay(1000);
      }
    }
  }
  lastButtonState = reading;
}

When you’re done, test it out by calling rollDice() in loop().

Further Learning

And that’s a wrap! I hope you enjoyed creating this Cool Arduino project! In the next lesson, we’ll add an LCD to display user information and probability calculations. If you enjoyed this project, be sure to leave a comment below.

You can also support Learn Robotics by downloading the full lesson plans including full code download, worksheets, PowerPoint slides, and rubrics here.

 

Support Content Like This

You May Also Like

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying a coffee.
If you'd like to work with Liz, you can schedule a call here.

2 comments

  1. You should come up with an LCD version to play it at a board game. I personally will be working on this. but as a beginner I may not. Wish me Luck!!!!

    1. Liz Miller

      Simon, yes that’s a good idea. Here’s an LCD tutorial that’ll help you get set up. The wiring is to a sensor shield, but you can go directly to the pins on your Arduino. The software configuration should be the same. Print the “random” code to the LCD instead of triggering LEDs. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Learn Robotics Botly Favicon

Become a Learn Robotics Insider

 

Be the first to know when we launch new robotics content. Plus gain FREE robotics tips to your inbox.

Awesome! Check your inbox for a surprise!

Remote Robotics Courses for High School, Middle School, or Pre-College Training

Join the Powered by Learn Robotics Program

Teach robotics anywhere with our turnkey robotics program!

Download our free program brochure!

Success! Check your email for the download!