MIDI out only if analogIn change

Hi all, I try to programe a small Arduino code. It simply should send different MIDI out (NoteOns) in certain ranges of the analogIn data. This works great with if else.... But it loops very fast. I need only one MidiOut (NoteOn), when the analogIn changes (or a new range is achieved).

How can I do this???

I do not want to do it with delay etc...

I tried it with the following code below, but doesn't work...

Thank you very much,


// Variables:

char noteOld = 0; char note = 0; // The MIDI note value to be played int AnalogValue = 0; // value from the analog input

void setup() {

// Set MIDI baud rate: Serial.begin(31250); } void loop() { // potentiometer range from 0 to 1023: AnalogValue = analogRead(0);

if ( AnalogValue > 200 && noteOld != 0x41) { note = 0x41; noteOn(0x90, note, 0x40); note = noteOld; }

else { note = 0x48; noteOn(0x90, note, 0x40); note = noteOld; } } // noteOn(0x90, note, 0x40); void noteOn(char cmd, char data1, char data2) { Serial.print(cmd, BYTE); Serial.print(data1, BYTE); Serial.print(data2, BYTE); }

You want a variable called say LastAnalogueValue. Then say if the absolute value (abs function) of AnalogValue - LastAnalogueValue is greater than some variable called threshold then do your note thing. Here you would see what note you have and change it. Not forgetting to do an old note off MIDI function first.

Then before you leave the loop make LastAnalogueValue = AnalogValue

I'm sure you can actually write the code.

Hi Mike, thank you very much for your reply. Meanwhile I solved the problem my self, but it is exactly the way you suggested. It works fine and I will think about more functions to be integrated.