adsense

arduino midi drum code

 Here's an example code for MIDI drum using 8 piezo sensors on an Arduino Uno board:

arduino midi drum diagram


scss
#include <MIDI.h> // Include the MIDI library // Define the piezo sensor pins const int piezoPins[8] = {A0, A1, A2, A3, A4, A5, A6, A7}; // Define the MIDI note numbers for each sensor const int noteNumbers[8] = {36, 38, 41, 43, 46, 48, 50, 51}; // Define the MIDI channel to send on const int midiChannel = 10; // Set the minimum threshold for triggering a note const int threshold = 20; // Create an instance of the MIDI library MIDI_CREATE_DEFAULT_INSTANCE(); void setup() { // Set all piezo pins as inputs for (int i = 0; i < 8; i++) { pinMode(piezoPins[i], INPUT); } // Initialize the MIDI library MIDI.begin(midiChannel); } void loop() { // Read the sensor values int sensorValues[8]; for (int i = 0; i < 8; i++) { sensorValues[i] = analogRead(piezoPins[i]); } // Check if any of the sensors have been triggered for (int i = 0; i < 8; i++) { if (sensorValues[i] > threshold) { // Map the sensor value to a MIDI velocity (0-127) int velocity = map(sensorValues[i], 0, 1023, 0, 127); // Send the MIDI note on message MIDI.sendNoteOn(noteNumbers[i], velocity, midiChannel); // Wait a short time delay(10); // Send the MIDI note off message MIDI.sendNoteOff(noteNumbers[i], 0, midiChannel); } } }

This code reads the analog values from each piezo sensor and maps them to a MIDI velocity value (0-127). If the value is above a certain threshold, a MIDI note on message is sent for the corresponding note number and velocity. After a short delay, a MIDI note off message is sent to stop the note. The code uses the MIDI library to handle the MIDI communication. You will need to connect the piezo sensors to the Arduino Uno board and connect the board to a MIDI device to hear the output.


Post a Comment

0 Comments