adsense

Arduino MIDI Controller for Drums Made at Home

 


Arduino midi octapad piezo
  MIDI octapad
  Connect 8 pizzo plate to analog input pins
  
  Use hairless-midiserial software
      MidiYokeSetup
      FL Studio
      

 
*/

#define LED 02

#define PIEZ_CNT 8 
#define DRUM_CHANEL 8

#define ANOTHER_MUTE_PIN_
#define SHOW_

#define CRASH 0 
// #define CRASHC 54
#define PIN_CRASH_STOP 9 
#define OPEN_HAT 90 
//#define OPENING_HAT 200 
#define KANAL_HAT 1 
#define KNOPKA_HAT 100 

// Threshold values for detecting kicks from multiple piezoelectric sensors
int kick_threshold[PIEZ_CNT] = {200, 200, 200, 200, 200, 600, 200, 200};

// Time duration for muting kicks from each sensor
long kick_mute_time[PIEZ_CNT] = {60000, 50000, 50000, 50000, 50000, 50000, 50000, 50000};

// Musical notes corresponding to each piezoelectric sensor
byte kick_note[PIEZ_CNT] = {10, 20, 30, 40, 50, 60, 70, 80};

// Mute status for each sensor (1 for muted, 0 for unmuted)
bool mute[PIEZ_CNT] = {1, 1, 1, 1, 1, 1, 1, 1};

// Analog input pins connected to the piezoelectric sensors
const uint8_t kick_pin[PIEZ_CNT] = {A0, A1, A2, A3, A4, A5, A6, A7};

#ifdef ANOTHER_MUTE_PIN
  // Additional pins for muting, if defined
  const uint8_t stop_pin[PIEZ_CNT] = {A0, A1, A2, A


void noteOn(int channel, int note, int velocity) {
    Serial.write( (0x90 | channel) );
    Serial.write(note);
    Serial.write(velocity);
}

void noteOff(int channel, int note, int velocity) {
    Serial.write( (0x80 | channel) );
    Serial.write(note);
    Serial.write(velocity);
}

void setup() {
    Serial.begin(115200);
    //Serial.begin(31250);
    pinMode(LED, OUTPUT);

    pinMode(PIN_CRASH_STOP, INPUT);
    pinMode(KNOPKA_HAT, INPUT);

    digitalWrite(LED, LOW);
    for (byte i = 0; i < PIEZ_CNT; i++) {
        digitalWrite(kick_pin[i], LOW);
        pinMode(kick_pin[i], INPUT);
#ifdef ANOTHER_MUTE


void setup() {
  // Configure ADC Control Register A to set ADPS2 and clear ADPS1 and ADPS0
  ADCSRA |= (1 << ADPS2);
  ADCSRA &= ~((1 << ADPS1) | (1 << ADPS0));
}

void loop() {
  // Check if CRASH is not muted and the CRASH_STOP pin is LOW
  if (!mute[CRASH] && digitalRead(PIN_CRASH_STOP) == LOW) {
    noteOn(DRUM_CHANNEL, kick_note[CRASH], 0);
  }

  // Decrement piez_idx or reset it to PIEZ_CNT-1
  if (piez_idx > 0) {
    piez_idx--;
  } else {
    piez_idx = PIEZ_CNT - 1;
  }

  // Read analog value from kick_pin[piez_idx]
  int adc = analogRead(kick_pin[piez_idx]);

  // Check if adc value exceeds the threshold
  if (adc > kick_threshold[piez_idx]) {
    if (tm[piez_idx] == 0) {
      tm[piez_idx] = micros() + 2000;
      kick_val[piez_idx] = 0;
      digitalWrite(LED, HIGH);
      tm_led = millis() + 10;
    }
  }

  // Handle LED turning off after a delay
  if (tm_led > 0 && millis() > tm_led) {
    tm_led = 0;
    digitalWrite(LED, LOW);
  }

  // Check if it's time to trigger a note
  if (tm[piez_idx] > 0 && micros() > tm[piez_idx]) {
    if (mute[piez_idx]) {
      mute[piez_idx] = false;
      velocity = 127;

      if (velocity > 127) {
        velocity = 127;
      }

      // Check if piez_idx is KANAL_HAT and the button is pressed
      if (piez_idx == KANAL_HAT && digitalRead(KNOPKA_HAT) == LOW) {
        // Handle special case
      } else {
        noteOn(DRUM_CHANNEL, kick_note[piez_idx], velocity);
      }

      tm[piez_idx] += kick_mute_time[piez_idx];

      // Configure pin mode based on conditional compilation
      #ifdef ANOTHER_MUTE_PIN
        pinMode(stop_pin[piez_idx], OUTPUT);
      #else
        pinMode(kick_pin[piez_idx], OUTPUT);
      #endif

      // Handle optional serial output
      #ifdef SHOW
        cnt++;
        velocity = kick_val[piez_idx];
        Serial.print("IDX=");
        Serial.print(chan);
        Serial.print(" Vel=");
        Serial.print(velocity);
        Serial.print(" cnt=");
        Serial.println(note);
      #endif
    }
  }

  // Update kick_val if adc value is greater
  if (kick_val[piez_idx] < adc) {
    kick_val[piez_idx] = adc;
  }

  // Reset tm[piez_idx] and mute[piez_idx] if it's time
  if (micros() > tm[piez_idx]) {
    tm[piez_idx] = 0;
    mute[piez_idx] = true;

    // Configure pin mode based on conditional compilation
    #ifdef ANOTHER_MUTE_PIN
      pinMode(stop_pin[piez_idx], INPUT);
    #else
      pinMode(kick_pin[piez_idx], INPUT);
    #endif
  }
}

Post a Comment

0 Comments