字幕列表 影片播放
Hi my name is Massimo Banzi
and I like to make things.
Welcome to another
Arduino tutorial video.
Today we are going to build
version of the magic 8ball (Crystall Ball)
This is a
simple device with a LCD screen
that provides a different answer
every time is being shaken.
So, let's try. Look at this.
8ball says: Outlook good.
How does this project all work?
We have a sensor,
which is the tilt-sensor over here
that contains a small ball
sitting on two contacts.
When the tilt sensor is vertical
the ball sits on the contacts,
closes the circuit and acts like
a push button that is been pressed.
When you shake the board
the ball bounces off the contacts
and opens the circuit and is almost like
when you release your finger from a button.
The data from the sensor
goes into the Arduino
and there is a software that detects
that you are shacking the Arduino
and picks a random answer
and displays in on this LCD display.
It is fairly simple to connect
an LCD screen to Arduino
and this one in particular
because it is a character based LCD,
so it contains already electronics
on the LCD module itself
that can receive data from the Arduino
in terms of character codes
and then display the characters
on the LCD screen in the right position.
Let's have a look at the circuit.
We have the LCD module here.
We are bringing four wires
from four Arduino pins
to the LCD module.
Those four wires carry the data
from the Arduino to the LCD screen.
Then we have an extra two pins
connected to the Arduino board
that are used in the communication between
the Arduino and the LCD screen.
Then we power the LCD screen.
You see there are a red and a black wire
coming from the plus and minus rails.
Then there is this potentiometer.
This potentiometer here is used
to generate a voltage
between 0 and 5 volts
that is applied to the contrast PIN
so if I start turning this potentiometer
you can see that the contrast
on the display changes
so we have to tweak it
until the value makes the display
work properly.
So this can be tweaked
depending also on the angle
that you watch the LCD.
As I said: data,
a couple of control lines,
power, contrast
and this is all we need
in order to connect to the LCD module.
Here we have the tilt sensor.
It is wired up exactly
like a regular push button
so we have one leg
of the tilt sensor
which is connected to a resistor
to ground, the other one
is connected to five volts
and the place when the resistor
and the tilt sensor connect
is where we connect a wire
that goes to an input on the Arduino board,
then the Arduino can read
if the tilt sensor is connected or not.
So, when I shake... like this...
Arduino detects the shaking
and changes the answer on the screen.
So, to recap we have a tilt sensor
connected to the Arduino
and we have six wires
coming from the Arduino
and connecting to the LCD screen.
The data that goes from the Arduino
to the LCD screen is actually
represented as 8 bit numbers,
but we wire up only
four wires and we use
a special mode in the LCD display
that carries 8 bit data,
4 bits at a time.
Using the LCD would require
to write quite a lot of code
but luckily there is
a liquid cristal library
inside the Arduino platform
that allows you to control
this class of characteral LCD displays
in a very simple way.
Now we are going to have a look at the code
and we are going to figure out
how everything works.
Lets look at the code.
We start by including
the liquidCrystal library
so we use #include
<liquidCrystal.h>.
This can be done, actually,
by selecting appropriate import library menu
from the IDE,
and then once we have included
the liquidCrystal library into our code
we have to tell the library
which one are the pins
that are connected to the LCD.
So, we specify 12, 11, 5, 4, 3 and 2.
These are the pins we are using here
to convey the four pins of data,
then the RS and RW pins
that are used in the hand-shacking
and in the communication between
the Arduino and the LCD.
Once we have done that
we are ready to use the LCD screen
so we define another constant:
switchPin=6.
This pin number 6
is where we connect the tilt sensor,
switchState again is a variable
used to store the state,
the current state of the tilt switch.
And then we have another variable
called prevSwitchState
and the fact that we need to
store the current and the previous
value of a certain switch
will become clear later.
Now we have another integer
variable called "reply".
Let's look at the setup() function.
We open the communication
with the LCD screen by using
lcd.begin()
and then in the begin() function
we specify 16 and 2
to tell the library
that the LCD we are using
has two lines of sixteen characters each
because there are many different types
of LCD screen like this,
so when we initialize the communication
we have to specify the size
of the LCD display.
Then we use pinMode()
to tell Arduino that switchPin
is an input
and then we use lcd.print()
to write the first line
at the top that says "Ask the".
Then we use another interesting function
of the LCD library: lcd.setCursor()
setCursor() allows us
to specify each column and row
we want to start printing from.
So I can move the cursor
anywhere on the LCD display
by specifying the position.
With the last line in the setup()
we print the second line on the screen
lcd.print("Magic 8ball!")
To recap:
in the setup() we are basically
opening the communication with the LCD screen,
preparing the switch pin to be an input
and then we print on the two lines
of the display "Ask the" "Magic 8ball!".
Then let's get into the loop.
The first line in the loop
stores the current state of the switch
into switchState by doing
a digitalRead() on the pin.
And then we say:
if switchState is different then
the prevSwitchState
this if-statement is used to figure out
if the state of the button
has recently changed,
because we want to provide
a new answer on the screen
only when the state of the switch changes
and the state of the switch changes
only when I shake the circuit
and the ball inside the tilt-switch
jumps up and down.
So if switchState
and prevSwitchState are different
then we can move on and we can say
"if (switchState == LOW)"
then actually generate a new reply.
So we start from clearing the display
by using lcd.clear(),
then we generate
a random number between 0 and 7
that is stored into the "reply" variable.
Then we setCursor() to 0, 0
which is the top left corner of the screen.
We do lcd.print("8ball says:"),
then we set the cursor on the second
line of the display
by doing lcd.setCursor (0, 1)
and then we use an instruction
called "switch" that
allows us to run different
parts of code
depending on the value
of the specific variable.
In this case we switch
based on the value
of the "reply" variable.
So, if the number
that was generated randomly is 0
we are going to lcd.print()
on the screen the word "Yes".
Then we have a statement
called "break" that tells
Arduino that we are done
executing the code in that section
and we want to exit the switch-statement.
Then, for every particular value
that the variable can assume,
we have 0, 1, 2, 3
and each one of them corresponds to a message.
So we have "Unsure", "Ask again",
"Outlook good", "No".
Once we are done going
through the switch-statement
the screen will have an answer
and then at the very bottom of the code
we have one line that says
"prevSwitchState = switchState".
So the current state
of the switch "switchState"
is not current anymore.
At the end of your code that's old,
it's the previous state.
So we store it in the previous state
and we go back to the beginning
where the first line
is taking a new value
into "switchState"
and this allows us to
detect every time the value changes.
We got to the end of the code
and this is the end result.
So, I'm going to press the reset button
so we can start the code from the beginning.
You see "Ask the Magic 8ball!".
I'm going to shake this
and the Magic 8ball says "Yes"
so I have to ask a question:
"Was this video cool?".
"Yes", the Magic 8ball says "Yes".
So I think this is a very good conclusion
for this video.
I hope you enjoyed the video
and remember: you have to build this tutorial,
you have to hack it
and you have to share the results
on the internet
because Arduino is you.