/* Use arrays to manage lists of knobs/pots and pushbuttons.
By Leif Oddson
https://forum.pjrc.com/threads/45376
This more complex example demonstrates how to use arrays to
manage a larger number of inputs, without duplicating your
code for every signal.
You must select MIDI from the "Tools > USB Type" menu
This example code is in the public domain.
*/
//************LIBRARIES USED**************
// include the ResponsiveAnalogRead library for analog smoothing
#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
#include <ResponsiveAnalogRead.h>
//usbMIDI.h library is added automatically when code is compiled as a MIDI device
// ******CONSTANT VALUES********
// customize code behaviour here!
const int channel = 1; // MIDI channel
const int A_PINS = 6; // number of Analog PINS
// define the pins you want to use and the CC ID numbers on which to send them..
const int ANALOG_PINS[A_PINS] = {A0};
const int CCID[A_PINS] = {16};
//******VARIABLES***********
// a data array and a lagged copy to tell when MIDI changes are required
byte data[A_PINS];
byte dataLag[A_PINS]; // when lag and new are not the same then update MIDI CC value
//************INITIALIZE LIBRARY OBJECTS**************
// not sure if there is a better way... some way run a setup loop on global array??
// use comment tags to comment out unused portions of array definitions
// initialize the ReponsiveAnalogRead objects
ResponsiveAnalogRead analog[]{
{ANALOG_PINS[0],true},
{ANALOG_PINS[1],true},
{ANALOG_PINS[2],true},
{ANALOG_PINS[3],true},
{ANALOG_PINS[4],true},
{ANALOG_PINS[5],true}/*,
{ANALOG_PINS[6],true},
{ANALOG_PINS[7],true},*/
};
//************SETUP**************
void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
}
//************LOOP**************
void loop() {
getAnalogData();
while (usbMIDI.read()) {
// controllers must call .read() to keep the queue clear even if they are not responding to MIDI
}
}
//************ANALOG SECTION**************
void getAnalogData(){
for (int i=0;i<A_PINS;i++){
// update the ResponsiveAnalogRead object every loop
analog[i].update();
// if the repsonsive value has change, print out 'changed'
if(analog[i].hasChanged()) {
data[i] = analog[i].getValue()>>3;
if (data[i] != dataLag[i]){
dataLag[i] = data[i];
usbMIDI.sendControlChange(CCID[i], data[i], channel);
}
}
}
}