diff --git a/ArduinoNano33BLE/MidiBle-tilt/MidiBle-tilt.ino b/ArduinoNano33BLE/MidiBle-tilt/MidiBle-tilt.ino new file mode 100644 index 0000000..3766905 --- /dev/null +++ b/ArduinoNano33BLE/MidiBle-tilt/MidiBle-tilt.ino @@ -0,0 +1,106 @@ +#include +#include + +//#include +//#include +//#include +#include + +BLEMIDI_CREATE_DEFAULT_INSTANCE() + +unsigned long t0 = millis(); +bool isConnected = false; + +// accelerometer +float x, y, z; +float mx, my, mz; // midi mapped + + +// ----------------------------------------------------------------------------- +// When BLE connected, LED will turn on (indication that connection was successful) +// When receiving a NoteOn, LED will go out, on NoteOff, light comes back on. +// This is an easy and conveniant way to show that the connection is alive and working. +// ----------------------------------------------------------------------------- +void setup() +{ + Serial.begin(115200); + + Serial.println("MIDI-BLE TEST 1"); + Serial.println("2022-03-02"); + + MIDI.begin(); + + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + BLEMIDI.setHandleConnected([]() { + isConnected = true; + digitalWrite(LED_BUILTIN, HIGH); + Serial.println("MIDI-BLE CONNECTED"); + + }); + + BLEMIDI.setHandleDisconnected([]() { + isConnected = false; + digitalWrite(LED_BUILTIN, LOW); + Serial.println("MIDI-BLE DISCONNECTED"); + + }); + + MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) { + digitalWrite(LED_BUILTIN, LOW); + }); + MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) { + digitalWrite(LED_BUILTIN, HIGH); + }); + + // accelerometer + if (!IMU.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + + Serial.print("Accelerometer sample rate = "); + Serial.print(IMU.accelerationSampleRate()); + Serial.println(" Hz"); + Serial.println(); + Serial.println("Acceleration in G's"); + Serial.println("X\tY\tZ"); + +} + +// ----------------------------------------------------------------------------- +// +// ----------------------------------------------------------------------------- +void loop() +{ + if (IMU.accelerationAvailable()) { + IMU.readAcceleration(x, y, z); + + mx = x * 64 + 64; + my = y * 64 + 64; + mz = max(10, abs(z) * 1000); + + Serial.print(mx); + Serial.print('\t'); + Serial.print(my); +// Serial.print(y); +// Serial.print('\t'); +// Serial.println(z); + Serial.println(); + } + + MIDI.read(); + + + + if (isConnected && (millis() - t0) > mz) + { + t0 = millis(); + + MIDI.sendNoteOn (my, mx, 1); // note 60, velocity 100 on channel 1 + Serial.println("ping"); + delay(mz); + MIDI.sendNoteOff(my, mx, 1); + } +}