I am very new to the arduino world and am struggling to recover that one high school programing class I took 8 years ago.
I'm trying to write a program to help roll dice for a tabletop game named 'shadowrun' and then display the results on some LED's. It is supposed to read the number of dice the user is supposed to roll and then roll that many six sided die. The program works great for single digit numbers, but when I enter 11, for instance, it rolls two sets of one dice each. or for 13 it rolls one dice then a set of three.
The program does some extra stuff specific to 'shadowrun' rules which you can ignore. If someone has an easy solution or just wants to provide some handy tips on my code let me know. My only request is to keep it simple I'm still pretty green.
Here is my code
int diecount = 0;
int hitcount = 0;
int glitchcount = 0;
int dienumber = 0;
void setup ()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
randomSeed(analogRead(0));
Serial.println("Number of dice:");
}
void loop ()
{
if(Serial.available() > 0)
{
int pin = 13;
for(int i = 0; i < 9; i++)
{
digitalWrite(pin, LOW);
pin--;
}
diecount = Serial.read() - '0';
for(int i = 0; i < diecount; i++)
{
dienumber = random(1,7);
Serial.println(dienumber);
if(dienumber >= 5)
{
hitcount++;
}
if(dienumber <= 1)
{
glitchcount++;
}
}
Serial.println("-----------------------");
Serial.print("Hits: ");
Serial.println(hitcount);
Serial.print("Glitches: ");
Serial.println(glitchcount);
Serial.println("~~~~~~~~~~~~~~~~~~~~~~~~");
pin = 13;
for(int i = 0; i < hitcount; i++)
{
digitalWrite(pin, HIGH);
pin--;
delay(200);
}
if(glitchcount > hitcount)
{
digitalWrite(5, HIGH);
}
hitcount = 0;
glitchcount = 0;
Serial.println("Number of dice:");
}
}