1

I am trying to change up this function to work with a midi controller instead of buttons most of the code for the function i will be using as is. it just the parts of the code the deal with button that i am finding a bit tricky to translate to work with a midi controller. any advice or suggestions would be a great help. here's the code for the function:

long currentMillis = 0;
long previousMillis = 0;

// Returns the position of Button pressed (0, 1, 2, or 3) or -1 if no button is pressed in the time period.
int waitForButton(int delay)
{
  int buttonPressed = -1;
  int input;
  boolean buttonBackUp = false;

  currentMillis = millis();         // The number of ms since the program started running
  previousMillis = currentMillis;   // Records the point when we start spinning the loop.

  // Keep spinning the loop until "delay" seconds have passed.
  while (currentMillis - previousMillis < delay & buttonBackUp == false)
  {
    // Read the button and record when it has been pushed down.
    for(int pin = 0; pin < 4 & buttonBackUp == false; pin++)
    {
      if(digitalRead(buttons[pin]) == HIGH)
      {
        buttonPressed = pin;

        // Show the LED pushed.
        digitalWrite(leds[pin], HIGH);

        // It is possible the button is still being pushed.
        // This loop spins until the button is let up.
        while (currentMillis - previousMillis < delay & buttonBackUp == false)
        {
          input = digitalRead(buttons[pin]);
          if(input == LOW)
          {
            buttonBackUp = true;
          }
          currentMillis = millis();
        }

        // Turn the LED pushed off.
        digitalWrite(leds[pin], LOW);

        // See if they took to long.
        if(currentMillis - previousMillis > delay)
        {
          buttonPressed = -1; // They took to long to let the button up so they lose.
        }
      }
    }

    currentMillis = millis();
  }

return buttonPressed;
  • How would you like to use MIDI to send commands? Could you elaborate sth more about desired inputs? – mico Apr 17 '17 at 15:49
  • instead of button presses i want it to be for key presses from a midi controller – Hjhk90 Apr 17 '17 at 16:32
  • You get MIDI commands, so instead of reading the current state, you have events. – CL. Apr 17 '17 at 21:03
  • how would i implement the event of a key press substituting into the code of that of the read current state? which midi commands would be best ? – Hjhk90 Apr 17 '17 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.