Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
MrPeanutcrackerful
Published

Pain Game Controller

The main purpose of this controller is to feel pain as you play! Prick yourself with Cacti to move, and scream in pain to jump!

IntermediateShowcase (no instructions)6 hours165
Pain Game Controller

Story

Read more

Code

Pain Game Controller Code

Python
This is Code made in Circuit Python that controls how the Pain Controller works. It uses the sound sensor on the CPX in order to pick up if the player is yelling or not, and converts that into a button press on the keyboard. The Capacitive touch pads on the CPX are connected to the Cacti on the Controller, so when the player pricks themselves with the cacti, the player will move in the corresponding direction.
   # Write your code here :-)

from adafruit_circuitplayground import cp
from adafruit_hid.keyboard import Keyboard
import usb_hid
from adafruit_hid.keycode import Keycode
import time
import array
import math
import audiobusio
import board


def mean(values):
    return sum(values) / len(values)


def normalized_rms(values):
    minbuf = int(mean(values))
    sum_of_samples = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )

    return math.sqrt(sum_of_samples / len(values))


mic = audiobusio.PDMIn(
    board.MICROPHONE_CLOCK,
    board.MICROPHONE_DATA,
    sample_rate=16000,
    bit_depth=16
)
samples = array.array('H', [0] * 160)
mic.record(samples, len(samples))

kbd = Keyboard(usb_hid.devices)
a1_delta = [False, False]
a2_delta = [False, False]
##a3_delta = [False, False]

jumpToggle = False
while True:
    time.sleep(0.2)

    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))
    time.sleep(0.1)

    if cp.touch_A2:
        print("Touched pad A2 - Right")
        kbd.press(Keycode.RIGHT_ARROW)
    if cp.touch_A1:
        print("Touched pad A1 - Left")
        kbd.press(Keycode.LEFT_ARROW)
    if not cp.touch_A1 and not cp.touch_A2 and not (magnitude>500):
        kbd.release_all()
    if magnitude>500:
        print("Scream - Jump")
        jumpToggle = not jumpToggle
    else:
        jumpToggle=False

    if jumpToggle:
        time.sleep(0.01)
        kbd.press(Keycode.SPACE)

Credits

MrPeanutcrackerful
4 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.