For loop function returning an error. Help please!

edited December 2017 in Library Questions

Hi all.

I have a small piece of code that I want to implement into a larger piece of code that I'm working on atm and I'm just testing it out but keep getting "the operator is undefined for the argument type(s) int int" for line 6.

I basically want to have Processing check every 200 milliseconds for overdue midinotes (I'm using this instead of a delay function as the delay function was delaying my entire code and I've been told that this will help stop that)

Please let me know if you want to see the rest of the code for context.

Thanks! :)

     import themidibus.*; 
        MidiBus midi; 

        void setup() {
          int [] timestamps = new int [128];
          for (int i = -1; i < timestamps; i++); 

         midi.sendNoteOn(0, 62, 127);
          timestamps[62] = millis();
          if (mills()>lastMillis+200)

            midi.sendNoteOff(0, 62, 127);
}
Tagged:

Answers

  • edited December 2017

    First thing I see wrong is the semicolon after your for loop. This means that nothing happens inside your loop. Perhaps use brackets instead?

    Next, timestamps is an array. You can't compare i, an int, with an array. Maybe you wanted timestamps.length?

  • edited December 2017

    Edit: beaten to it by tfguy's answer

  • Ah bugger, sorry, I thought I had updated that. I have edited my code now but now I'm getting lastMillis cannot be resolved to a variable. Sorry, I'm quite a noob to all this so apologies if my questions seem stupid!

    import themidibus.*; //Import the library
    MidiBus midi; 
    
    void setup() {
      int [] timestamps = new int [128];
      for (int i = -1; i < timestamps.length; i++)
        println (i);
      midi.sendNoteOn(0, 62, 127);
      timestamps[62] = millis();
      if (millis()>lastMillis+200)
        midi.sendNoteOff(0, 62, 127);
    
  • You have a variable on line 10 called lastMillis, but it's not defined anywhere.

    Maybe add int lastMillis; in above setup()?

  • Just tried that and the code runs but I'm getting NullPointerExpection and no midi data being sent :/

  • Answer ✓

    You aren't initialising midi. You define it but then use it without initialising it.

  • Answer ✓

    (use {} around code blocks even if they are only 1 line long - it's much clearer. In the above do you really mean to just println 128 times? What is the point of that?)

  • Cool, I shall try that and see what happens. And I was printing (i) as I wanted to check to see if it was reading the loop. Thinking about it now, it doesn't really point the loop, just prints 128 times lol.

Sign In or Register to comment.