0

members

Medium Arduino Starter Kit: Project 11 CRYSTAL BALL

of Arduino in UNO

Arduino Starter Kit: Project 11

Project name: CRYSTAL BALL

Discover: LCD displays, switch/case statements, random()

Attachments: libraries and program1 and program2

In this project, you needed these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.Jumper cables

3. Tilt switch 1 pc

4. LCD screen or LCD1602 I2C module (see connections and sketch below)

5. Potentiometer

6. Resistor 2 pcs (1pc 10 kOm and 1 pc 220 Om)

7. Breadboard half size 

8. Arduino IDE ( you can download it from here  )

GENERAL

Crystal balls can help “predict” the future. You ask a question to the all-knowing ball, and turn it over to reveal an answer. The answers will be predetermined, but you can write in anything you like. You’ll use your Arduino to choose from a total of 8 responses. The tilt switch in your kit will help replicate the motion of shaking the ball for answers. The LCD can be used to display alphanumeric characters. The one in your kit has 16 columns and 2 rows, for a total of 32 characters. There are a large number of connections on the board. These pins are used for power and communication,so it knows what to write on screen, but you won’t need to connect all of them. See picture below for the pins you need to connect. The pins on the LCD screen that are used in the project and labels.

THE CIRCUIT

The circuit is not overly complex, but there are a lot of wires. Pay attention when wiring everything up to make sure it’s correct. Connect power and ground to one side of your breadboard. Place the tilt switch on the breadboard and attach one lead to 5V. Attach the other side to ground through a 10-kilohm resistor, and to your Arduino’s pin 6. You’re wiring this as a digital input, just as you’ve done in several other projects. The register select (RS) pin controls where the characters will appear on screen. The read/write pin (R/W) puts the screen in read or write mode. You’ll be using the write mode in this project. The enable (EN) tells the LCD that it will be receiving a command. The data pins (D0-D7) are used to send character data to the screen. You’ll only be using 4 of these (D4-D7). Finally, there’s a connection for adjusting the contrast of the display. You’ll use a potentiometer to control this. The LiquidCrystal library that comes with the Arduino software handles all the writing to these pins, and simplifies the process of writing software to display characters. The two outside pins of the LCD (Vss and LED-) need to be connected to ground. Also, connect the R/W pin to ground. This places the screen in write mode. The LCD power supply (Vcc) should connect directly to 5V. The LED+ pin on the screen connects to power through a 220-ohm resistor. Connect: Arduino Digital pin 2 to LCD D7, Arduino Digital pin 3 to LCD D6, Arduino Digital pin 4 to LCD D5, Arduino Digital pin 5 to LCD D4. These are the data pins that tell the screen what character to display. Connect EN on the screen to pin 11 on your Arduino. RS on the LCD connects to pin 12. This pin enables writing to the LCD. Place the potentiometer on the breadboard, connecting one end pin to power and the other to ground. The center pin should connect to V0 on the LCD. This will allow you to change the contrast of the screen.

THE CODE
First, you’ll need to import the LiquidCrystal library. Next, you’ll initialize the library, somewhat similar to the way you did with the Servo library, telling it what pins it will be using to communicate
Now that you’ve set up the library, it’s time to create some variables and constants. Create a constant to hold the pin of the switch pin, a variable for the current state of the switch, a
variable for the previous state of the switch, and one more to choose which reply the screen will show. Set up the switch pin as an input with pinMode() in your setup(). Start the LCD library, and tell it how large the screen is. Now it’s time to write a small introductory screen welcoming you to the 8-ball. The print() function writes to the LCD screen. You’re going to write the words “Ask the” on the top line of the screen. The cursor is automatically at the beginning of the top line.
In order to write to the next line, you’ll have to tell the screen where to move the cursor. The coordinates of the first column on the second line are 0,1 (recall that computers are zero indexed.
0,0 is the first column of the first row). Use the function lcd. setCursor() to move the cursor to the proper place, and tell it to write “Crystal ball!”. Now, when you start the program, it will say “Ask the Crystal ball!” on your screen. In the loop(), you’re going to check the switch first, and put the value in the switchState variable. Use an if() statement to determine if the switch is in a different
position than it was previously. If it is different than it was before, and it is currently LOW, then it’s time to choose a random reply. The random() function returns a number based on the
argument you provide it. To start, you’ll have a total number of 8 different responses for the ball. Whenever the statement random(8) is called, it will give a number between 0-7. Store that
number in your reply variable.
LCD library reference: http://arduino.cc/lcdlibrary
Random reference: http://arduino.cc/random
Clear the screen with the function lcd.clear(). This also moves the cursor back to location 0,0; the first column in the first row of the LCD. Print out the line “The ball says:” and move the cursor for the output.
The switch() statement executes different pieces of code depending on the value you give it. Each of these different pieces of code is called a case. switch() checks the value of the variable reply; whatever value reply holds will determine what named case statement is executed. Inside the case statements, the code will be the same, but the messages will be different. For example, in case 0 the code says lcd.print (“Yes”). Afer the lcd.print() function, there’s another command: break. It tells the Arduino where the end of the case is. When it hits break, it skips to the end of the switch statement. You’ll be creating a total of 8 case statements to start out. Four of the responses will be positive, 2 will be negative, and the final 2 will ask you to try again.
The last thing to do in your loop() is to assign switchState’s value to the variable prevSwitchState. This enables you to track changes in the switch the next time the loop runs.

