| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 11 - Crystal Ball |
| 4 |
|
| 5 |
This sketch is written to accompany Project 11 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- 220 ohm resistor |
| 9 |
- 10 kilohm resistor |
| 10 |
- 10 kilohm potentiometer |
| 11 |
- 16x2 LCD screen |
| 12 |
- tilt switch |
| 13 |
|
| 14 |
created 13 Sep 2012 |
| 15 |
by Scott Fitzgerald |
| 16 |
|
| 17 |
https://store.arduino.cc/genuino-starter-kit |
| 18 |
|
| 19 |
This example code is part of the public domain. |
| 20 |
*/ |
| 21 |
|
| 22 |
// include the library code: |
| 23 |
#include <LiquidCrystal.h> |
| 24 |
|
| 25 |
// initialize the library with the numbers of the interface pins |
| 26 |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); |
| 27 |
|
| 28 |
// set up a constant for the tilt switch pin |
| 29 |
const int switchPin = 6; |
| 30 |
|
| 31 |
// variable to hold the value of the switch pin |
| 32 |
int switchState = 0; |
| 33 |
|
| 34 |
// variable to hold previous value of the switch pin |
| 35 |
int prevSwitchState = 0; |
| 36 |
|
| 37 |
// a variable to choose which reply from the crystal ball |
| 38 |
int reply; |
| 39 |
|
| 40 |
void setup() { |
| 41 |
// set up the number of columns and rows on the LCD |
| 42 |
lcd.begin(16, 2); |
| 43 |
|
| 44 |
// set up the switch pin as an input |
| 45 |
pinMode(switchPin, INPUT); |
| 46 |
|
| 47 |
// Print a message to the LCD. |
| 48 |
lcd.print("Ask the"); |
| 49 |
// set the cursor to column 0, line 1 |
| 50 |
// line 1 is the second row, since counting begins with 0 |
| 51 |
lcd.setCursor(0, 1); |
| 52 |
// print to the second line |
| 53 |
lcd.print("Crystal Ball!"); |
| 54 |
} |
| 55 |
|
| 56 |
void loop() { |
| 57 |
// check the status of the switch |
| 58 |
switchState = digitalRead(switchPin); |
| 59 |
|
| 60 |
// compare the switchState to its previous state |
| 61 |
if (switchState != prevSwitchState) { |
| 62 |
// if the state has changed from HIGH to LOW you know that the ball has been |
| 63 |
// tilted from one direction to the other |
| 64 |
if (switchState == LOW) { |
| 65 |
// randomly chose a reply |
| 66 |
reply = random(8); |
| 67 |
// clean up the screen before printing a new reply |
| 68 |
lcd.clear(); |
| 69 |
// set the cursor to column 0, line 0 |
| 70 |
lcd.setCursor(0, 0); |
| 71 |
// print some text |
| 72 |
lcd.print("the ball says:"); |
| 73 |
// move the cursor to the second line |
| 74 |
lcd.setCursor(0, 1); |
| 75 |
|
| 76 |
// choose a saying to print based on the value in reply |
| 77 |
switch (reply) { |
| 78 |
case 0: |
| 79 |
lcd.print("Yes"); |
| 80 |
break; |
| 81 |
|
| 82 |
case 1: |
| 83 |
lcd.print("Most likely"); |
| 84 |
break; |
| 85 |
|
| 86 |
case 2: |
| 87 |
lcd.print("Certainly"); |
| 88 |
break; |
| 89 |
|
| 90 |
case 3: |
| 91 |
lcd.print("Outlook good"); |
| 92 |
break; |
| 93 |
|
| 94 |
case 4: |
| 95 |
lcd.print("Unsure"); |
| 96 |
break; |
| 97 |
|
| 98 |
case 5: |
| 99 |
lcd.print("Ask again"); |
| 100 |
break; |
| 101 |
|
| 102 |
case 6: |
| 103 |
lcd.print("Doubtful"); |
| 104 |
break; |
| 105 |
|
| 106 |
case 7: |
| 107 |
lcd.print("No"); |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
// save the current switch state as the last state |
| 113 |
prevSwitchState = switchState; |
| 114 |
} |