#include #include #include Adafruit_USBD_MIDI usb_midi; MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI); // constants won't change. They're used here to set pin numbers: const int inputPin_push = 1; // the number of the pushbutton pin const int inputPin_switch = 3; // the number of the switchbutton pin const int inputPin_knob = 26; const int ledPin_r = 17; // the number of the red LED pin const int ledPin_g = 16; // the number of the green LED pin const int ledPin_b = 25; // the number of the blue LED pin // variables will change: int inputState_push = 0; // variable for reading the pushbutton status int inputState_switch = 0; int inputState_knob = 0; bool noteOn = false; int velocity = 0; int note = 48; void setup() { Serial.begin(9600); //SETUP LEDs //----------------------------------------------------------------------------- pinMode(ledPin_r, OUTPUT); pinMode(ledPin_g, OUTPUT); pinMode(ledPin_b , OUTPUT); //----------------------------------------------------------------------------- //SETUP MIDI //----------------------------------------------------------------------------- #if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) TinyUSB_Device_Init(0); // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 #endif pinMode(LED_BUILTIN, OUTPUT); MIDI.begin(MIDI_CHANNEL_OMNI); while( !TinyUSBDevice.mounted() ) delay(1); //----------------------------------------------------------------------------- } void loop() { inputState_switch = digitalRead(inputPin_switch); inputState_push = digitalRead(inputPin_push); inputState_knob = analogRead(inputPin_knob)/8; if (inputState_switch == HIGH) { analogWrite(ledPin_g, 120); if (inputState_push == HIGH) { digitalWrite(ledPin_b, LOW); if (noteOn == false) { velocity = 127-inputState_knob; if (velocity < 5) { velocity = 5; } MIDI.sendNoteOn(60, velocity, 1); noteOn = true; } } else { digitalWrite(ledPin_b, HIGH); if (noteOn == true) { MIDI.sendNoteOff(60, 0, 1); noteOn = false; } } analogWrite(ledPin_r, 255-inputState_knob*2); } else { digitalWrite(ledPin_r, HIGH); digitalWrite(ledPin_g, HIGH); digitalWrite(ledPin_b, HIGH); if (noteOn == true) { MIDI.sendNoteOff(60, 0, 1); noteOn = false; } } if (MIDI.read()) { Serial.println("MIDI message received!"); } }