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.

37 Comments

  1. Posted February 11, 2018 at 2:51 pm | Permalink

    This text is priceless. When can I find out more?

  2. Posted February 11, 2018 at 5:36 pm | Permalink

    Excellent pieces. Keep posting such kind of info on your page. Im really impressed by it.

  3. Posted February 11, 2018 at 7:16 pm | Permalink

    Hi to every body, it’s my first pay a quick visit of this
    web site; this web site consists of remarkable and in fact fine
    material for visitors.

  4. Posted February 11, 2018 at 9:48 pm | Permalink

    You actually make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!|

  5. Posted February 12, 2018 at 3:26 am | Permalink

    I’ve learn a few good stuff here. Certainly price bookmarking for revisiting. I wonder how much attempt you put to make this sort of magnificent informative website.|

  6. Posted February 12, 2018 at 10:21 am | Permalink

    Excellent, what a webpage it is! This web site presents helpful information to us, keep it up.|

  7. Posted February 12, 2018 at 11:18 am | Permalink

    First off I want to say superb blog! I had a quick question in which I’d like to ask if you don’t mind. I was curious to find out how you center yourself and clear your thoughts prior to writing. I have had a hard time clearing my mind in getting my thoughts out there. I do take pleasure in writing but it just seems like the first 10 to 15 minutes are usually lost just trying to figure out how to begin. Any recommendations or tips? Cheers!|

  8. Posted February 12, 2018 at 11:27 am | Permalink

    I’ve learn several just right stuff here. Definitely worth bookmarking for revisiting.
    I wonder how much effort you put to create any
    such wonderful informative site.

  9. Posted February 12, 2018 at 12:47 pm | Permalink

    If if you happen too can onlpy afford one home
    makeover, itt must be painting. Take salt-water
    swimmin popls for example; iit takes a different type of cleaning method.
    If you would invite guests over, they will definitely enjoly it if would be
    considered a pool party.

  10. Posted February 12, 2018 at 2:18 pm | Permalink

    Does your website have a contact page? I’m having trouble
    locating it but, I’d like to shoot you an e-mail. I’ve got some suggestions for
    your blog you might be interested in hearing. Either way, great
    blog and I look forward to seeing it develop over time.

  11. Posted February 12, 2018 at 3:42 pm | Permalink

    Good day I am so glad I found your site, I really found you by mistake, while I was browsing on Google for something else, Anyhow I am here now and would just like to say kudos for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to read it all at the moment but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic work.|

  12. Posted February 12, 2018 at 4:39 pm | Permalink

    Amazing blog! Do you have any recommendations for aspiring writers?
    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go for a paid option? There are so many
    choices out there that I’m completely overwhelmed ..

    Any tips? Cheers!

  13. Posted February 12, 2018 at 10:58 pm | Permalink

    Since the admin of this website is working, no doubt very shortly it will be renowned, due to its feature contents.|

  14. Posted February 13, 2018 at 6:03 am | Permalink

    I take pleasure in, cause I found just what I used to be looking for. You’ve ended my four day long hunt! God Bless you man. Have a nice day. Bye|

  15. Posted February 13, 2018 at 4:43 pm | Permalink

    Greetings from Ohio! I’m bored to tears at work so I decided to check out your website on my iphone during lunch break. I love the info you provide here and can’t wait to take a look when I get home. I’m shocked at how fast your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyhow, amazing site!|

  16. Posted February 13, 2018 at 10:14 pm | Permalink

    At this time I am going away to do my breakfast, afterward having my breakfast coming yet again to read further news.|

  17. Posted February 14, 2018 at 4:01 pm | Permalink

    Woah! I’m really loving the template/theme
    of this blog. It’s simple, yet effective. A lot of times it’s very hard
    to get that “perfect balance” between usability
    and visual appeal. I must say that you’ve done a superb job with this.
    Also, the blog loads extremely fast for me on Safari.
    Exceptional Blog!

  18. Posted February 14, 2018 at 4:36 pm | Permalink

    I’m impressed, I must say. Seldom do I encounter a blog that’s both equally educative and entertaining, and let me tell you, you’ve hit the nail on the head. The problem is an issue that not enough men and women are speaking intelligently about. Now i’m very happy I found this in my search for something relating to this.|

  19. Posted February 14, 2018 at 8:03 pm | Permalink

    Hi are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding knowledge to make your own blog? Any help would be greatly appreciated!|

  20. Posted February 14, 2018 at 11:49 pm | Permalink

    I’m extremely pleased to find this site. I wanted to thank you
    for your time for this wonderful read!! I definitely liked every part of it
    and i also have you saved as a favorite to look at new stuff in your website.

  21. Posted February 15, 2018 at 12:36 am | Permalink

    Awesome site you have here but I was curious about if you knew of any
    user discussion forums that cover the same topics talked about here?

    I’d really like to be a part of community where I
    can get feedback from other knowledgeable people that
    share the same interest. If you have any suggestions, please let me
    know. Kudos!

  22. Posted February 15, 2018 at 3:33 am | Permalink

    I every time spent my half an hour to read this blog’s posts daily along with
    a cup of coffee.

  23. Posted February 15, 2018 at 3:48 am | Permalink

    This is the perfect site for anybody who really wants to find out about this topic.
    You realize so much its almost tough to argue with you
    (not that I really would want to…HaHa). You certainly put a new
    spin on a topic that’s been discussed for decades.
    Great stuff, just great!

  24. Posted February 15, 2018 at 8:13 am | Permalink

    First of all I want to say excellent blog! I had a quick question in which I’d like to ask if you do not mind.

    I was curious to know how you center yourself and
    clear your mind prior to writing. I’ve had trouble clearing my thoughts in getting my ideas out there.

    I truly do enjoy writing but it just seems like the first
    10 to 15 minutes are generally wasted just trying to figure out
    how to begin. Any suggestions or hints? Appreciate it!

  25. Posted February 16, 2018 at 2:02 am | Permalink

    Hey There. I found your weblog the use of msn. That is a very neatly written article. I’ll make sure to bookmark it and return to read extra of your useful info. Thank you for the post. I’ll certainly return.|

  26. Posted February 16, 2018 at 2:42 am | Permalink

    I am regular visitor, how aare you everybody? This poszt posted at this website is acctually pleasant.

  27. Posted February 16, 2018 at 3:13 am | Permalink

    Do you mind if I quote a couple of your articles as long as I provide credit and sources back to
    your blog? My blog site is in the exact same niche
    as yours and my visitors would genuinely benefit from a
    lot of the information you provide here. Please let me know if
    this alright with you. Regards!

  28. Posted February 16, 2018 at 6:51 am | Permalink

    Just wish to say your article is as astounding. The clearness for your put up is simply
    great and that i could suppose you are a professional on this subject.
    Well with your permission allow me to grasp your RSS feed to keep updated with approaching post.
    Thank you one million and please carry on the gratifying work.

  29. Posted February 16, 2018 at 7:40 am | Permalink

    This is a topic which is near to my heart…
    Thank you! Exactly where are your contact details though?

  30. Posted February 16, 2018 at 4:17 pm | Permalink

    I do not even know the way I ended up here, but I assumed this publish was once great. I don’t realize who you are however definitely you’re going to a famous blogger in the event you aren’t already. Cheers!|

  31. Posted February 17, 2018 at 1:09 am | Permalink

    Hey there! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your articles.

    Can you recommend any other blogs/websites/forums that deal
    with the same subjects? Thanks a lot!

  32. Posted February 17, 2018 at 1:17 am | Permalink

    If some one wishes to be updated with most up-to-date technologies then he must be go to see this web site and be up to
    date everyday.

  33. Posted February 17, 2018 at 2:18 am | Permalink

    This is the perfect web site for anyone who really wants to find
    out about this topic. You know so much its almost hard to argue with you (not that I really will need to…HaHa).
    You definitely put a new spin on a subject which has been written about for decades.
    Excellent stuff, just excellent!

  34. Posted February 17, 2018 at 6:35 am | Permalink

    Hey there just wanted to give you a quick heads up.
    The text in your article seem to be running off the screen in Safari.
    I’m not sure if this is a format issue or something to do with
    web browser compatibility but I thought I’d
    post to let you know. The layout look great though!

    Hope you get the problem resolved soon. Cheers

  35. Posted February 18, 2018 at 8:29 pm | Permalink

    You should be a part of a contest for one of the best websites on the internet. I most certainly will recommend this blog!|

  36. Posted February 19, 2018 at 1:54 pm | Permalink

    Peculiar article, totally what I wanted to find.

  37. Posted February 20, 2018 at 6:06 am | Permalink

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with
    something like this. Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

Post a Comment

Your email is never shared. Required fields are marked *

*
*