Skip to content

Instantly share code, notes, and snippets.

@RyoKosaka
Last active April 21, 2024 01:18
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RyoKosaka/1f488427e02054422966fb9625c9f8e5 to your computer and use it in GitHub Desktop.
Save RyoKosaka/1f488427e02054422966fb9625c9f8e5 to your computer and use it in GitHub Desktop.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
uint8_t note = 38;
int SNARE[6] = {150, 4000, 38, 3, 0, 0}; //{threshold, sensitivity, note(no use), flag, velocity, last peakValue}
boolean snareFlag = false;
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
#define MIDI_SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
#define MIDI_CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
uint8_t midiPacket[] = {
0x80, // header
0x80, // timestamp, not implemented
0x00, // status
0x3c, // 0x3c == 60 == middle c
0x00 // velocity
};
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MIDI"); //Device Name
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEDevice::setEncryptionLevel((esp_ble_sec_act_t)ESP_LE_AUTH_REQ_SC_BOND);
// Create the BLE Service
BLEService *pService = pServer->createService(BLEUUID(MIDI_SERVICE_UUID));
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
BLEUUID(MIDI_CHARACTERISTIC_UUID),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR
);
pCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND);
pSecurity->setCapability(ESP_IO_CAP_NONE);
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
pServer->getAdvertising()->addServiceUUID(MIDI_SERVICE_UUID);
pServer->getAdvertising()->start();
}
void loop() {
if (deviceConnected) {
//Sensing Start
int piezoValue = analogRead(33);
if (piezoValue > SNARE[0] && snareFlag == false) {
for (int i = 0; i < 4; i++) {
int peak = analogRead(33);
delay(1);
if (peak > SNARE[4]) {
SNARE[4] = peak;
}
}
SNARE[5] = SNARE[4];
SNARE[4] = map(SNARE[4], SNARE[0], SNARE[1], 0, 127);
if (SNARE[4] <= 1) {
SNARE[4] = 1;
}
if (SNARE[4] > 127) {
SNARE[4] = 127;
}
//Sensing Done
// noteOn
midiPacket[2] = 0x90; // note down, channel 0
midiPacket[3] = note; //snare note is 38
midiPacket[4] = SNARE[4]; // velocity
pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes
pCharacteristic->notify();
// noteOff
midiPacket[2] = 0x80; // note up, channel 0
midiPacket[3] = note; //snare note is 38
midiPacket[4] = 0; // velocity
pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes)
pCharacteristic->notify();
snareFlag = true;
}
//Canceling Retrigger
if (piezoValue < (SNARE[5] * (0.01 * SNARE[3])) && snareFlag == true) {
delay(3); //mask time
int sensorValue0 = analogRead(33);
if (sensorValue0 < (SNARE[5] * (0.01 * SNARE[3]))) {
snareFlag = false;
}
}
}
}
@SergiofBeltran
Copy link

Hi there, this code works fine for me.
How i add another piezo in this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment