Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "ESP32 Bluetooth"
- - Source Code compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-06-18 23:12:20
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* bluetooth connection. converts X, Y Z axes values */
- /* to MIDI CC! Smooth values, no jumps nor jitter */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <MPU6050.h> // https://github.com/electroniccats/mpu6050
- #include <BluetoothSerial.h> // Bluetooth library for ESP32
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void sendMIDI(int control, int value);
- int smoothValue(int newValue, int oldValue);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t mpu6050_MPU6050_Interrupt_PIN_D4 = 4;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t mpu6050_MPU6050_I2C_PIN_SDA_D21 = 21;
- const uint8_t mpu6050_MPU6050_I2C_PIN_SCL_D22 = 22;
- const uint8_t mpu6050_MPU6050_I2C_SLAVE_ADDRESS = 104;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- MPU6050 mpu(mpu6050_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 with the I2C address
- BluetoothSerial SerialBT; // Initialize Bluetooth Serial
- /****** VARIABLES FOR SMOOTHING *****/
- int prevX = 0, prevY = 0, prevZ = 0;
- void setup(void)
- {
- // Initialize Serial for debugging
- Serial.begin(115200);
- // Initialize Bluetooth Serial
- SerialBT.begin("ESP32_MPU6050"); // Bluetooth device name
- // Initialize I2C communication
- Wire.begin(mpu6050_MPU6050_I2C_PIN_SDA_D21, mpu6050_MPU6050_I2C_PIN_SCL_D22);
- // Initialize MPU6050
- mpu.initialize();
- // Check if MPU6050 is connected
- if (mpu.testConnection()) {
- Serial.println("MPU6050 connection successful");
- } else {
- Serial.println("MPU6050 connection failed");
- }
- }
- void loop(void)
- {
- // Read raw accelerometer values
- int16_t ax, ay, az;
- mpu.getAcceleration(&ax, &ay, &az);
- // Smooth the values
- int smoothedX = smoothValue(ax, prevX);
- int smoothedY = smoothValue(ay, prevY);
- int smoothedZ = smoothValue(az, prevZ);
- // Send smoothed values as MIDI CC over Bluetooth
- sendMIDI(1, smoothedX);
- sendMIDI(2, smoothedY);
- sendMIDI(3, smoothedZ);
- // Update previous values
- prevX = smoothedX;
- prevY = smoothedY;
- prevZ = smoothedZ;
- // Delay to avoid flooding
- delay(100);
- }
- // Function to send MIDI CC messages over Bluetooth
- void sendMIDI(int control, int value)
- {
- // MIDI CC message format: [status, control, value]
- uint8_t status = 0xB0; // Control Change message
- uint8_t midiMessage[3] = {status, (uint8_t)control, (uint8_t)(value / 128)};
- // Send MIDI message over Bluetooth
- SerialBT.write(midiMessage, 3);
- }
- // Function to smooth values to avoid jumps and jitter
- int smoothValue(int newValue, int oldValue)
- {
- const float alpha = 0.5; // Smoothing factor
- return (int)(alpha * newValue + (1 - alpha) * oldValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement