I'm hoping someone might be able to help me with a problem i am having. I am not the greatest at coding in the world, and you guys have been marvellous in the past and i thank you in advance for any assistance you can give. I have been banging my head against the wall for some time.

I'm doing an explenation of MIDI and CV for those who don't use it . just in case someone can help

The project I am working on involves a Midi to Control Voltage / Gate Voltage convertor which controls an old Synthesiser. Basically you play a note on a keyboard which is Midi equipped this goes into the Arduino via a midi circuit. When you play a note i am using the Library which allows me to convert the Midi note number to a voltage using a DAC + circuit . This is feed into the synth CV input which controls the pitch. The library also allows me to convert a Note On message into a Gate Voltage which is feed into the synth this triggers an envelope which allows me to hear the note, and when a Note Off message is sent the envelope closes and therefore the note is muted (I am using digital out 8 for this task).

There are two types of midi Note Off message a traditional Note Off message and a Note Off message which is actually a Note On with zero velocity. Both can be handled by the library but my problem is concerned with the Note Off with zero velocity. I am not going through any Computer sequencer package but directly from my Midi Keyboard into the Arduino which sends out midi using the Note Off's with zero velocity standard.

The code I am posting is a stripped down version of a much larger program I have compressed the code for the DAC to make it easier to scroll and the DAC is not a problem.

So what is the problem well actually everything works. When you play a note the dac sends out a voltage the Gate opens this triggers the synth and all is hunky dory.what I am trying to introduce is the following: (allow the overlap of notes)

When you press a note down the gate opens and a note is heard .If you release the note the gate shuts and the note is muted (great)If you do the above again but this time because of the speed you are playing (for example) you hit a second note whilst still holding down the first note, the pitch changes which is great but when you release the key on the first note it shuts down the gate and obviously cuts of the second note. (not great)So i thought to myself i would try using an array when a note is hit firstly it moves what is in position 0 of the array to position 1, then it puts a number in position 0. (thought I had it)So i thought when the Note of command is sent it would clear the array. because i thought the Note Off command follows the note on command. If it worked how i wanted it to if two notes were pressed i would have an array that i could sum and use that number if it was over 19 in this case (usually it would be 10) then i could say don't shut the gate till the second note is released.

But obviously I have found that it doesn't work like that and the note off command as it says on the tin…. is a new note so my array is always summed at twenty

I really hope that makes senseI'm totally out of ideas ……..please can you help…….thanks for reading

here is where i'm up to For those who know the library I have engaged the note off as zero velocity in the settingsthere is two if's wrapped around the DAC voltage as I'm using different Zones in the larger program.

I'm really sorry if my code looks big I can't reduce the font size (i'm not having a good week)

if (velocity == 0){digitalWrite(led,LOW);}//clear array code was in here

I suppose led is the gate output. And of course, if you release any of the keys you pressed, it'll make it go off, even if you're holdinganother note, because MIDI is serial. An easy escape is, make two counters, one that sums one when a note on is receivedand another one that sums one when a noteoff is received. So, before bringing the gate output low, compare the two countersand if they're the same, the gate goes off.For example, you play three note onnote on -> counter 1note on -> counter 2note on - > counter 3we have the counter at 3 here, now with the note offnote off -> counter2 1note off -> counter2 2note off -> counter2 3now if they're the same, gate off. This way, the gate will go low when you release ALL the keys, like it should be. So you can playlegato

I thank capicoso for his wisdom and reply and the approach to the code did indeed work.

I have again been banging my head against a wall for a week nowto try and improve this approach.

with the code which has been implemented using capicoso approach

if i play a note say C (all great plays a C)then while C is still pressed play note D (all is great plays a D)if i then release D while C is still pressed I would like it to go back to playing a Cat the moment it still plays a D

its like i want to achieve a current status on the current note

I hoping someone might be able to unlock me going round and round in circles

I didn't remember it was for a midi 2 cv project.First you should auto format your sketch, I can't follow the lines, very hard to read.what is this for?if (noteOnCounter == 4){noteOnCounter = 0;}i would make the counters 0 when they're equal.You can use arrays and store let's say, the last 4 notes. And when a noteoff is sent, read the note before that one. If you're holding more than two keys, well, which one will be played when the last one is released? You can choose to do it by pitch order, for example:I play legato in this order, 60, 66, 70. I have them all pressed. So they're stored in array pos 0, 1 and 2. If the last one is released, read array position 1, and so on. I'm not sure how older keyboards used to work, for example, if the latest key was 66(the middle) i'm not sure which should play next, if 60, or 70. You can do it by order of pitch, or order of time. I would do it with pitch