Go Down

Topic: Project 11 Crystal Ball with push button switch (Read 820 times) previous topic - next topic

pittfan

My students are trying to trouble shoot how to use a push button switch instead of a tilt switch.
here is their code.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup() {
  lcd.begin(16, 2);
  pinMode(switchPin,INPUT);
  lcd.print("Ask the");
  lcd.setCursor(0, 1);
  lcd.print("Crystal Ball!");
}
void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    if (switchState == HIGH) {
      reply = random(0,9);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Crystal Ball Says:");
      lcd.setCursor(0, 1);
      switch(reply){
        case 0:
        lcd.print("yes");
        break;
        case 1:
        lcd.print("Most Likely");
        break;
        case 2:
        lcd.print("Certainly");
        break;
        case 3:
        lcd.print("Probably");
        break;
        case 4:
        lcd.print("Unsure");
        break;
        case 5:
        lcd.print("Come Again");
        break;
        case 6:
        lcd.print("Doubtful");
        break;
        case 7:
        lcd.print("No");
        break;
      }
    }
  }
  prevSwitchState = switchState;
}

I am attaching a few pictures of our circuit.
Thank You

pittfan

I apologize... I forgot to tell you our problems..

The LCD displays "Ask the Crystal Ball!" But the does not change when the button is pressed.

Thank you

JohnLincoln

I think might have chosen the wrong pins on the push button switch to connect to. 

I believe that the two you have connected to are connected together permanently, and when the button is pressed they are connected to the other two.

You can get round this by rotating the push button switch through 90°.

pittfan

That was the problem! It works!... thank you for your help and quick response.
Best Wishes.

 :)

Go Up