Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <frequencyToNote.h>
- #include <MIDIUSB.h>
- #include <MIDIUSB_Defs.h>
- #include <pitchToFrequency.h>
- #include <pitchToNote.h>
- #include <Keyboard.h>
- // MIDI and keyboard output for Arduino Leonardo
- // Kick Drum and hihat pedals for Paradiddle
- // Discord mic toggle channel mute
- // MIDI code referenced from https://www.arduino.cc/en/Tutorial/MidiDevice
- // define pins
- const uint8_t kick = 2;
- const uint8_t hihat = 8;
- const uint8_t mic = 4;
- const uint8_t mute = 7;
- const byte notePitches[3] = {36, 42, 46}; // will be using MIDI drum notes:
- // 36 kick drum, 42 close hi-hat, 46 open hi-hat
- // set volumes. 64 gives normal volume for MIDI notes
- uint8_t kickVolume = 64;
- uint8_t closehihatVolume = 30;
- uint8_t openhihatVolume = 0; // 64 is too loud in Paradiddle (gives sound of hitting hihat)
- uint8_t pressedPedals = 0x00;
- uint8_t previousPedals = 0x00;
- void setup()
- {
- // make pins an input and use internal resistor (pullup)
- // so it reads high unless connected to ground
- pinMode(kick, INPUT_PULLUP);
- pinMode(hihat, INPUT_PULLUP);
- pinMode(mic, INPUT_PULLUP); // red button (mute mic)
- pinMode(mute, INPUT_PULLUP); // blue button (mute channel)
- Keyboard.begin();
- }
- void loop()
- {
- readPedals();
- readButtons();
- playNotes();
- }
- // First parameter is the event type (0x0B = control change).
- // Second parameter is the event type, combined with the channel.
- // Third parameter is the control number number (0-119).
- // Fourth parameter is the control value (0-127).
- void controlChange(byte channel, byte control, byte value)
- {
- midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
- MidiUSB.sendMIDI(event);
- }
- void readPedals()
- {
- if (digitalRead(kick) == LOW)
- {
- bitWrite(pressedPedals, 0, 1);
- delay(20); // prevents multiple events and feels better
- }
- else
- bitWrite(pressedPedals, 0, 0);
- if (digitalRead(hihat) == LOW)
- {
- bitWrite(pressedPedals, 1, 1);
- }
- else
- bitWrite(pressedPedals, 1, 0);
- }
- void readButtons()
- {
- if(digitalRead(mic)==LOW) // red button: Discord mic mute
- {
- Keyboard.press(KEY_LEFT_CTRL);
- Keyboard.press(KEY_LEFT_SHIFT);
- Keyboard.press(77); // send m key
- delay(100);
- Keyboard.releaseAll();
- delay(300);
- }
- if(digitalRead(mute)==LOW) // blue button: Discord mute channel
- {
- Keyboard.press(KEY_LEFT_CTRL);
- Keyboard.press(KEY_LEFT_SHIFT);
- Keyboard.press(68); // send d key
- delay(100);
- Keyboard.releaseAll();
- delay(300);
- }
- }
- void playNotes()
- {
- if (bitRead(pressedPedals, 0) != bitRead(previousPedals, 0)) // kick pedal
- {
- if (bitRead(pressedPedals, 0))
- {
- bitWrite(previousPedals, 0 , 1);
- noteOn(0, notePitches[0], kickVolume);
- MidiUSB.flush();
- }
- else
- {
- bitWrite(previousPedals, 0 , 0);
- noteOff(0, notePitches[0], 0);
- MidiUSB.flush();
- }
- }
- if (bitRead(pressedPedals, 1) != bitRead(previousPedals, 1)) // hi-hat
- {
- if (bitRead(pressedPedals, 1))
- {
- bitWrite(previousPedals, 1 , 1);
- noteOn(0, notePitches[1], closehihatVolume);
- MidiUSB.flush();
- }
- else
- {
- bitWrite(previousPedals, 1 , 0);
- noteOn(0, notePitches[2], openhihatVolume);
- MidiUSB.flush();
- }
- }
- }
- // First parameter is the event type (0x09 = note on, 0x08 = note off).
- // Second parameter is note-on/note-off, combined with the channel.
- // Channel can be anything between 0-15. Typically reported to the user as 1-16.
- // Third parameter is the note number (48 = middle C).
- // Fourth parameter is the velocity (64 = normal, 127 = fastest).
- void noteOn(byte channel, byte pitch, byte velocity)
- {
- midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
- MidiUSB.sendMIDI(noteOn);
- }
- void noteOff(byte channel, byte pitch, byte velocity)
- {
- midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
- MidiUSB.sendMIDI(noteOff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement