1

How do i have code wait for an x number of inputs be reached in an array before doing anything else?

int largestindex = 0;
int learnindex=0;

    void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
    learn[learnindex]=pitch;
     learnindex++;
    }

    while(learn[largestindex]==0){
          MIDI.read();
          if (learn[largestindex]!=0){
           break;
              }
2
  • Can you provide some example code of what you have so far?
    – Mazaryk
    Commented Apr 25, 2017 at 15:24
  • Please edit your question to include a Minimal, Complete, and Verifiable example of code, not just snippets. Also link to whatever MIDI library you use. Does MIDI.read(); affect any of the variables mentioned in the snippet? Commented Apr 25, 2017 at 18:30

1 Answer 1

1

If the call to MIDI.read() is a blocking call (it stays in there until it has a value) then you are pretty close:

const int NumberOfInputs (42);
for (int index = 0; index < NumberOfInputs; ++index)
{
    MIDI.read();
}

You can simplify you handler by post incrementing the learnindex counter as you use it. (Post increment will happen after the value has been 'used')

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) 
{
    learn[learnindex++] = pitch;
}

When you increment a variable on its own line its usually better to pre increment it, because you aren't using the value and it might result in quicker execution. However remember you are increasing the value before using it, so if you use it as above you end up accessing the array element one higher than you intended to!

int learnIndex = 0;
++learnindex;            // Pre-increment it to 1
learn[learnIndex++] = 1; // Set element 1 to 1.
learn[++learnIndex] = 3; // Set element 3 to 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.