Advertisement
pleasedontcode

"MIDI Accelerometer" rev_01

Jun 18th, 2024
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "MIDI Accelerometer"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-18 23:09:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* bluetooth connection. converts X, Y Z axes values */
  21.     /* to MIDI CC! Smooth values, no jumps nor jitter */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <MPU6050.h> // https://github.com/electroniccats/mpu6050
  27. #include <BluetoothSerial.h> // Bluetooth library for ESP32
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void sendMIDI(int control, int value);
  33. int smoothValue(int newValue, int oldValue);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t mpu6050_MPU6050_Interrupt_PIN_D4 = 4;
  37.  
  38. /***** DEFINITION OF I2C PINS *****/
  39. const uint8_t mpu6050_MPU6050_I2C_PIN_SDA_D21 = 21;
  40. const uint8_t mpu6050_MPU6050_I2C_PIN_SCL_D22 = 22;
  41. const uint8_t mpu6050_MPU6050_I2C_SLAVE_ADDRESS = 104;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. MPU6050 mpu(mpu6050_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 with the I2C address
  45. BluetoothSerial SerialBT; // Initialize Bluetooth Serial
  46.  
  47. /****** VARIABLES FOR SMOOTHING *****/
  48. int prevX = 0, prevY = 0, prevZ = 0;
  49.  
  50. void setup(void)
  51. {
  52.     // Initialize Serial for debugging
  53.     Serial.begin(115200);
  54.  
  55.     // Initialize Bluetooth Serial
  56.     SerialBT.begin("ESP32_MPU6050"); // Bluetooth device name
  57.  
  58.     // Initialize I2C communication
  59.     Wire.begin(mpu6050_MPU6050_I2C_PIN_SDA_D21, mpu6050_MPU6050_I2C_PIN_SCL_D22);
  60.  
  61.     // Initialize MPU6050
  62.     mpu.initialize();
  63.  
  64.     // Check if MPU6050 is connected
  65.     if (mpu.testConnection()) {
  66.         Serial.println("MPU6050 connection successful");
  67.     } else {
  68.         Serial.println("MPU6050 connection failed");
  69.     }
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // Read raw accelerometer values
  75.     int16_t ax, ay, az;
  76.     mpu.getAcceleration(&ax, &ay, &az);
  77.  
  78.     // Smooth the values
  79.     int smoothedX = smoothValue(ax, prevX);
  80.     int smoothedY = smoothValue(ay, prevY);
  81.     int smoothedZ = smoothValue(az, prevZ);
  82.  
  83.     // Send smoothed values as MIDI CC over Bluetooth
  84.     sendMIDI(1, smoothedX);
  85.     sendMIDI(2, smoothedY);
  86.     sendMIDI(3, smoothedZ);
  87.  
  88.     // Update previous values
  89.     prevX = smoothedX;
  90.     prevY = smoothedY;
  91.     prevZ = smoothedZ;
  92.  
  93.     // Delay to avoid flooding
  94.     delay(100);
  95. }
  96.  
  97. // Function to send MIDI CC messages over Bluetooth
  98. void sendMIDI(int control, int value)
  99. {
  100.     // MIDI CC message format: [status, control, value]
  101.     uint8_t status = 0xB0; // Control Change message
  102.     uint8_t midiMessage[3] = {status, (uint8_t)control, (uint8_t)(value / 128)};
  103.  
  104.     // Send MIDI message over Bluetooth
  105.     SerialBT.write(midiMessage, 3);
  106. }
  107.  
  108. // Function to smooth values to avoid jumps and jitter
  109. int smoothValue(int newValue, int oldValue)
  110. {
  111.     const float alpha = 0.5; // Smoothing factor
  112.     return (int)(alpha * newValue + (1 - alpha) * oldValue);
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement