Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

Raspberry Pi project: Magic 8 Ball

At this Raspberry Pi project we are going to use one Raspberry Pi and make Sense HAT a Magic 8-Ball!

 This manual covers: 

  • Supplies
  • Circuit Making
  • Upgrade and Update
  • Create new file
  • Programming (Text version)
  • Programming (SenseHat)
  • To shake
  • Beginner 40% 40%
  • 60 -70 minutes 45% 45%
  • Costs €146,15 55% 55%

Step 2: Making a circuit

We are first going to attach the Sense HAT to the Raspberry Pi. You do this by removing the screws from underneath the Raspberry Pi and tighten the spacers, then we connect the 40 pin GPIO header to the Raspberry Pi. You can now connect the Sense HAT 40 pin GPIO header. Then secure the Sense HAT from the top by screwing screws from the top through the Sense HAT into the spacers. It should look like this:

Image of an AstroPi

Step 3: Update and Upgrade

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, restart the Raspberry Pi to complete the installation:

sudo reboot

Step 4: Create New File

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 have none Raspberry Pi and have Sense HAT, you can use the Sense HAT emulator here or use the built-in Sense HAT emulator for Windows under each step in this project.

A great way to write and test your code intermittently is to use IDLE 3, a development application for Python. 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.

Step 5: Programming (Text Version)

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, turn it over and read a randomly chosen answer. That's why you need a list of answers and a way to randomly choose one and display that answer on the screen.

First of all, 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")

Then there must be a pause before the program responds with an answer, allowing the user to ask a question. You can use the time library to ask the program to wait a certain amount of time, for example like this:

sleep(3)

The program will pause for three seconds. You can adjust this value to increase or decrease the time. Now make a list of answers that 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 called 'number' contains four items. Your list will contain text strings that will be displayed on the screen. These texts 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 if it works by clicking Run and Run Module. You should see “Ask a Question” output in the IDLE 3 shell window.

Step 6: Programming Sense HAT

Now that you have text printing in the Python 3 shell window on your screen, let's modify the code so that the text scrolls over the LED matrix on your Sense HAT. To do this you 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()

Then 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 find that the text moves slowly across the LED matrix Raspberry Pi scrolls. 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("Ask a question", scroll_speed=0.06) sleep(3) answers = ['Signs point to yes', 'Without a doubt', 'You can count on it', 'Don't count on it', 'It looks good', 'Can't predict it now', 'It definitely is', 'Prospect is not so good' ] sense.show_message(choice (answers), scroll_speed=0.06)

Step 7: Shake

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 by using the accelerometer, which measures vibrations and movement. Accelerometers are found in most smartphones that 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 running the part of your program that provides a random 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 you use 4 spaces to indent after a line with a colon. Using abs converts each number to a positive number, meaning it ignores the direction of vibration and looks at the amount of vibration! Now it's time to set a condition in our code that checks if the x, y, and z axes have changed (i.e. it's moved) before it selects a random answer. If no motion is detected, no response will be given.

if x > 2 or y > 2 or z > 2 :

sense.show_message(choice(replies))

else:

sense.clear()

The program checks whether the axes x, y and z are greater than the value 2. By changing this value you can adjust the sensitivity of the program to movement. If you want someone to have the Raspberry Pi and Sense HAT really needs to be shaken 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("ask a question", scroll_speed=0.08) sleep(3) replies = ["YES!", "No", "Maybe" , "Definitely!", "Of course not!", ] 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 was it! Did it not work out? Take a look back at the previous steps to see what went wrong. Did it work? Then take a look at our projects!

The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 5005 reviews.