(not a ) switch case question (anymore...)

PeterH:
This bit of code does things in the wrong order:

      state++;

{
       lcd.begin(16,2);
       lcd.print("DigiDice - D");
       lcd.print(dValue[state]);
     }
   }
   if (state > 6)




You print the new dice value before you do the 'if(state > 6)' check to reset it to zero. The '1' you're seeing on the display is just whatever happens to be in the memory location after the last dice.

A better way to do this would be like this:



// DICE_COUNT is the number of elements in the dValue array
     state = (state+1) % DICE_COUNT;
     lcd.begin(16,2);
     lcd.print("DigiDice - D");
     lcd.print(dValue[state]);

you are 100% correct sir!
while you posted that, I had accidentally stumbled on the answer myself, but hadn't realized it yet, or why.
I am very nearly successful at my next stage of complexity with this project.
here is what I am doing with it now....

There is a push button to select through the range of dice modes.
there is a toggle switch to select rolling one dice or two dice at a time. (both dice must be of the same value though, ex - two d6.)
I can get the LCD to display the correct dice mode all through the count now, and I can get two different results with one shake of the dice.

my only problem now is the LCD seems to be trying to display both modes at the same time, and it makes the words flicker like mad, and is a bit hard to read everything.
I am using a switch case at the moment to handle switching between one and two dice per throw. I originally wrote it as an if/else, but changed it to a switch to see if that would cure my screen flicker. It seems to have helped a bit, but it is still very much an issue.
I am working my way through the LCD examples now trying to find out how to change the number of dice, and print two completely different screen readouts, without the flicker
if that makes any sense...

here is the code as I have it wrote now...

#include <LiquidCrystal.h>

LiquidCrystal lcd(2,3,4,5,6,7);

const int nButton = 11;
const int mButton = 9;
int mode = 0;
int oldMode = 0;
const int Tilt = 8;
int val = 0;
int oldVal = 0;
int state = 0;
int number = 0;

int dValue[] =
{2,4,6,8,10,12,20};

long result;
long resultA;
long resultB;


void setup()
{
  pinMode(Tilt, INPUT);
  pinMode(mButton, INPUT);
  pinMode(nButton, INPUT);
  
  randomSeed(analogRead(0));
}

void loop()
{
  mode = digitalRead(mButton);
  if ((mode == HIGH) && (oldMode == LOW))
  {
    state++;
  }
  if (state > 6)
  {
    state = 0;
    delay(10);
  }
  oldMode = mode;
  {
    number = digitalRead(nButton);
    switch(number)
    {
      case 0:
      {
        {
          lcd.begin(16,2);
          lcd.print("DigiDice - D");
          lcd.print(dValue[state]);
        }
        val = digitalRead(Tilt);
        if ((val == HIGH) && (oldVal == LOW))
        {
          result = random(1, dValue[state] + 1);
          lcd.setCursor(0, 1);
          lcd.print("Result = ");
          lcd.print(result);
          lcd.print("       ");
          delay(3000);
        } else
        {
          lcd.setCursor(0, 1);
          lcd.print("Give Me A Shake!");
        }
      }
      break;
      case 1:
      {
        {
          lcd.begin(16,2);
          lcd.print("Roll Two - D");
          lcd.print(dValue[state]);
        }
        val = digitalRead(Tilt);
        if ((val == HIGH) && (oldVal == LOW))
          {
            resultA = random(1, dValue[state] + 1);
            resultB = random(1, dValue[state] + 1);
            lcd.setCursor(0,1);
            lcd.print("R1 = ");
            lcd.print(resultA);
            lcd.print("   R2 = ");
            lcd.print(resultB);
            lcd.print("     ");
            delay(3000);
          } else
          {
            lcd.setCursor(0, 1);
            lcd.print("C'mon...Shake Me!!");
          }
        }
        break;
      }
    }
}

I seem to be off on my curly brackets somewhere, because my indenting is messed up a bit, but I will fix that when I have the whole code in working order.

off to keep researching, but will be happy to hear any suggestions to stabilize my display :slight_smile:

ETA: I should mention that when you shake the "dice", the display does stabilize, and very clearly prints everything it is supposed to.
This makes me think I should have the mode selecting code in both cases, like the Tilt code, and maybe that will work it out.
seems very redundant to me, but this is how we learn :wink:

Again, very happy to take suggestion improvements and general criticism on the project!
be brutal if need be :slight_smile: