D&D Dice HELP

Hey guys I really need help with this code It is set up to have the 6 different dice from D&D there is a potentiometer that is set to change the number of dice that are rolled at once (Ex. i want to roll 4d6 at one time) What do i need to add to make it so i can roll more then one die at a time? thanks for the help!!

Dice.ino (3.16 KB)

Fathertime41:
Hey guys I really need help with this code It is set up to have the 6 different dice from D&D there is a potentiometer that is set to change the number of dice that are rolled at once (Ex. i want to roll 4d6 at one time) What do i need to add to make it so i can roll more then one die at a time? thanks for the help!!

I have looked at the dice.ino sketch and have questions.

what is the model of the LCD display that you are using?
Do you have a paper and pencil wiring diagram that you can post?

What does "I want to roll 4d6 at one time" mean?

I am going to assume the "D&D" means Dungeons & Dragons.
I never played the game.

I am using the sparkfun 16,2 lcd display

i also do not have a diagram i can post at the moment

but roll 4d6 at one time i mean i would like to use the code to set the number of dice (4 in this example) then be able to select the sided dice (6 in this example) press the roll button and have it display either each individual die roll or a sum total of all of them. An example output i would love to be able to get is something like "You rolled 24" (All 4 die rolled 6) or "you rolled 6 6 6 6"

Does that make a little more sense?

Fathertime41:
Does that make a little more sense?

A little more sense perhaps. Could you read the forum guidelines please?

Paul

Fathertime41:

Does that make a little more sense?

Basically, I get the concept.
N == the number of die to be rolled
S == the number of sides to the die

roll N die of S sides and display the results.

Now, Would you please post the dice.ino properly to the forum.

also take pencil and paper and draw out the circuit diagram.
post a scan or photograph of pencil and paper drawing.

Hey guys I really need help with this code It is set up to have the 6 different dice from D&D there is a potentiometer that is set to change the number of dice that are rolled at once (Ex. i want to roll 4 six sided dice at one time) What do i need to add to make it so i can roll more then one die at a time? thanks for the help!!

#include <LiquidCrystal.h>

const int diceUpPin = 8;
const int rollPin = 9;
int pot = A0;
int die = 0;
int val = 0;

int theDice[] = {4, 6, 8, 10, 12, 20, 100};

int diceUpState = 0;
int rollState = 0;

int increment = 0;

int randomChance;

boolean rolled = false;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
pinMode(diceUpPin, INPUT);
pinMode(rollPin, INPUT);

intro();

}

void loop()
{
val = analogRead(pot);
die = map(val,0,1024,1,11);

diceUpState = digitalRead(diceUpPin);
rollState = digitalRead(rollPin);

rolled = false;

lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Using ");
lcd.print(die);
lcd.print(" D");
lcd.print(theDice[increment]);
delay(250);

if(diceUpState == HIGH)
{

increment++;

if(increment > 6)
{
increment = 0;
}
}

if(rollState == HIGH) /
{

randomSeed(analogRead(A1));
randomChance = random(1,theDice[increment] + 1);

rolled = true;
}

if(rolled)
{
lcd.setCursor(0,1);
lcd.print("You rolled a ");
lcd.print(randomChance);
delay(2000);
}

}

void intro()
{
lcd.begin(16, 2);
lcd.print(" Welcome to D&D");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" By: Andrew Coe");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Red button to");
lcd.setCursor(0,1);
lcd.print("switch dice");
delay(1750);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Green button to");
lcd.setCursor(0,1);
lcd.print("roll dice");
delay(1750);
}

Dice.ino (3.16 KB)

If you post your code as described in the "how to use this forum-please read" stickies, more people will be able to see it.

DON'T CROSS POST!!!!!!!!!!!!!!!!!!!!
http://forum.arduino.cc/index.php?topic=525812
I HAVE REPORTED THIS THREAD TO THE MODERATORS

Threads merged.

  val = analogRead(pot);
  die = map(val,0,1024,1,11);

Magic numbers suck. We can reasonably guess what 0 and 1024 mean. But, what the heck are 1 and 11 supposed to mean?

