So, I’ve posted before about setting up my exercise bike in front of a PlayStation. It does two things:

  • Makes my exercise a bit more fun.
  • stops me gaming until 4am like a teenager and thus wrecking my job, relationships and entire life.

I’ve got an alarm on my watch that beeps if my heartrate drops down below target and I’ve been working my way through the best games of, like, 2007.

However, something I’ve always wanted was a system that mapped the speed of the pedals to something in the game. This weekend I got around to putting an old cadence sensor on the spin bike and writing a quick script that took the cadence sensor information and used it to control the keyboard, and thus control a simple online game.

Here’s the proof of concept:

You can see the basic code here and the particular code shown in the video is this one:

from bleak import BleakClient
import asyncio
import viciouscycle
import bleak
import keyboard

def go_forward(sender, data): 
    print("Here")
    cadence=viciouscycle.decode_and_handle_measurement(sender,data) 
    if cadence is None:
        print("None") 
        return
    if cadence > 60: 
        if cadence <150: 
            print("Doing great!")         
            keyboard.hold_key('up',2) 

    else:
        print("No, do better") 



async def play():
    async with BleakClient(viciouscycle.sensor_address) as client:
        print("Start to play") 
        await client.start_notify(viciouscycle.CSC_MEASUREMENT_UUID, go_forward)
        await asyncio.sleep(60)
        await client.stop_notify(viciouscycle.CSC_MEASUREMENT_UUID)
        print("Stopped notifications")

if __name__ == "__main__":
    print("Entering Warm up") 
    print("Seeking Sensor")  
    try: 
        asyncio.run(play())
    except bleak.exc.BleakDeviceNotFoundError: 
        print("Device was NOT found")  

It’s my hope that I’ll come back to this shortly with some more complex code and, much more importantly, a setup where I’ve been able to play some games using the pedals for a few hours. I don’t expect to get as far as Mario Kat 8, but it would be nice to have something with a nice tight feedback loop.

JUNE EDIT: Since trying this I rebuilt the setup so that it will be a lot easier to do things like ‘fit the laptop on the desk’ and so on. I also bought a retrode on the basis that would be a nice way in to doing some bike controlled gaming.