THE CODE

First, you’ll need to import the LiquidCrystal library. Next, you’ll initialize the library, somewhat similar to the way you did with the Servo library, telling it what pins it will be using to communicate. Now that you’ve set up the library, it’s time to create some variables and constants. Create a constant to hold the pin of the switch pin, a variable for the current state of the switch, avariable for the previous state of the switch, and one more to choose which reply the screen will show. Set up the switch pin as an input with pinMode() in your setup(). Start the LCD library, and tell it how large the screen is. Now it’s time to write a small introductory screen welcoming you to the 8-ball. The print() function writes to the LCD screen. You’re going to write the words “Ask the” on the top line of the screen. The cursor is automatically at the beginning of the top line.In order to write to the next line, you’ll have to tell the screen where to move the cursor. The coordinates of the first column on the second line are 0,1 (recall that computers are zero indexed.0,0 is the first column of the first row). Use the function lcd. setCursor() to move the cursor to the proper place, and tell it to write “Crystal ball!”. Now, when you start the program, it will say “Ask the Crystal ball!” on your screen. In the loop(), you’re going to check the switch first, and put the value in the switchState variable. Use an if() statement to determine if the switch is in a differentposition than it was previously. If it is different than it was before, and it is currently LOW, then it’s time to choose a random reply. The random() function returns a number based on theargument you provide it. To start, you’ll have a total number of 8 different responses for the ball. Whenever the statement random(8) is called, it will give a number between 0-7. Store thatnumber in your reply variable.

LCD library reference: http://arduino.cc/lcdlibrary

Random reference: http://arduino.cc/random

Clear the screen with the function lcd.clear(). This also moves the cursor back to location 0,0; the first column in the first row of the LCD. Print out the line “The ball says:” and move the cursor for the output.The switch() statement executes different pieces of code depending on the value you give it. Each of these different pieces of code is called a case. switch() checks the value of the variable reply; whatever value reply holds will determine what named case statement is executed. Inside the case statements, the code will be the same, but the messages will be different. For example, in case 0 the code says lcd.print (“Yes”). Afer the lcd.print() function, there’s another command: break. It tells the Arduino where the end of the case is. When it hits break, it skips to the end of the switch statement. You’ll be creating a total of 8 case statements to start out. Four of the responses will be positive, 2 will be negative, and the final 2 will ask you to try again.The last thing to do in your loop() is to assign switchState’s value to the variable prevSwitchState. This enables you to track changes in the switch the next time the loop runs.

THE START

To use the magic ball, power the Arduino. Check the screen to make sure it says “Ask the Crystal ball!” If you can’t see the characters, try turning the potentiometer. It will adjust the contrast of the screen. Ask a question of your crystal ball, and try tilting the switch upside down and back again. You should get an answer to your question. If the answer doesn’t suit you, ask again. Try adding your own sayings to the print() statements, but be mindful of the fact that there are only 16 characters to use per line. You can also try adding more responses. Make sure when you add additional switch cases, you adjust the number of options that will randomly populate the reply variable.

LCDs work by changing the electrical properties of a liquid sandwiched between polarized glass. The glass only allows certain kinds of light to pass through. When the liquid between the glass is charged, it starts to form into a semi-solid state. This new state runs in a different direction than the polarized glass, blocking light from passing through, thus creating the characters you see on the screen.

The functions covered here for changing the LCD screen’s text are fairly simple. Once you have a handle on how it works, look at some of the other functions the library has. Try getting text to scroll, or continually update. To find out more about how the LiquidCrystal library works, visit: http://arduino.cc/lcd.

An LCD display enables you to show text on a screen, using the LiquidCrystal library. With a switch...case statements control the flow of programs by comparing a variable to specified values.

THE BONUS

If you have LCD 1602 I2C module:

The sketch is here.



Other projects of Arduino

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 02-07-2017
Viewed: 1293 times