LCD CRYSTAL BALL WITH BUTTON

Hi! I'm modifying the crystal ball code for the Monopoly Chance and Community chest cards. I wanted to add a button so the user can switch between chance or community chest.

I came up with this code, and it compiled. But when I ran it on the arduino, it's stuck on the
"monopoly" screen.

    // include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//int counter = 1;
//int buttonPin = 13;

// set up a constant for the tilt switchPin
const int switchPin = 6;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

// a variable to choose which reply from the crystal ball
int reply;

void setup() {
//  pinMode(buttonPin, INPUT);
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);
  Serial.begin (9600);

  // set up the switch pin as an input
  pinMode(switchPin, INPUT);

  // Print a message to the LCD.
  //lcd.print("Ask the");
    Serial.print ("MONOPOLY");
  // set the cursor to column 0, line 1
  // line 1 is the second row, since counting begins with 0

}

void loop() {

//Handle input
digitalRead(buttonPin);
 if(buttonPin == HIGH && counter==1)
 {
  counter=2; // chance
   //Reset count if over max mode number
   if(counter == 2)
   lcd.setCursor (0,0);
   Serial.print ("CHANCE");
   {
     counter = 1; //community chest
     lcd.setCursor (0,0);
     Serial.print ("COMMUNITY CHEST");
   }
 }
 
 //Change mode
 if(counter == 1) // COMMUNITY CHEST
 {
 // compare the switchState to its previous state
  if (switchState != prevSwitchState) {
    // if the state has changed from HIGH to LOW
    // you know that the ball has been tilted from
    // one direction to the other
    if (switchState == LOW) {
      // randomly chose a reply
      reply = random(11);
      // clean up the screen before printing a new reply
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // print some text
      //lcd.print("the ball says:");
      // move the cursor to the second 
        Serial.print ("Shake me");
     

      // choose a saying to print baed on the value in reply
      switch (reply) {
        case 0:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Pass GO- ");
            lcd.setCursor(0,1);
            Serial.print ("Collect $200!");
            delay (500);

          break;

        case 1:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Doctor's fees- ");
            lcd.setCursor(0,1);
            Serial.print ("Pay $50.");
            delay(500);
        

            break;
            
        case 2:
          lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Get out of");
            lcd.setCursor(0,1);
            Serial.print ("Jail Free!");
            delay(500);

          break;

        case 3:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Go directly to");
            lcd.setCursor(0,1);
            Serial.print ("Jail.");
            delay (500);
          break;

        case 4:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Xmas fund ");
            lcd.setCursor(0,1);
            Serial.print ("matures. Get $50");
            delay(500);
          break;

        case 5:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("It's your Bday!");
            lcd.setCursor(0,1);
            Serial.print ("Get $50/player.");
            delay(500);

          break;

        case 6:
          lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Pay hospital fee");
             lcd.setCursor(0,1);
            Serial.print ("of $100..");
            delay(500);
          break;

        case 7:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Receive $25 ");
            lcd.setCursor(0,1);
            Serial.print ("consultancy fee.");
            delay(500);

          break;

          case 8:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("YOU INHERIT");
            lcd.setCursor(0,1);
            Serial.print ("$100!!");
            delay(500);

          break;

           case 9:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Pay school fees");
            lcd.setCursor(0,1);
            Serial.print ("of $100.");
            delay(500);

          break;
           
           case 10:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Your stocks sell");
            lcd.setCursor(0,1);
            Serial.print ("you get $50.");
            delay(500);

          break;
      }
    }
  }
  // save the current switch state as the last state
  prevSwitchState = switchState;
}

 //else (counter == 2);// COMMUNITY CHEST

 
 {
   // compare the switchState to its previous state
  if (switchState != prevSwitchState) {
    // if the state has changed from HIGH to LOW
    // you know that the ball has been tilted from
    // one direction to the other
    if (switchState == LOW) {
      // randomly chose a reply
      reply = random(11);
      // clean up the screen before printing a new reply
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // print some text
      //lcd.print("the ball says:");
      // move the cursor to the second 
        Serial.print ("Shake me");
     

      // choose a saying to print baed on the value in reply
      switch (reply) {
        case 0:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Advance to GO.");
            lcd.setCursor(0,1);
            Serial.print ("Collect $200");

          break;

        case 1:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Advance yo ");
            lcd.setCursor(0,1);
            Serial.print ("Illinois Ave.");
            delay(500);

            break;
            
        case 2:
          lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Advance to St.");
            lcd.setCursor(0,1);
            Serial.print ("Charles Place.");
            delay(500);

          break;

        case 3:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Advance to nearest");
            lcd.setCursor(0,1);
            Serial.print ("utility.");
            delay(500);
          break;

        case 4:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Get out of Jail");
            lcd.setCursor(0,1);
            Serial.print ("Free!");
            delay(500);
          break;

        case 5:
         lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Go back 3");
            lcd.setCursor(0,1);
            Serial.print ("spaces.");
            delay(500);

          break;

        case 6:
          lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Pay poor tax of");
            lcd.setCursor(0,1);
            Serial.print ("$15.");
            delay(500);
        

          break;

        case 7:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Take a walk on ");
            lcd.setCursor(0,1);
            Serial.print ("Boardwalk.");
            delay(500);

          break;

          case 8:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Win a contest");
            lcd.setCursor(0,1);
            Serial.print ("Collect $100!!");
  delay(500);
          break;
      
       case 9:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Pay each player");
            lcd.setCursor(0,1);
            Serial.print ("$50.");
              delay(500);

          break;
          
           case 10:
        lcd.setCursor(0,0);
          //lcd.print("Yes");
            Serial.print ("Bank pays you ");
            lcd.setCursor(0,1);
            Serial.print ("dividend of $50");
              delay(500);

          break;
      }
      }
    }
  }
  // save the current switch state as the last state
  prevSwitchState = switchState;
}

Any suggestions? :slight_smile:

Thank you so much for the help guys!

I'm no code expert, but your code is hard to follow. I am not sure where the problem is exactly. I might suggest using more function calls, and simplifying the logic, getting that to work, then building on all the data you want (all the card info). it really seems, a very long switch case is a poor tool here. I have not run this, just pulled form working code - but this is how I'd tackle it. use string arrays and a pointer. move the pointer based on input, then print the string at its current pointer. the increment logic here will rotate through 4 choices. only thing I saw is that you didnt say your initial state of your button (low), and bad wiring can mess this up to (not using pull up or pull down resister).

int ModePin = 3;
char* ModePrint[] ={"choice 0","choice 1","choice 2","choice 3"};
int mode = 0;

void setup() {

pinMode(ModePin, LOW);
Serial.begin(9600);
}

void loop() {

ModeSelect();
Serial.print(ModePrint[mode]);

}

void ModeSelect() //used for modes - if switch is high, increments mode until 3, then circle back to 0.
{
int switchstate = 0; // for the mode select function
switchstate = digitalRead(ModePin);

if(switchstate == HIGH)
{
if(mode <4)
{
mode ++;
}
else
{
mode = 0;
}
}

}

You said switchPin is 6, but check on buttonPin, where is buttonPin?

const int switchPin = 6;


void loop() {

	//Handle input
	digitalRead(buttonPin);
	if(buttonPin == HIGH && counter==1)
	{

You need to hold the state of the switch and not check on the pin which is always 6

switchState = digitalRead(buttonPin);