Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years ago.
USBMIDI and Interrupts
Hello. Is it possible to use simultaneously USB MIDI library and port (switches) interrupts on K64F board? I'm trying to send some midi note by pressing onboard switch.
Here is my not working code:
code
#include "mbed.h" #include "USBMIDI.h" USBMIDI midi; DigitalOut led_red(LED_RED); InterruptIn sw2(SW2); int i = 48; int task = 0; void sw2_release(void) { task=1; } int main() { sw2.rise(&sw2_release); led_red = 1; while (1) { //for(int i=48; i<83; i++) { // send some messages! if(task == 1){ led_red = 0; midi.write(MIDIMessage::NoteOn(i)); wait(0.25); midi.write(MIDIMessage::NoteOff(i)); wait(0.5); led_red = 1; flag=0; } } }
1 Answer
9 years ago.
You need to define task as volatile int. That is always required when you have variables which get modified in an interrupt handler, otherwise the compiler will get a bit too optimization happy.
Hello, thank you for advice but it doesn't solves my problem. For example code below works perfectly. But if you uncomment line 4 (USB MIDI midi;) then interrupt and LED not working and no errors during compilation.
code
#include "mbed.h" #include "USBMIDI.h" //USBMIDI midi; DigitalOut led_red(LED_RED); InterruptIn sw2(SW2); void sw2_release(void) { led_red = !led_red; printf("On-board button SW2 was released.\n"); } int main() { sw2.rise(&sw2_release); while (true) { } }