MIDI Exercise Bike

About a year ago I bought an excercise bike in my quest to get a little more physically active. Suffice to say, it didn’t work. The poor, lonely bike sat beside my desk for the past year gathering dust.

Something about the bike caught my attention during the Christmas break however (yes, I was getting pretty bored). The bike’s computer is connected to the rest of the bike via a jack socket. So what’s the obvious thing to do? That’s right… turn it into a MIDI controller.

After a bit of prodding with a multi-meter I figured out the sensor that controls the onboard ‘computer’ was a simple switch, probably a reed switch, that completes a circuit once per revolution.

With that in mind, I hacked together this circuit on a breadboard:

I actually used an arduino nano, which made the breadboarding a lot easier as I could just shove the whole arduino into the board.

I tested the circuit with a simple bit of code that just reads the switch input (pin 2) and outputs the value to the serial monitor, using the output of that I figured out that the switch is held on for around a quarter of the bike’s revolution.

The switch is connected to pin 2 so we can use an interrupt routine to read the switch. The thing is, what do you record? The switch is either on or off… and we need some sort of signal between 0 and 127 to send as MIDI. The answer was to record the number of milliseconds since the last time the switch was triggered. Here’s the interrupt routine:

void revolution(){
  unsigned long currMillis = millis();
  //Crude debounce
  if(prevMillis != currMillis){
    float diff = currMillis - prevMillis;
    target = (1.0 / diff) * 50000;
    prevMillis = currMillis;
  }
}

The first line of the function stores the current milliseconds since startup. Then we make sure the interrupt routine hasn’t been called before in this millisecond as a really crap debounce. If the current time isn’t the same as the previous time, we calculate the number of milliseconds since the last time revolution() was called. This is usually in the range of ~300 – 800 milliseconds.

Since we want the MIDI signal to go up when we’re cycling faster we need to do a bit of maths on it (yes, maths…). To convert the period, the time between cycles, to a frequency we divide one by the period. To get the MIDI signal into the right range we multiply by 5000. I got 5000 by trial and error, YMMV.

We’ve now got a signal that goes from around 0 to around 127, but it only gets updated around twice a second when you’re cycling at a normal speed (my normal speed’s probably a lot slower than most normal, heathly people… I don’t get out much, hence having a MIDI excercise bike). I smoothed out the signal by using a low-pass filter function I found in the Arduino playground wiki area. This is all done in the loop() function, which is called repeatedly as long as the Arduino is running:

void loop() {
  unsigned long diff = millis() - prevMillis;
 
  currentValue = smooth(target, 0.9, currentValue);
  if((char)currentValue != previousValue){
    midiCC(5, currentValue);
  }
 
  // Timeout
  if(diff > 2000){
    target = 0;
  }
 
  previousValue = currentValue;
  delay(50);
}

In the loop we’re storing the milliseconds since the pedals last went round (for later use), then running the smooth function. This takes three arguments, the first is the value you want to tend towards (the target), the second is the speed of the smoothing (lower is slower, higher is faster), the third is the last value the smoothing function returned. If this value has changed we send it as a midi CC message.

The rest of the loop function sets the target value to zero if the pedals haven’t rotated for more than two seconds. This is so you can’t just stop pedaling and leave the MIDI signal the same, you have to keep pushing!

Here’s a quick demo of it hooked up to Ableton Live, with the CC message mapped to the tempo.

The full code for the Arduino sketch is up on my github account.

3 Comments

  1. Posted August 30, 2010 at 4:38 pm | Permalink

    crikey!
    fine work sir

  2. Posted September 15, 2011 at 2:24 am | Permalink

    I’m still learning from you, while I’m trying to achieve my goals. I absolutely love reading all that is posted on your website.Keep the posts coming. I loved it!

  3. Posted December 18, 2011 at 11:56 pm | Permalink

    I watched your musical bike and am quite impressed. I am pretty much ignorant on the electronics and I am relying on your expertise either for a referral or assistance.

    I have a product that a person can skate on wearing hockey skates or running shoes. (www.stridedeck.ca)
    I wish to add electronics. The only practical solution, since the foot begins and ends at different locations and speed, is to use a contact microphone to capture the vibration of the stride.

    I would like to take this vibration from the piezo (contact microphone) and convert it into musical notes and the skater can hear the music while skating. This way, the user can actually make notes (music) with the exercise of the leg. The amount of force will give the individual notes A, B, C, etc. and the length of the stride will give the note’s length (an eight, quarter, half, whole note).

    I look forward to your response.

One Trackback

  1. By MIDI exercise bike sets the pace | SquareCows on January 14, 2010 at 4:44 am

    [...] Nice – sounds like a great way to enhance a workout! More project info + source code available @ 55th Floatilla. [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*