|
|
Raspberry pi pico |
x 1 | |
|
|
Mpu6050 |
x 1 | |
|
|
Jumper Cables |
x 4 |
Controlling Games With you hand
I had gotten the Rapberry pi pico a few weeks ago and I was experimenting with it. On one fine day I was learning how to use Mpu6050 with Pico and circuit python and control my mouse with it. Then while just messing around, I thought why not try to control a game? I thought asphalt 8 would be ideal as it only needs five controls ( front, back, left, right and nitro).
I did all the connections and now the cable connecting pico and mpu6050 was too short. So I made the wire longer and I had an old Mi Band lying around which was not working. So I placed the mpu6050 inside the band and used it as a wearable.
Here is a video of it working:
you can till your hand left and right to steer the car. Tilt it Forward and Backward to control the direction of the car (front and back) and flick your hand towards the top for Nitro.
the video is also posted on reddit by the username : u/hriday746 : which is me
the video is also on a blog : hridaybarot.home.blog : which is also me
the video is also on instagram by the name: friday746 : which is me
and also on youtube.
for a more detailed guide with the code, visit: https://hridaybarot.home.blog/2021/03/23/controlling-asphalt-8-with-hand-gestures-using-mpu6050-and-raspberry-pi-pico/
import time
from math import atan2, degrees
import board
import busio
import adafruit_mpu6050
from adafruit_hid.keyboard import Keyboard as kbd
from adafruit_hid.keycode import Keycode
i2c = busio.I2C(board.GP15, board.GP14)
sensor = adafruit_mpu6050.MPU6050(i2c)
import usb_hid
Keyboard=kbd(usb_hid.devices)
kw=Keycode.W
ka=Keycode.A
ks=Keycode.S
kd=Keycode.D
k_nitro=Keycode.SPACE
# Given a point (x, y) return the angle of that point relative to x axis.
# Returns: angle in degrees
def vector_2_degrees(x, y,l=False):
if l:
angle = degrees(atan2(y, x))
else:
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle
# Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z
# Returns: tuple containing the two angles in degrees
t=""
f=""
def get_inclination(_sensor):
x, y, z = _sensor.acceleration
return vector_2_degrees(x, z,True), vector_2_degrees(y, z), int(x),int(y),int(z)
x,y,z=0,0,0
def press(key):
Keyboard.press(key)
def release(key):
Keyboard.release(key)
turn=None
ac=None
toA=None
#l=[None,None]
while True:
try:
turn, ac,x,y,z = get_inclination(sensor)
turn=int(turn)
ac=int(ac)
except:
pass
#print(turn,ac,x,y,z,sep=" : ")
if turn<50:
if t=="r":
Keyboard.release(kd) #leave r
print("left")
Keyboard.press(ka)
#l[0]=ka
t="l"
elif turn>130:
if t=="l":
Keyboard.release(ka) #leave l
print("right")
Keyboard.press(kd)
#l[0]=kd
t="r"
else:
if t=="l":
Keyboard.release(ka) #leave l
elif t=="r":
Keyboard.release(kd) #leave r
l[0]=None
if ac<50:
if f=="f":
Keyboard.release(kw) # leave f
print("brake")
Keyboard.press(ks)
#toA=ks
f="b"
elif ac>120:
if f=="b":
Keyboard.release(ks) #leave b
print("accel")
Keyboard.press(kw)
#toA=kw
f="f"
else:
if f=="f":
Keyboard.release(kw) # leave f
elif f=="b":
Keyboard.release(ks) #leave b
if z >=11:
print("nitro")
Keyboard.send(k_nitro)
time.sleep(0.000001)
Controlling Games With you hand
- Comments(0)
- Likes(1)
-
MXKA03
Apr 21,2021
- 1 USER VOTES
- YOUR VOTE 0.00 0.00
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
9design
-
9usability
-
10creativity
-
10content
More by Hriday Barot
-
-
AEL-2011 Power Supply Module
320 0 1 -
AEL-2011 50W Power Amplifier
294 0 1 -
-
-
Custom Mechanical Keyboard
563 0 0 -
Tester for Touch Screen Digitizer without using microcontroller
228 2 2 -
Audio reactive glow LED wristband/bracelet with NFC / RFID-Tags
233 0 1 -
-
-







