Exercise Machine Controlled Video

Photo of Espruino

Made by Espruino

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

Hardware components

Oscilloscope Probe Oscilloscope Probe x 1
Cross Trainer Cross Trainer x 1
Puck.js Puck.js x 1

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:


  1. var controls = require("ble_hid_controls");
  2. NRF.setServices(undefined, { hid : controls.report });
  3.  
  4. var rotations = 0;
  5. var lastRotations = 0;
  6. var wasPlaying = false;
  7.  
  8. var PLAYSPEED = 5;
  9.  
  10. setWatch(function(e) {
  11. digitalPulse(LED3,1,10);
  12. rotations++;
  13. }, D1, { edge:"falling",repeat:true});
  14.  
  15. function setPlaying(play) {
  16. if (play!=wasPlaying) {
  17. wasPlaying = play;
  18. // flash a LED
  19. if (play) {
  20. // green = go
  21. digitalPulse(LED2,1,100);
  22. } else {
  23. // red = stop
  24. digitalPulse(LED1,1,100);
  25. }
  26. // Call playpause to toggle beteen play and stop
  27. // Catch any exceptions thrown here in case HID
  28. // wasn't enabled
  29. try { controls.playpause(); } catch (e) { }
  30. }
  31. }
  32.  
  33.  
  34. NRF.on('connect', function(addr) {
  35. setInterval(function() {
  36. if (rotations >= PLAYSPEED &&
  37. lastRotations >= PLAYSPEED) {
  38. // if both the last time periods have been fast enough
  39. // start playing
  40. setPlaying(true);
  41. } else if (rotations < PLAYSPEED &&
  42. lastRotations < PLAYSPEED) {
  43. // else if both were too slow, stop playing
  44. setPlaying(false);
  45. }
  46. lastRotations = rotations;
  47. rotations = 0;
  48. }, 2000);
  49. });
  50.  
  51. NRF.on('disconnect', function() {
  52. // Remove any intervals
  53. clearInterval();
  54. });

Code

Source code

Credits

Photo of Espruino

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...