So, we’ve sorted out the problems. They were, for reference:

Seven nasty problems, but almost all of them have been sorted in one way or another, or at least addressed and considered. I will pay attention to the solutions of each in turn below.

1. Cable

This could have easily been the most problematic in a lot of ways. The simple issue was finding hte right cable! I managed to get my hands on a 5-pin DIN to DIN to USB cable, which was perfect, and also was lucky enough to secure a 7 pin female DIN connector, so it was little trouble wiring it up and utilising it.

2. Writing MIDI

Writing MIDI was possibly the largest problem I had encountered up until this point. MIDI data can only be read in a certain way, and thus obviously can only be written using a specific method. It took a fair bit of reading and working with my aforementioned colleague, Jason Gollings, to finally suss out what was needed, and it turned out to be fairly simple. Once you understand the basics, writing MIDI in Arduino is dead simple:

First, just remember that MIDI is made up of three basic things: the Command, the Pitch (also known as Value) and the Velocity. Below, the coding command for Arduino to create MIDI, called SendMidi (), can be seen with 3 pieces of data after it, inside the brackets. The 0x90 is the

Command, and is in hecideximal format. The Command tells the Arduino whether to turn the note on or off. In this case, the 0x90 is telling the Arduino “Turn the note On”. The 0x80 is the same, but tells the Arduino to turn the note off.

The second piece of data is the Pitch, or Value. MIDI understands that the number 45 corresponds to a particular pitch of note, in this case, A. 42 corresponds to F sharp. The note name is after the command, written with a // before it, meaning it is regarded as a comment and ignored, as far as coding is concerned.

Lastly, the BP and BP2 in this case refer to the velocity. Normally, this would be a number, ranging from 0 to 127, but in this case, it is made of two letters. The BP stands for Bellows Pressure, and refers to the pressure coming in from the sensors. This is then scaled to a number between 0 and 127 and applied as Velocity.

Other things to notice from this screengrab: The first part, the “if (BP < oldBP) {” etc, means simply: “if the Bellows Pressure now is smaller than it was a few milleseconds ago, then do the following, if not, move on down the code”. I need this distinction to allow different notes to play from the same button, so long as the Melodeon is moving in different directions.

To complete the code, the following snippet needs to be included at the bottom of the sketch:

To be honest, I’m not sure why, but take it from me, you need it.

3. Reading MIDI

This was, as I discovered later, not as huge a problem as I had at first thought. MIDI requires a BAUD rate of 32500, and Arduino cannot naturally read data at 32500, which simply meant that if I tried to read the data through the Arduino’s Serial.print function I would just get a string of gibberish. This is exactly what did happen to me, until I discovered a program called Midi Monitor, and since then I’ve been able to accurately read MIDI data on the computer, alongside Arduino.

4. Utilising Pressure as Volume

Most of this was covered way, way back in part 2, but not all of it, so I’ll continue about pressure here. The pressure readings I was getting from the sensors consistently gave me measurements of around 6x what I needed. As I said earlier, MIDI notes can only be written as between 0-127 in volume, so a small amount of scaling was needed.

BP = (average / 6); //BP = Bellow Pressure

And that was it!

5. Altering the Pitch based on Pressure

It seems simple on paper, but this was surprisingly tricky to figure out. We should refer back to this screenshot:

As I mentioned earlier, the above code says “if BP is smaller than oldBP, do something, if not, do something else”. One of the hardest things to figure out was obtaining a ‘previous’ rating, the ‘oldBP’ command. I needed to come up with a way to tell the Arduino to remember a previous value and compare it to the current value, in this case, the BP, or the value obtained from inside the bellows. Eventually, I wrote the following piece of code to be placed at the end of the sketch:

What this says, in layman’s, is “wait for one millisecond and then make ‘oldBP’ equal to ‘BP'”. Combined with another delay function at the beginning of the loop, this means the BP value is attributed to oldBP, to be used as desired.

Hopefully, the above image should start to make sense!

6. Inverting the Amplitude

Another problem I mentioned in my last post. Normally, the harder you pull or push the instrument, the louder the note is. The way I had coded my Synthelodeon meant the harder you compressed it, the louder the note, and the harder you drew it, the quieter, which was obviously wrong. The following formula sorted out that problem for me:

BP2 = (((average / 6) *-1) + 187);

The astute pedants amongst you may notice I’ve changed the value to + 187 from +127, and this is because the notes on the draw were not large enough. The ‘average’ command is a reference to the solution for the next problem:

7. Fluctuations

The minor fluctuations in air pressure was something I hadn’t considered until it became a problem. It necessitated the implementation of an average function in the code to allow the velocity of the midi notes to move more gradually, meaning there wouldn’t be hundreds of note changes every second per button. The code was fairly complex, and I have copied it from the Arduino website. It looks like the following:

const int numReadings = 50;

int readings[numReadings]; // the readings from the analog input

int index = 0; // the index of the current reading

int total = 0; // the running total

int average = 0; // the average

// subtract the last reading:

total= total – readings[index];

// read from the sensor:

readings[index] = analogRead(inputPin);

// add the reading to the total:

total= total + readings[index];

// advance to the next position in the array:

index = index + 1;

// if we’re at the end of the array…

if (index >= numReadings)

// …wrap around to the beginning:

index = 0;

// calculate the average:

average = total / numReadings;

// send it to the computer as ASCII digits

BP = average / 6;

BP2 = (((average / 6) *-1) + 187);

There are dozens of comments all overthe code that explain the purpose and funtino much better than I could, so I encourage you to read them yourself, but basically it says: “take 50 readings of the pressure and make a moving average of the lot”. I haven’t quite got this part perfected yet; it’s hard to get a balance between taking too many readings for an average and not enough. Too many slows the response of the instrument and makes the note-changing function lethargic, but not enough readings makes the fluctuations too great. You may be able to see what I mean with the link below (the first clip of the Synthelodeon in action!):

So, a lot of problems, and a lot of solutions! There is still room for improvement, of course, but the end is in sight.

Synthelodeon in action!