source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/10.StarterKit_BasicKit/p11_CrystalBall/p11_CrystalBall.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

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