midi plugstrip

midi plugstrip

November 9, 2019 DIY / Maker 0

#midi controlled plugstrip for haunted lamps synced to the beat.

code for arduino pro micro (leonardo) midi to relay AC control

#include <MIDIUSB.h>

// Midi relay plugstrip Woz Supposedly of Greasy Conversation - 11-09-19

int vNote;

#define p1 5
#define p2 7
#define p3 14
#define p4 15
#define p5 16

bool r1;
bool r2;
bool r3;
bool r4;
bool r5;

void noteOn(byte channel, byte vNote, byte velocity) {

  if (channel == 15) {
    if (vNote == 36) {  // kick1
      if (r1) {
        r1 = false;
      } else {
        r1 = true;
      }
    }
    if (vNote == 35) {  // snare1
      if (r2) {
        r2 = false;
      } else {
        r2 = true;
      }
    }
    if (vNote == 38) {  // kick2
      if (r3) {
        r3 = false;
      } else {
        r3 = true;
      }
    }
    if (vNote == 40) {  // kick1
      if (r4) {
        r4 = false;
      } else {
        r4 = true;
      }
    }
    if (vNote == 42) {  // hat
      if (r5) {
        r5 = false;
      } else {
        r5 = true;
      }
    }
  } else {    // if not chan 15
    int pick = random(5);
    if (pick == 0) {  // kick1
      if (r1) {
        r1 = false;
      } else {
        r1 = true;
      }
    }
    if (pick == 1) {  // snare1
      if (r2) {
        r2 = false;
      } else {
        r2 = true;
      }
    }
    if (pick == 2) {  // kick2
      if (r3) {
        r3 = false;
      } else {
        r3 = true;
      }
    }
    if (pick == 3) {  // kick1
      if (r4) {
        r4 = false;
      } else {
        r4 = true;
      }
    }
    if (pick == 4) {  // hat
      if (r5) {
        r5 = false;
      } else {
        r5 = true;
      }
    }
  }
}
void checkkeys() {
  midiEventPacket_t rx = MidiUSB.read();

  switch (rx.header) {
    case 0x9:
      noteOn(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //pitch
        rx.byte3         //velocity
      );
      relay();
      break;
    default:
      break;
  }

}   // end checkkeys

void relay() {
  if (r1) {
    digitalWrite(p1, HIGH);
  } else {
    digitalWrite(p1, LOW);
  }
  if (r2) {
    digitalWrite(p2, HIGH);
  } else {
    digitalWrite(p2, LOW);
  }
  if (r3) {
    digitalWrite(p3, HIGH);
  } else {
    digitalWrite(p3, LOW);
  }
  if (r4) {
    digitalWrite(p4, HIGH);
  } else {
    digitalWrite(p4, LOW);
  }
  if (r5) {
    digitalWrite(p5, HIGH);
  } else {
    digitalWrite(p5, LOW);
  }
}


void setup() {

  pinMode(p1, OUTPUT);
  pinMode(p2, OUTPUT);
  pinMode(p3, OUTPUT);
  pinMode(p4, OUTPUT);
  pinMode(p5, OUTPUT);

}


void loop() {
  checkkeys();

}