Exercise Machine Controlled Video
About the project
In this video I show you how to rig up a cross trainer or other exercise machine to play or pause video depending on whether is it used or not. It uses Puck.js's Bluetooth LE HID capability.
Project info
Difficulty: Easy
Estimated time: 4 hours
Items used in this project
Story
The method of connecting to the trainer here could be used for all kinds of things - for instance you could easily log your usage of the trainer throughout a whole training session and then download it to your phone or PC afterwards.
Wiring
I used an oscilloscope to look at the signal on the wire from the trainer. However if you don't have one, just use a volt meter and move the exercise machine very slowly.
The important thing is to get the polarity of the wiring correct. All you need to do is make sure that when the meter is across the wire, it reads a positive (not negative) voltage.
If the voltage across the wire is higher than 3v, ask on the forums - you may need some extra work to safely connect to Puck.js without damaging it.
Software
The code used in the video is:
- var controls = require("ble_hid_controls");
- NRF.setServices(undefined, { hid : controls.report });
- var rotations = 0;
- var lastRotations = 0;
- var wasPlaying = false;
- var PLAYSPEED = 5;
- setWatch(function(e) {
- digitalPulse(LED3,1,10);
- rotations++;
- }, D1, { edge:"falling",repeat:true});
- function setPlaying(play) {
- if (play!=wasPlaying) {
- wasPlaying = play;
- // flash a LED
- if (play) {
- // green = go
- digitalPulse(LED2,1,100);
- } else {
- // red = stop
- digitalPulse(LED1,1,100);
- }
- // Call playpause to toggle beteen play and stop
- // Catch any exceptions thrown here in case HID
- // wasn't enabled
- try { controls.playpause(); } catch (e) { }
- }
- }
- NRF.on('connect', function(addr) {
- setInterval(function() {
- if (rotations >= PLAYSPEED &&
- lastRotations >= PLAYSPEED) {
- // if both the last time periods have been fast enough
- // start playing
- setPlaying(true);
- } else if (rotations < PLAYSPEED &&
- lastRotations < PLAYSPEED) {
- // else if both were too slow, stop playing
- setPlaying(false);
- }
- lastRotations = rotations;
- rotations = 0;
- }, 2000);
- });
- NRF.on('disconnect', function() {
- // Remove any intervals
- clearInterval();
- });
Code
Credits
Espruino
Espruino, Espruino Pico and Puck.js are low-power Microcontrollers that run JavaScript. Espruino is a JavaScript Interpreter for Microcontrollers that is designed to make development quick and easy. The Espruino interpreter is firmware that runs on a variety of different microcontrollers, but we also make Espruino Boards that come with the interpreter pre-installed and are the easiest devices to get started with. However Espruino itself isn't just the interpreter firmware or hardware - there's also the Web IDE, command-line tools, documentation, tutorials, and modules that form a complete solution for embedded software development.
Leave your feedback...