Go Down

Topic: (not a ) switch case question (anymore...) (Read 3 times) previous topic - next topic

Neight

guess I am not doing as well as I hoped...

Tried rearranging the code to take out some elements I thought were redundant (like having the mode select code in both cases.)
now, the dice values displayed are not right again.
The roll results are still good, and the rate they cycle through the dice modes when you push the button is still correct also.
now, when you turn it on, it shows you are in D2 mode, which is correct, from there it goes through D4, D6, D8, D10, D12, then D20 which is also right. 
Here is where it gets odd.
it hangs on 20 for two pushes, then goes to 40, 60, 80, 10, 12, 20, 20, 40, 60.... on like that.
after the first time though, it starts displaying the modes in tens, instead of single and double digits.
on the second pass, the dice modes are still right, but the name for them is wrong.
once again, I cannot see where the code is making the error, so without further ado...

Code: [Select]
#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);
 
  lcd.begin(16,2);
 
  randomSeed(analogRead(0));
}

void loop()
{
  {
    mode = digitalRead(mButton);
    if ((mode == HIGH) && (oldMode == LOW))
    {
      state++;
    }
    if (state >= 7)
    {
      state = 0;
      delay(10);
    } oldMode = mode;
  }
  {
    number = digitalRead(nButton);
    switch(number)
    {
      case 0:
      {
        {
          lcd.setCursor(0,0);
          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.setCursor(0,0);
          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;
    }
  }
}


This is where I have it at now.
The mode select part of the code seems to be working right, and I have wrote it several different ways, to test it. 
The code to show the dice value isn't something I changed, and it went from working fine to not.  Don't know what happened.
any help would be awesome.
it is possible I just need fresh eyes on it, and the problem is fairly obvious, but for right now, I am stumped.
absence of proof is not proof of absence

PeterH


thinking about an enclosure for this one, and making something permanent out of it.  It's been a struggle, and it would make a fun souvenir when I get it all done.


Have you considered using a cup of some sort as the enclosure? Perhaps with the display in the base so that you shake it, put it down upside down, and see the rolled values displayed in the base? Just make sure nobody tries to drink out of it. :)
I only provide help via the forum - please do not contact me for private consultancy.

Neight



thinking about an enclosure for this one, and making something permanent out of it.  It's been a struggle, and it would make a fun souvenir when I get it all done.


Have you considered using a cup of some sort as the enclosure? Perhaps with the display in the base so that you shake it, put it down upside down, and see the rolled values displayed in the base? Just make sure nobody tries to drink out of it. :)



Man, you are full of great ideas!
I have been looking into 3D printers lately, and could even design and print something that could fit the hardware and battery in nice and secure.
very cool, I really did hope this project would turn into something fun and long term, that I could actually use.  I really liked the idea, and its only getting better as I move on!
absence of proof is not proof of absence

djjoshuad


it hangs on 20 for two pushes, then goes to 40, 60, 80, 10, 12, 20, 20, 40, 60.... on like that.
after the first time though, it starts displaying the modes in tens, instead of single and double digits.

This one I know :)

What's happening is you're overwriting the two digit number with a one digit number... with nothing to write in the second digit, the LCD continues to display what was already there.  Try using sprintf to format your number.  That should get rid of the unwanted 0

Neight



it hangs on 20 for two pushes, then goes to 40, 60, 80, 10, 12, 20, 20, 40, 60.... on like that.
after the first time though, it starts displaying the modes in tens, instead of single and double digits.

This one I know :)

What's happening is you're overwriting the two digit number with a one digit number... with nothing to write in the second digit, the LCD continues to display what was already there.  Try using sprintf to format your number.  That should get rid of the unwanted 0


HA!  you were right :D
I should have seen that, it wasn't the first time I ran into that problem, but last time it was much more obvious, I had a whole word that never left the screen.
I took the easy way out for a fix though, I just added a couple of spaces next to the result so it turns off the couple of spaces after every number.

Not sure why that started all of a sudden, but it's fixed now, and I probably just accidentally deleted something when I was rearranging the code...

Thank you so much for pointing out my error!

Now to start having fun with the display, make it a little more showy :)
absence of proof is not proof of absence

Go Up