Hello, everyone. Welcome back.
Now, we'll be going through the code for the Magic 8 Ball,
so guys understand a little bit of how it works.
Andrew will just be my little monkey just going through it,
and he'll also talk about the code.
Monkey, okay. Let's go through with it using vim,
and this was created through C++.
So, here we are. As you can see,
we see the copyright at the top,
saying that it was created by Linaro our 96 Boards.
We see that it includes certain libraries.
If you guys don't know the first two libraries,
you guys can look these up.
These are pretty popular in C++.
We are not going to go through them.
But if you guys are curious of what it actually does,
use your favorite search engine to learn about it more.
The next two, the first one is
mraa.hpp and if you guys haven't seen our libmraa walkthrough,
this is basically allowing us to access the Pens through
their numbers and we would advise you guys
to go back and look at that if you guys don't know what that is.
UPM is the same deal.
If you didn't watch our libupm walkthrough,
you guys can go back as well and look at which files we're actually copying over.
And using namespace STD,
this is telling the code that we're are using STD.
In C++ it's standard where you add something and then you do colon colon.
But if you just do using namespace,
then you don't need to do that.
You guys can learn about this more on your own as well.
Int last touch, this is a variable that
tracks when was the last time you actually touched a button and what was the state of it.
So, this is important because you need to track whether you were touching it.
So when you release it,
it knows that you released it as well.
Or if you're not touching it and then you start to touch it,
it knows when you're doing that as well.
So here's the actual execution part of the code where everything happens.
The first two are just variables to keep track of what state the touch sensor is in,
whether or not it's being touched or not.
And then this first part initializes
the GPIO for the touch sensor and for the accuracy for
the LCD display and the part right after that is just clearing
out the LCD display and setting up the first amount of text.
As you can see, it will just display ask question and press.
So the next part is the while loop
and since it needs to keep checking the touch sensor and the LCD,
you will need a while loop.
A while loop is a loop that just goes continuously until the condition is met.
So while (true) means that you're always going to go through it unless you stop the code.
You guys can learn about this more as well.
And here's the part where we take
the random number through the random number generator and divide it by 20.
So, we know that these numbers will range from zero to 19.
And once we get that number from zero to 19,
it will go through a switch statement.
A switch statement is basically saying that if it's this number, it will do this.
If it's another number, it will do something else.
As you can see,
if we scroll down to 15, "very doubtful".
And, you guys saw this in our last video.
That means two times the random number divided by
20 would have given us 15 allowing the LCD to display "very doubtful".
This may be a little complicated for
you guys if you guys are just starting off with coding but,
trust me, it's very easy to understand once you guys understand the concepts.
And then here's the usleep and this is allowing a delay for the touch sensors,
so you can't spam it.
You need some time for the LCD and everything to process.
And then the last_touch basically tracks your last touch,
compare it to the previous touch.
And, this is used to track whether you're pressing it or releasing it.
And then this is the last part, just some cleaning up,
deleting everything when it's done and exiting the program.
There is the program.
Feel free to just add another case.
For example a case 20,
and changing the randoms so
that it can actually go to that 20 and adding your own message,
or just customizing it, or even changing the color if you want to.
This is just a basic project to teach you more about how it works.