Advertisement
pleasedontcode

"ESP32 Bluetooth" rev_02

Jun 18th, 2024
669
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: "ESP32 Bluetooth"
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-18 23:12:20
  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.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <MPU6050.h> // https://github.com/electroniccats/mpu6050
  28. #include <BluetoothSerial.h> // Bluetooth library for ESP32
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void sendMIDI(int control, int value);
  34. int smoothValue(int newValue, int oldValue);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t mpu6050_MPU6050_Interrupt_PIN_D4 = 4;
  38.  
  39. /***** DEFINITION OF I2C PINS *****/
  40. const uint8_t mpu6050_MPU6050_I2C_PIN_SDA_D21 = 21;
  41. const uint8_t mpu6050_MPU6050_I2C_PIN_SCL_D22 = 22;
  42. const uint8_t mpu6050_MPU6050_I2C_SLAVE_ADDRESS = 104;
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  45. MPU6050 mpu(mpu6050_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 with the I2C address
  46. BluetoothSerial SerialBT; // Initialize Bluetooth Serial
  47.  
  48. /****** VARIABLES FOR SMOOTHING *****/
  49. int prevX = 0, prevY = 0, prevZ = 0;
  50.  
  51. void setup(void)
  52. {
  53.     // Initialize Serial for debugging
  54.     Serial.begin(115200);
  55.  
  56.     // Initialize Bluetooth Serial
  57.     SerialBT.begin("ESP32_MPU6050"); // Bluetooth device name
  58.  
  59.     // Initialize I2C communication
  60.     Wire.begin(mpu6050_MPU6050_I2C_PIN_SDA_D21, mpu6050_MPU6050_I2C_PIN_SCL_D22);
  61.  
  62.     // Initialize MPU6050
  63.     mpu.initialize();
  64.  
  65.     // Check if MPU6050 is connected
  66.     if (mpu.testConnection()) {
  67.         Serial.println("MPU6050 connection successful");
  68.     } else {
  69.         Serial.println("MPU6050 connection failed");
  70.     }
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Read raw accelerometer values
  76.     int16_t ax, ay, az;
  77.     mpu.getAcceleration(&ax, &ay, &az);
  78.  
  79.     // Smooth the values
  80.     int smoothedX = smoothValue(ax, prevX);
  81.     int smoothedY = smoothValue(ay, prevY);
  82.     int smoothedZ = smoothValue(az, prevZ);
  83.  
  84.     // Send smoothed values as MIDI CC over Bluetooth
  85.     sendMIDI(1, smoothedX);
  86.     sendMIDI(2, smoothedY);
  87.     sendMIDI(3, smoothedZ);
  88.  
  89.     // Update previous values
  90.     prevX = smoothedX;
  91.     prevY = smoothedY;
  92.     prevZ = smoothedZ;
  93.  
  94.     // Delay to avoid flooding
  95.     delay(100);
  96. }
  97.  
  98. // Function to send MIDI CC messages over Bluetooth
  99. void sendMIDI(int control, int value)
  100. {
  101.     // MIDI CC message format: [status, control, value]
  102.     uint8_t status = 0xB0; // Control Change message
  103.     uint8_t midiMessage[3] = {status, (uint8_t)control, (uint8_t)(value / 128)};
  104.  
  105.     // Send MIDI message over Bluetooth
  106.     SerialBT.write(midiMessage, 3);
  107. }
  108.  
  109. // Function to smooth values to avoid jumps and jitter
  110. int smoothValue(int newValue, int oldValue)
  111. {
  112.     const float alpha = 0.5; // Smoothing factor
  113.     return (int)(alpha * newValue + (1 - alpha) * oldValue);
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement