I am trying to make a digital die for my project at school. I am having trouble 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 for example if I pressed the button twice it would change the mode to generate numbers from 1 to 6.
THE PROBLEM I'M HAVING:
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[] = {
B00000,
B00000,
B11111,
B11001,
B10101,
B10011,
B11111,
B00000
};
void setup()
{
lcd.begin(16, 2);
lcd.createChar(0, customChar);
lcd.home();
pinMode(Led, OUTPUT); //define LED as a output port
randomSeed(analogRead(0));
pinMode(Shock, INPUT); //define shock sensor as a output port
lcd.write(byte( 0));
lcd.print("Digital dice");
lcd.write(byte( 0));
delay(1000);
}
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.clear();
lcd.print("Rolling dice...");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
randNumber = random(1,7);
lcd.print("Dice 1 = ");
lcd.print(randNumber);
lcd.setCursor(0, 1);
randNumber = random(1,7);
lcd.print("Dice 2 = ");
lcd.print(randNumber);
}
delay(150);
}
I appreciate anyone that wants to help in any way!
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.
Rolling
delay
line in code is the one that tells you how long something is displayed?delay
instruction actually stops the program ... that is why its use is not desired when the program should be responsive to inputs