Home

Guide no. 45

Display, button and random numbers: press it and the dice rolls on the screen, slows down and stops. A project that brings the previous guides together.

By Ada Intelisano · informaticait.it technical team

Download the PDF (A4, 4 pages) Free to print and share, without changes — CC BY-ND 4.0
In short
  • The display from guide no. 44 plus a button on pin 2.
  • random(1, 7) gives a number from 1 to 6.
  • randomSeed(analogRead(A0)): different rolls at every power-up.
  • A for loop with longer and longer pauses makes the dice slow down.
Time: 30 minutesLevel: day eightBefore: guides no. 35 and 44

Your first project to actually use: a dice for board games. Press the button, the numbers spin fast, slow down and stop on one from 1 to 6. We put together the button from guide no. 35 and the display from no. 44.

1Get the parts ready

  • the 16×2 LCD display with I²C module from guide no. 44, already working
  • a pushbutton, the breadboard and four more jumper wires

2Build the circuit

Arduino UNO, LCD and buttonElectronic diceRoll:    4116GNDVCCSDASCLSCLSDAAREFGND131211109876543210IOREFRESET3.3V5VGNDGNDVINDIGITAL (PWM ~)POWERANALOG INUNO−+2buttonGND5VA4A516×2 display with I²C backpackinformaticait.it

The display has the same wires as in guide no. 44, with one difference: now GND has two jobs. Take the board's GND to the − rail of the breadboard, and from there run one wire to the display GND and a short jumper to one leg of the button. The other leg of the button goes to pin 2 (green wire).

3Write and upload the sketch

// Electronic dice: press the button, the numbers spin and stop on one from 1 to 6.
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp lcd;
const int BUTTON = 2;        // with the ESP32 write 27

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  randomSeed(analogRead(A0));          // A0 left free: a different "noise" at every power-up
  lcd.begin(16, 2);
  lcd.print("Electronic dice");
  lcd.setCursor(0, 1);
  lcd.print("Press the button");
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {    // you pressed it
    int number = 1;
    for (int roll = 0; roll < 15; roll++) {   // 15 numbers, slower and slower
      number = random(1, 7);                   // from 1 to 6 (7 is excluded)
      lcd.setCursor(0, 1);
      lcd.print("Roll:    ");
      lcd.print(number);
      lcd.print("       ");
      delay(40 + roll * 15);
    }
    while (digitalRead(BUTTON) == LOW) {}      // wait until you let go of the button
  }
}
New word: randomrandom(1, 7) picks a random number from 1 to 6: the second number is excluded, which is why you write 7.
New word: randomSeedArduino can't really draw lots: it produces a series of numbers that look random, but it is the same series at every power-up. randomSeed(analogRead(A0)) makes it start from a different point by reading a free pin where the value changes by itself.

4How the dice rolls

The for loop repeats 15 times: it picks a number, writes it, waits. The wait is 40 + roll * 15 thousandths of a second: 40 on the first pass, 250 on the last. That is why the numbers slow down like a real dice coming to a stop. The last number written is the result.

New word: forfor (int roll = 0; roll < 15; roll++): start from 0 and add 1 at each pass as long as roll is less than 15. The lines between the curly braces run 15 times.
✓ What you should seeOn the display “Electronic dice” and “Press the button”. Press it: the numbers spin, slow down and stop. Every roll is different.
If it doesn't work
  • Blank display: guide no. 44, step 4 (contrast and connections).
  • Nothing happens: is the green wire on pin 2? Does the button straddle the center groove?
  • It keeps rolling by itself: the button is turned the wrong way. Rotate it a quarter turn.

5Challenge yourself

Try it yourself
  • Two dice: write two numbers on the same row, and their sum too.
  • Add the buzzer from guide no. 38: a “tick” for every number that spins by.
  • Count how many times you get a 6: out of 60 rolls, about 10?
✓ What you learned
  • random(min, max) picks a number, with the maximum excluded;
  • randomSeed stops the rolls from repeating the same way at every power-up;
  • for repeats a piece of the sketch an exact number of times.

With the ESP32 (button on GPIO 27) the 5 V display needs a level shifter: guide no. 44.

Sources: Arduino, language reference: random(), randomSeed(), for, pinMode() with INPUT_PULLUP · hd44780 library by Bill Perry · informaticait guides no. 35, 38 and 44

Useful products

Other guides

All our guides →
Loading...
Back to top