Learn creative coding writing simple programs

124. Controlling Processing using MidiBus, part 2

In part 1 we made a program that draws lines or circles when receiving control change MIDI messages. This first approach has an important limitation: it only draws when it receives messages. If you stop moving your knobs or sliders, it does not draw anything.

Imagine you use two knobs to move a circle horizontally and vertically. You probably want your circle to stay visible on the display even if you are not moving your knobs. This means we need a way to be able to read the last value the knob had. The solution is simple: every time we receive a Control Change message, we store the value in an array. This allows us to read the knob value any time, not just during the instant it is received.

We could use an array of integers to store these values, as the value by default is a number between 0 and 127. But I think most of the time it's more practical to work with numbers between 0 and 1, that's the reason I used a float array. Then, every time we receive a message, we map the message value from the range 0..127 to 0..1.

What's the advantage of numbers between 0 and 1? They are very easy to map into other ranges! To map a number between 0 and 1 into numbers between 0 and 400 you just have to multiply the number by 400. No need to use map().

In this episode we write a program that draws a circle. This circle can be controlled using a MIDI controller (position, size, color and oscillation speed).

If you pay attention, you will note that apparently I can't tell the difference between red and blue :)

Tags: midi, controlchange, midibus, controller

Code editor

You can make changes to the code below. Then
Questions and comments are welcome! :) But please try to stay close to the topic of this episode. Use the Processing forums for help with your Processing projects (or hire me for help ;-)