/* Midi controller made with an Arduino Mega connected to sensors like : buttons, Piezo and Joystick. This code sends midi signals via the USB port in "serial format". You need to install Hairless-midiserial software on Mac or PC to translate that "serial" midi signal to your DAW (Digital Audio Workstation) http://projectgus.github.io/hairless-midiserial/ For Windows users, you will also need to install Loopmidi http://www.tobias-erichsen.de/software/loopmidi.html This example code is in the public domain. */ // variables byte NoteON = 144; // NoteOn command ( cf. protocole midi) http://computermusicresource.com/MIDI.Commands.html byte ControlChange = 176; // Control Change command ( cf. protocole midi) http://computermusicresource.com/MIDI.Commands.html int ButtonPin[] ={22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}; // Buttons Pin numbers (for the Arduino Mega) byte NbButtons = 12; // Number of Buttons int ButtonState[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // variable for reading the pushButton status int PreviousButtonState[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Variable to reccord last button state int FirstPitchButton = 36; // Lowest pitch of all the notes that can be played by the Buttons 36 => C2 int MajorKeyButton[] = {0, 2, 4, 5, 7, 9, 11, 12, 14, 16};// Number of semitones for each note of a major Key int LedButtonPin[] = {34, 35, 36, 37, 38 ,39 ,40, 41, 42, 43, 44, 45}; // LED Pin numbers (for the Arduino Mega) byte NbLeds = NbButtons; // Number of Leds int PiezoPin[] = {A12, A13, A14, A15}; // Piezo Sensors Pin numbers (using Arduino Mega) int NbPiezo = 4; // Number of Piezo sensors int FirstPitchPiezo = 103; // Pitch of the lowest note played by the Piezos 103 => G8 int MajorKeyPiezo[] = {0, 2, 4, 5, 7, 9, 11, 12, 14, 16}; // Number of semitones for each note of a major Key int ThresholdPiezo = 60; // Anything over fifty means we've hit a Piezo int DelayPiezo = 150; // Delay minimum between two knock on a piezo int LedPiezoPin[] = {2, 3, 4, 5}; // Piezo's LED Pin numbers int Joystick[] = {A8,A9,A10, A11}; // Joysticks Pin numbers int NbJoystick = 2; // Number of Joysticks int JoystickPin = 0; // Variable to record the Joystick current value int JoystickValue[] = {0, 0, 0, 0}; // Joystick value mapped between 0 and 127 to be compatible with midi protocol int PreviousJoystickValue[] = {0, 0, 0, 0}; // Variable to record the Joystick last value // functions // This function send a MIDI message via the serial port to the Hairless-midiserial software void MIDImessage(byte NoteOrCmd, byte Pitch, byte velocity) { Serial.write(NoteOrCmd); Serial.write(Pitch); Serial.write(velocity); } // the setup function runs once when you press reset or power the board void setup() { //Serial.begin(31250); // 31250 bps rate if you communicate via a midi shield Serial.begin(9600); // 9600 bps rate if you communicate via USB port (serial) // initialize digital pin i as an output for the Leds for (int i=0; iThresholdPiezo){ MIDImessage(NoteON, PitchPiezo, 127); // Play the midi note digitalWrite(LedPiezoPin[i], HIGH); // Turn on the led delay(DelayPiezo); MIDImessage(NoteON, PitchPiezo, 0); // Turn off the midi note digitalWrite(LedPiezoPin[i],LOW); } }; // End of Piezo sensors part // Joysticks sensors part for (int i=0; i