[Music]
hi my name is masim OB banzi and I like
to make things welcome to another
Arduino tutorial video today we're going
to build an electronic version of the
Magic 8 pole so this is a simple device
with an LCD screen that provides a
different answer every time it's being
shaken so let's try look at this eight B
says Outlook good
so how does this project all work we
have a sensor which is the Tilt sensor
over here that contains a small bowl
sitting on a 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's being pressed
when you shake the board the ball
bounces off the contacts and open the
circuits and it's almost like you it's
like when you release your finger from a
button the data from the sensor goes
into the Arduino and there's a software
that detects that you're shaking the
Arduino and picks a random answer and
displays on this LCD display it's fairly
simple to connect an LCD screen to
Arduino
and this one in particular because it's
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 so we have the LCD module
here we are bringing four wires from
four Arduino pins to the LCD modules
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 uh you see there's a red and a
black wire coming from the plus and
minus rail uh then there is this
potentiometer the 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 on the angle that you
watch the LCD so 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 til sensor is
wired up exactly like a regular push
button so we have one leg of the til
sensor is connected to a resistor to
ground the other one is connected to 5
Vols and the place where the resistor
and the till sensor connect is where we
connect a wire that goes to an input on
the Arduino board then the Arduino can
read if the teal 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
connect connected to the Arduino and we
have six wire 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 8bit numbers but we are
we wire up only four wires and we use a
special mode in the LCD displays that
carries 8bit data at four bits at a
time so using the LCD would require you
to write quite a lot of code but luckily
there is a liquid Crystal library inside
the Arduino uh platform that allows you
to control this class of uh character
LCD displays in a very simple way so now
we're going to have a look at the code
and we're going to figure out how
everything
works let's look at the code we start by
by including the Liquid Crystal Library
so we use pound sign include liquid
crystal. this can be done actually by
selecting appropriate import Library
menu from the IDE and then once we have
included the Liquid Crystal 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 this are the
pins that we're using here to convey the
four pins of data then the RS and uh
RW pins that are used in the hand
shaking 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 switch pin
equals six this switch pin uh this pin
number six is where we connect the uh
tilt sensor switch State again is a
variable uh used to store the state the
current state of the til switch and then
we have another variable called pre-
switch State and the fact that we need
to uh store the current and the previous
value of a certain uh switch will become
clear later now we have one another
integer variable called reply so let's
look at the setup function we open the
communication with the LCD screen by
using LCD do begin and then in the begin
function we specify 16 and two to tell
the library that the LCD that we are
using has two lines of 16 characters
each because there are many different
types of LCD screens like this so when
we initialize the communication we have
to specify the size size of the LCD
display then we use pin mode to tell our
doino that switch pin is an input and
then we use LCD do print to write the
first line at the top that says ask
V then we use another interesting
function of the LCD um Library LCD do
set cursor set cursor allows us to
specify each Colum and raow we are 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 with the in the setup we
print the second line on the screen so
LCD dopr magic a b so to recap in the
setup we're basically opening the
communication with the LCD screen
preparing the pin a switch pin to be an
input and then we print on the two lines
of the display Ask the Magic a
pole then let's get into the
loop so the first line in the loop
stores the current state of the switch
into switch state by doing a digital
read on the pin and then we say if
switch state is different than previous
switch
State and this fun this this if
statement it's 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 switch
State and previous switch state are
different then we can move on and we can
say if switch State equal equal law then
actually generate a new reply so we
start from clearing the display by using
LCD do
clear then we generate a random number
between 0o and seven that gets stored
into the reply variable then we set the
cursor to 0 0 so the top left corner of
the screen we say we do LCD print8 ball
says then we set the cursor on the
second line of the display by doing LCD
set cursor
01 and then we use an instruction called
switch that
basically allows us to run different
parts of code depending on the value of
a specific variable in this case we
switch based on the value of the reply
variable so if the number that was
generated randomly is zero we are going
to LCD do print on on the screen the the
word yes and then we have a statement
called break that tells Arduino that
we're done executing the code in that
section and we want to exit the switch
statement and then for every particular
value that the uh variable can assume we
have 0 1 2 three and each one of them
corresponds to a message so we have
unsure ask again Outlook good
no once we are done going through
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 prev switch State equal switch
state so with the
current state of the switch switch state
is not current anymore at the end of
your code that's old it's it's the
preview state so we store it in preview
State and we go back to the beginning
where the first line is taking a new
value into switch State and 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 I'm
going to press the reset button so we
can start the code from the
beginning so you see ask the magic eight
ball so I'm going to shake this and the
magic eight ball says yes so I have to
ask a question was this video
cool yes the magic eight ball says yes
so I think this is a very good
conclusion for this
video I hope you enjoy 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
[Music]