MIDI & switch case calls function twice

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
LeeMartin
 
Posts: 6
Joined: Sat Sep 15, 2018 4:30 pm

MIDI & switch case calls function twice

Post by LeeMartin »

I'm reading incoming MIDI notes then calling a function when notes 45 or 55 are received. However, when each note is received, the function is called twice. I'm getting 2 calls for each case, one almost immediately after the other.

Can anyone see any problems with the code? I'm using a Metro M4.

Code: Select all

#include <frequencyToNote.h>
#include <MIDIUSB.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

byte rxVar;

void moveStepper() {
  Serial.println("*** Move stepper ***");  
}

void setup() {
  Serial.begin(115200);
  #define SerialUSB Serial
  while(!SerialUSB && millis()<5000) {
  //wait for USB serial to connect or 5 seconds to elapse
  } 
}

void loop() {
  
    midiEventPacket_t rx = MidiUSB.read();;
    rxVar = rx.byte2;

    switch (rxVar) {
    case 45:            //Note On message
        moveStepper();
        Serial.print("note1 = ");
        Serial.println(rxVar);
      break;
    case 55:            //Note On message
        moveStepper();
        Serial.print("note2 = ");
        Serial.println(rxVar);
      break;      
    default:        
        // do nothing
      break;
    }
}


User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: MIDI & switch case calls function twice

Post by adafruit_support_bill »

What is the serial output when that happens?

User avatar
LeeMartin
 
Posts: 6
Joined: Sat Sep 15, 2018 4:30 pm

Re: MIDI & switch case calls function twice

Post by LeeMartin »

It was being triggered by both the note on and note off messages.

Fixed now - this code works:

Code: Select all

#include <MIDIUSB.h>
#include <frequencyToNote.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

int midiVar;

void moveStepper1() {
  Serial.println("*** Move stepper 1 ***");
}

void moveStepper2() {
  Serial.println("*** Move stepper 2 ***");
}

void setup() {
  Serial.begin(115200);
#define SerialUSB Serial
  while (!SerialUSB || millis() < 5000) {
    //wait for USB serial to connect or 5 seconds to elapse
  }
}

void loop() {

  midiEventPacket_t rx;

  do {

    rx = MidiUSB.read();

    if (rx.byte2 != 0) {

      midiVar = (rx.byte1 + rx.byte2); // create unique number from on/off + note

      switch (midiVar) {
        case 189:            //Note 45
          moveStepper1();
          break;
        case 199:            //Note 55
          moveStepper2();
          break;
        default:
          // do nothing
          break;
      }
    }
  }
  while (rx.header != 0);
}

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: MIDI & switch case calls function twice

Post by adafruit_support_bill »

Good to see that you found a solution. Thanks for the follow-up.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”