/*
Arduino Starter Kit example
Project 11 - Crystal Ball
This sketch is written to accompany Project 11 in the Arduino Starter Kit
Parts required:
- 220 ohm resistor
- 10 kilohm resistor
- 10 kilohm potentiometer
- 16x2 LCD screen
- tilt switch
created 13 Sep 2012
by Scott Fitzgerald
https://store.arduino.cc/genuino-starter-kit
This example code is part of the public domain.
*/// include the library code:
#include<LiquidCrystal.h>// initialize the library with the numbers of the interface pins
LiquidCrystallcd(12,11,5,4,3,2);// set up a constant for the tilt switch pin
constintswitchPin=6;// variable to hold the value of the switch pin
intswitchState=0;// variable to hold previous value of the switch pin
intprevSwitchState=0;// a variable to choose which reply from the crystal ball
intreply;
voidsetup(){// set up the number of columns and rows on the LCD
lcd.begin(16,2);// set up the switch pin as an input
pinMode(switchPin,INPUT);// Print a message to the LCD.
lcd.print("Ask the");// set the cursor to column 0, line 1
// line 1 is the second row, since counting begins with 0
lcd.setCursor(0,1);// print to the second line
lcd.print("Crystal Ball!");}
voidloop(){// check the status of the switch
switchState=digitalRead(switchPin);// 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(8);// 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 line
lcd.setCursor(0,1);// choose a saying to print based on the value in reply
switch(reply){case0:lcd.print("Yes");break;case1:lcd.print("Most likely");break;case2:lcd.print("Certainly");break;case3:lcd.print("Outlook good");break;case4:lcd.print("Unsure");break;case5:lcd.print("Ask again");break;case6:lcd.print("Doubtful");break;case7:lcd.print("No");break;}}}// save the current switch state as the last state
prevSwitchState=switchState;}