5151+ reviews
Order by 16:00 for same day shipping
14 days return
EN
Individual
Business
In this Raspberry Pi project we will use a Raspberry Pi and a Sense HAT to create a Magic 8-Ball!
This manual covers:
First we are going to attach the Sense HAT to the Raspberry Pi . You do this by placing the screws from underneath the Raspberry Pi and tightening the spacers on them, then we connect the 40 pin GPIO header to the Raspberry Pi . You can now connect the Sense HAT 40 pin GPIO header. Then you attach the Sense HAT from the top by turning screws from the top through the Sense HAT into the spacers. It should look like this:
First, update and upgrade your system by entering the following commands in a terminal window (while connected to the internet):
sudo apt-get update
sudo apt-get upgrade
Now install the Sense HAT software packages:
sudo apt-get install sense-hat
sudo pip-3.2 install pillow
Finally, reboot your Raspberry Pi to complete the installation:
sudo reboot
In this project you will build your own Magic 8 Ball using your Raspberry Pi , a Sense HAT and some Python code. A Magic 8 Ball is a game where you ask a closed question, shake it and it gives you a prediction.
If you don't have Raspberry Pi and Sense HAT, you can use the Sense HAT emulator here or use the built-in Sense HAT emulator for Windows below each step in this project.
A great way to write and test your code in intervals is to use IDLE 3, a Python development application. You will write your code in Python 3.
Klik op Menu > Programmeren > Python 3 (IDLE)Zodra het Python-shellvenster is geladen,
klik je op Bestand en Nieuw bestand. Dit opent een teksteditorvenster waarin je je code kunt schrijven,
opslaan en testen. Sla het lege bestand op als magic8ball.py door te klikken op Bestand en Opslaan als.
A good way to start your Magic 8 Ball program is to first create a text version of a Magic 8 Ball program. Let’s think about what a Magic 8 Ball does. First, you ask a question, shake the ball, flip it over, and read a randomly chosen answer. Therefore, you need a list of answers and a way to randomly choose one and display that answer on the screen.
First you need to import the choice function from the random library and the sleep function from the time library. Type the following into your magic8ball.py text file:
from random import choice
from time import sleep
Using the print function, you can print text to the screen for the person using your program. Type:
print("Stel een vraag")
Next, there needs to be a pause before the program responds with an answer, so the user can ask a question. You can use the time library to ask the program to wait for a certain amount of time, like this:
sleep(3)
The program will pause for three seconds. You can adjust this value to make the time longer or shorter. Now create a list of answers the program can give to the question. Lists can be named in the same way as variables; for example, number = [1, 2, 3, 4]. This list named 'number' contains four items. Your list will contain strings of text that will be displayed on the screen. These strings will be quite long. To create your list, type:
replies = ['Tekenen wijzen op ja',
'Zonder twijfel',
'Je kunt erop vertrouwen',
'Reken er niet op',
'Het ziet er goed uit',
'Kan het nu niet voorspellen',
'Het is beslist zo',
'Outlook is niet zo goed'
]
Finally, an instruction is needed to randomly select an item from the list and then display it on the screen. You can use the random library for this by typing:
print(choice(replies))
Save your code by clicking File and Save. Then run your program to test that it works by clicking Run and Run Module. You should see “Ask a question” output in the IDLE 3 shell window.
Now that you have some text printing in the Python 3 shell window on your screen, let's modify the code so that the text scrolls across the LED matrix on your Sense HAT. To do this, you'll need to use the Sense HAT library and replace the print functions with the Sense HAT show_message function.
Under the imported modules section, add the following lines:
from sense_hat import SenseHatsense = SenseHat()
Next, replace “print” with “sense.show_message” in your code. There are two places you need to do this. Save your program by pressing Ctrl + S on your keyboard. Press F5 to run and test your program. You may notice that the text scrolls slowly across the LED matrix on your Raspberry Pi . To make the text scroll faster, you can add “scroll_speed=(0.06)” to your text strings, like this:
from sense_hat import SenseHat
from random import choice
from time import sleep
sense = SenseHat()
sense.show_message("Stel een vraag", scroll_speed=0.06)
sleep(3)
antwoorden = ['Tekenen wijzen op ja',
'Zonder twijfel',
'Je kunt erop rekenen',
'Reken er niet op',
'Het ziet er goed uit',
'Kan het nu niet voorspellen',
'Het is beslist zo',
'Vooruitzicht is niet zo goed'
]
sense.show_message(choice(antwoorden), scroll_speed=0.06)
Traditional Magic 8 Balls require the person asking a question to shake it before an answer is given. This can be simulated with a Sense HAT using an accelerometer, which measures vibration and movement. Accelerometers are found in most smartphones and change the orientation of the screen depending on how you hold the device.
Let's use the accelerometer on the Sense HAT to detect any changes in the amount of g-force on each axis (x, y, and z) before executing the part of your program that returns an arbitrary response to the user.
while True:
x, y, z = sense.get_accelerometer_raw().values()
x = abs(x)
y = abs(y)
z = abs(z)
Note that capitalization and indentation are very important in Python. Make sure to use 4 spaces to indent after a line with a colon. Using abs will convert any number to a positive number, meaning it ignores the direction of the vibration and looks at the amount of vibration! Now it's time to set up a condition in our code that checks if the x, y, and z axes have changed (i.e. moved) before it selects a random answer. If no movement is detected, no answer will be output.
if x > 2 or y > 2 or z > 2 :
sense.show_message(choice(replies))
else:
sense.clear()
The program checks if the x, y, and z axes are greater than a value of 2. By changing this value, you can adjust the program's sensitivity to movement. If you really want someone to shake the Raspberry Pi and Sense HAT vigorously, use a higher value. Save your program by pressing Ctrl + S on your keyboard. Press F5 to run and test your program. Your code should look like this:
from sense_hat import SenseHat
from random import choice
from time import sleep
sense = SenseHat()
sense.show_message("stel een vraag", scroll_speed=0.08)
sleep(3)
replies = ["JA!",
"Nee",
"Misschien",
"Zeker Weten!",
"Tuurlijk Niet!",
]
while True:
x, y, z = sense.get_accelerometer_raw().values()
x = abs(x)
y = abs(y)
z = abs(z)
if x > 2 or y > 2 or z > 2 :
sense.show_message(choice(replies))
else:
sense.clear()
And that's it! Didn't work? Look back at the previous steps to see what went wrong. Did it work? Then take a look at our projects!