This is part III of a three part post.
Please read Part I QLab Midi Controller for an introduction to the project
and Part II QLab MIDI Controller - The Build for the build.
After a short delay, the current version of the MIDI Controller code is now ready.
TeensyDuino is required in order to use the Arduino IDE, the software is available from the pjrc website here: https://www.pjrc.com/teensy/td_download.html
/* QLab MIDI Controller v2.0 Created: 10 Dec 2015 by Paul Ellison Updated: 25 Feb 2015 - Cleaned up code You must select MIDI from the "Tools > USB Type" menu To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI" */ #include <Bounce.h> // the MIDI channel number to send messages const int channel = 1; int blinkGo = 0; int blinkStop = 0; int goLEDstate = HIGH; int stopLEDstate = HIGH; long previousGoMillis = 0; long previousStopMillis = 0; long goBlinkInt = 200; long stopBlinkInt = 500; int goLED = 2; int stopLED = 3; int nextLED = 4; // Create Bounce objects for each button. The Bounce object // automatically deals with contact chatter or "bounce", and // it makes detecting changes very simple. Bounce goButton = Bounce(10, 5); Bounce stopButton = Bounce(11, 5); Bounce nextButton = Bounce(12, 5); void setup() { // Setup input pins for "active low" pinMode(10, INPUT_PULLUP); pinMode(11, INPUT_PULLUP); pinMode(12, INPUT_PULLUP); // Setup output pins pinMode(goLED, OUTPUT); pinMode(stopLED, OUTPUT); pinMode(nextLED, OUTPUT); // Set initial LED values. digitalWrite(goLED, HIGH); digitalWrite(stopLED, HIGH); } void loop() { // Update all the buttons. goButton.update(); stopButton.update(); nextButton.update(); // Check each button for "falling" edge. // Send a 'MIDI Note On' message when each button is pressed if (goButton.fallingEdge()) { usbMIDI.sendNoteOn(62, 99, channel); // 62 = D4 blinkGo = 4; } if (stopButton.fallingEdge()) { usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4 if (blinkStop == 0) { blinkStop = 6; } else { blinkStop = 0; digitalWrite(stopLED, HIGH); } } if (nextButton.fallingEdge()) { usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4 digitalWrite(nextLED, LOW); } // Check each button for "rising" edge // Send a 'MIDI Note Off' message when each button is released if (goButton.risingEdge()) { usbMIDI.sendNoteOff(62, 0, channel); // 62 = D4 } if (stopButton.risingEdge()) { usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4 } if (nextButton.risingEdge()) { usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4 digitalWrite(nextLED, HIGH); } // MIDI Controllers should discard incoming MIDI messages. // Read more at the following url: // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash while (usbMIDI.read()) { // Ignore incoming messages } unsigned long currentGoMillis = millis(); unsigned long currentStopMillis = millis(); if ((blinkGo > 0) || (goLEDstate == LOW)) { if(currentGoMillis - previousGoMillis > goBlinkInt) { // save the last time you blinked the LED previousGoMillis = currentGoMillis; // if the LED is off turn it on and vice-versa: if (goLEDstate == LOW) { goLEDstate = HIGH; } else { goLEDstate = LOW; } // set the LED with the ledState of the variable: digitalWrite(goLED, goLEDstate); blinkGo--; } } if ((blinkStop > 0) || (stopLEDstate == LOW)) { if(currentStopMillis - previousStopMillis > stopBlinkInt) { // save the last time you blinked the LED previousStopMillis = currentStopMillis; // if the LED is off turn it on and vice-versa: if (stopLEDstate == LOW) { stopLEDstate = HIGH; } else { stopLEDstate = LOW; } // set the LED with the ledState of the variable: digitalWrite(stopLED, stopLEDstate); blinkStop--; } } }
An improved version of the controller is in progress, build and code to follow in an additional post.
Would be interested to hear if anybody has any comments/suggestions/improvement ideas for the code (or project as a whole). Any additions will be credited.