Prerequisites
You should review the following before moving on:
Setup
Note: you can use any Phidget with an accelerometer for this project.
VINT Hub

Install Pygame Zero
In order to use Pygame Zero, you first have to install it. You do this in the same way you previously installed the Phidget22 library. Simply navigate to your package manager, search for pgzero and press install!
PyCharm
If you're using PyCharm, select File > Settings > Python Interpreter and use the + symbol to install pgzero.
PyScripter
If you're using PyScripter, select Tools > Tools > Install Packages with pip and enter pgzero.
Create Project Structure
Create the following project structure on your computer in the location of your choice.
Download the following files and place them in the appropriate location.
Finally, create a new python file named phidgets_pygame.py and save it in the same location
Write Code (Python)
Copy the code below into phidgets_pygame.py.
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.Accelerometer import *
import pgzrun
alien = Actor('alien')
alien.pos = 100, 56
WIDTH = 500
HEIGHT = alien.height + 20
def draw():
screen.clear()
alien.draw()
def update():
output = mapAcceleration(accelerometer.getAcceleration()[0])
alien.pos = output, HEIGHT/2
#Phidgets Code Start
def mapAcceleration(val):
try:
#for simple tilting, -1 to 1 is an appropriate range
minA = -1
maxA = 1
output = ((val - minA)/(maxA - minA) * WIDTH)
return output
except:
print("error")
#if sensor is out of range, return last position
return alien.pos[1]
#Create, Address, Subscribe to Events and Open
accelerometer = Accelerometer()
accelerometer.openWaitForAttachment(1000)
#Set data interval to minimum | This will increase the data rate from the sensor.
accelerometer.setDataInterval(accelerometer.getMinDataInterval())
#Phidgets Code End
pgzrun.go()
Run Your Program
When you tilt your accelerometer, the alien will also move.
Code Review
As you may have noticed, the program above is based on the program from the Introduction to Pygame Zero. The main difference is that the code to handle mouse clicks has been removed. To replace mouse clicks, the Accelerometer Phidget is used.
When using the Accelerometer Phiget, it's important to set the data interval to the minimum as shown in the code above. This will ensure your sensor is as responsive as possible. In this program, you are controlling the position of the alien with the sensor, so you must map the accelerometer range to the window range. You can do this with the following formula:
Y = (X - A) / (B - A) * (D - C) + C
where:
- A and B are the range of the Accelerometer when tilting (-1 to 1)
- C and D are the range of values you want to map to (0 to WIDTH)
- X is the current value that falls between A and B
- Y is the value you are looking for that falls between C and D
Practice
- Try commenting out the section of your code that sets the data interval. What is the result? The minimum data interval on the Accelerometer Phidget is 20ms meaning your program will get a new value 50 times per second. Try setting the data interval to something slightly higher, like 50, and see if the movement is smoother.
accelerometer.setDataInterval(50)
2. Try to identify when the Accelerometer Phidget is being shaken and place the alien in a random location when this occurs. Review the Dice Roll project for information.