I am trying to make a digital dice for my project in school. I am having troubles with the programming, I don't really know how to write code and I am not a programmer.

I took inspiration from a post on the Arduino website (https://projecthub.arduino.cc/tylerpeppy/arduino-digital-dice-4d7e2e) where someone already made the project that I'm trying to make.

In the code I need it to roll or randomly generate numbers from 1 to 12. If possible I would like to make it so that it has different modes where if for example if I pressed the button twice it would change the mode to generate numbers from 1 to 6.


When I upload the code to my Arduino it says "Digital", then immediately switches to saying "Rolling" and it stays on the word Rolling and doesn't say anything else until I press the button (then the whole thing starts over again).

NOTE: the right half of the display is blank due to hardware failure

So my question is how do I fix it so that it gives me results of the dice roll?

Here are the schematics and some pictures; enter image description here enter image description here enter image description here

This the code that I used for the programing;

include <LiquidCrystal.h>
long randNumber;

int Led = 13; //define LED port
int Shock = 2; //define shock port
int val;//define digital variable val

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12 );

byte customChar[] = {

void setup()
  lcd.begin(16, 2);
  lcd.createChar(0, customChar);

  pinMode(Led, OUTPUT); //define LED as a output port


  pinMode(Shock, INPUT); //define shock sensor as a output port

  lcd.write(byte( 0));
  lcd.print("Digital dice");
  lcd.write(byte( 0));

void loop()
  val = digitalRead(Shock); //read the value of the digital interface 3 assigned to val
  if (val == LOW) //when the shock sensor have signal do the following
    lcd.print("Rolling dice...");

    lcd.setCursor(0, 0);

    randNumber = random(1,7);

    lcd.print("Dice 1 = ");
    lcd.setCursor(0, 1);

    randNumber = random(1,7);

    lcd.print("Dice 2 = ");

I appreciate anyone that wants to help in any way!

  • you're trying to see something that lasts only a few milliseconds ... think about why you can see Rolling
    – jsotola
    Commented May 5 at 17:41
  • ahh I see so the delay line in code is the one that tells you how long something is displayed?
    – Urban
    Commented May 5 at 17:45
  • in this program, yes ... the delay instruction actually stops the program ... that is why its use is not desired when the program should be responsive to inputs
    – jsotola
    Commented May 5 at 17:59
  • the microcontroller is a very stupid machine, it can only do exactly what you tell it to do ... that is why you have to be aware what you are telling it to do at all times
    – jsotola
    Commented May 5 at 18:01
  • 1
    I hate when people say that delay "stops the program". That leads to the types of misconceptions that make understanding non-blocking code harder. The delay function doesn't "stop" the program. It IS the program. The program is running line by line just like any program does. And each line does its own thing one at a time. It's just that the delay line takes a really long time to finish and move to the next line. The program is running. It's just running that one line for a long time.
    – Delta_G
    Commented May 5 at 18:55

1 Answer 1


A delay() call favors one part of the sketch, or one part of it at a time, keeping others from executing. Generally we're trying to keep several rather slow (typically external) things happening at once, or at least tested often enough to service them as quickly as they need to be. To do that, we have to test whether each of those things needs to be started/stopped/adjusted, and do that really often and be really quick about it, until/unless we find one that needs some attention. Even then, we do whatever it needs, quickly!, and get back to testing the rest of our things.
A 'thing', in this case, means turning an LED on or off, writing a message or blanking an LCD display, starting or stopping a motor, etc.

Writing your sketch that way is called non-blocking programming. We don't let the processor be idle while some external thing is happening; we come back and check it again later (maybe only a few milliseconds later, but later).

Have a look at this earlier article of mine about non-blocking programming, then come back and read this answer again. That should give you a pretty good idea of how your sketch will need to work to do what you want it to do, and how to write one that does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.