Why is val a global variable? Not a single other function gives a rat's ass what is in val.

Where, in your code, are you defining the number of dice to roll? Why doesn't a single variable name seem to show that?

Where, in your code, are you defining the number of sides on each die? Why does nothing in your code seem to show that?

How many times, in any given program, does randomSeed() need to be called? What does the documentation about that function say?

How would YOU go about rolling n dice? All at once? Or one at a time?

How many can the Arduino simulate rolling at one time? Any answer other than 1 is wrong, since "rolling a die" requires calling random(), if you care about the individual dice values.

If you only care about the total, then the upper limit for the call to random() depends on the number of dice to roll, which I can't determine from the sometimes cryptic names you use.

  if(rollState == HIGH)                                         /
  {

Random slashing is NOT a good idea.

Quite simple.

Delete rolled and all references to it.

Combine the if(rolled) into if(rollState==HIGH)

if(rollState == HIGH)                               
  {
    
    randomSeed(analogRead(A1));                                       
    randomChance = random(1,theDice[increment] + 1);
    lcd.setCursor(0,1);
    lcd.print("You rolled a ");
    lcd.print(randomChance);
    delay(2000);  
  }

Then add a loop within the new if statement running from 0 to die.

if(rollState == HIGH)                               
  {
 
    randomSeed(analogRead(A1));                                       
    for (x=0; x<die; x++)
     {
      randomChance = random(1,theDice[increment] + 1);
      lcd.setCursor(0,1);
      lcd.print("You rolled a ");
      lcd.print(randomChance);
      delay(2000);  
     }
  }

The problem is each result will display for only 2 seconds.

What would better is to clear the display and then list the values. That will take changing the position of the display cursor. Instead of lcd.setCursor(0,1) you will have variables for 0 and 1. If this is what you want, try it first and repost your code for additional help.

Repeatedly calling randomSeed is a bad idea. Calling it repeatedly with analogRead is a terrible idea...

...
if(rollState == HIGH)                               
  {
    
    randomSeed(analogRead(A1));                                       
...

If you time the user's inputs in micros and use the low 16 bits for your random seed, you might do better.

Does it need verbose prompts and labels?
Could it just show the dice numbers all spaced out and maybe scroll if too long to fit?

GoForSmoke:
If you time the user's inputs in micros and use the low 16 bits for your random seed, you might do better.

That can also be a very bad idea. (For a different reason.)

I guess that depends on how well a user can repeatedly match timings at the granular level. 65ms (16 bits of usecs) is just longer than 2 video frames. The whole interval of actual user button time will be many of those with the low 16 bits always being inside of one 65ms segment. How close within that 65ms window can a user repeat when every microsecond different counts? Not possible.

Back in the days of using the 1/18.2 sec keybd clock ticks, yeah, problem real easy. Back then I timed to (Unix) seconds where now I time to microseconds. It's one of the first things I picked up on with Arduino. Changing pins even faster took a bit longer.

Perhaps with millis timing it would be a mistake. It's not just 1000x coarser but the millis() timer skips 6 values in every low 8 bits (to make 1024 fit into 1000) whereas micros() does not.

GoForSmoke:
I guess that depends on how well a user can repeatedly match timings at the granular level.

No. It has to do with a flaw in way the Arduino library uses random number generators like Park-Miller and a flaw in the generator itself regarding low-value seeds.

Simplest answer is to not use the flawed Arduino RNG and take the low bits from user-initiated event micros times -as- the random value.

Using the micros timer lets us get at 30 bits of detail (low 2 of the 32 are always 0, next 6 to 14 are good) very quickly, on-events as opposed to getting 10 bits of undetermined analog requiring blocking the sketch for a solid 105ms per read.

GoForSmoke:
Simplest answer is to not use the flawed Arduino RNG...

Especially when this is thrown in... there are faster, smaller, better quality generators available.

Geiger counter and smoke alarm.

Time an event as UL micros then use bits 2 to 11 as pseudo-random 0 to 1023, which microsecond of the millisecond the event occurred in is not data a human can cause to repeat.