Here's the entire program. I'm using a repurposed joystick and some knobs to control pitchbend, modulation, cutoff, resonance and LFO speed. It is really odd, I've re-ordered/edited the array of analog ins, pretty sure that any 3 will work, but a 4th one won't.
int Modulation=1;
// Define Moog first in arrays
int Res_grp[2]={21,30};
int Cut_grp[2]={51,29};
int LFO_grp[2]={3,16};
int Resonance,Cutoff,LFO;
int Mchan[2]={0,3};
// #define Moog_LFO 3
// #define DM_Res 30
// #define DM_Cutoff 29
// #define DM_LFO 16
#define Pitchbend 2
// 2 LEDs
// 1 joystick
// 1 switches - select device
// 2 extra Knobs
//int midiByte;
//int MIDIchannel = 1;
int channel;
int status=0;
int statusType;
int statusTest;
int runningStatus;
int Flag;
int runStatusFlag=0;
int realTimeTest;
int count;
int ledPin[2]={10,11}; // select the pin for the LED
int switchState;
int lightState[2]={0,0};
int currentSwitch;
int currentPot[4] ={0,0,0,0};
int i,n,val,t;
int potPin[4]={A0,A1,A2,A3};
int switchPin=9;
int pots_now=0;
int pastPot[4]={0,0,0,0};
byte midiByte;
byte MIDIchannel;
byte x;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
digitalWrite(2, HIGH); // +5 Volt supply to opto-coupler
//pinMode(4, INPUT); // Set Inputs for 4 way DIP Switch
//pinMode(5, INPUT);
//pinMode(6, INPUT);
//pinMode(7, INPUT);
//digitalWrite(4, HIGH); // Set inputs Pull-up resistors High
//digitalWrite(5, HIGH);
//digitalWrite(6, HIGH);
//digitalWrite(7, HIGH);
pinMode(switchPin, INPUT_PULLUP);
// pinMode(switchPin, INPUT);
for(n=0; n<2; n++){
pinMode(ledPin[n], OUTPUT);
}
Serial.begin(31250); // set MIDI baud rate
//Serial.begin(9600);
Serial.flush();
//Serial.println("Let's Get this started-----------------------------");
}
void loop() {
// Read 4-way DIP switch
// MIDIchannel=digitalRead(4) + (digitalRead(5)<<1) + (digitalRead(6)<<2) + (digitalRead(7)<<3);
// midiThru();
currentSwitch = digitalRead(switchPin);
if( currentSwitch == LOW && switchState == LOW ){
setMidi(1);
// LED = Yellow switchState=currentSwitch;
}
if( currentSwitch == LOW && switchState == HIGH ){
setMidi(1);
}
if( currentSwitch == HIGH && switchState == LOW ){
setMidi(2);
}
switchState=currentSwitch;
for(n=0; n<5; n++){
currentPot[n] = analogRead(potPin[n]);
currentPot[n] = analogRead(potPin[n]);
if(pastPot[n] < (currentPot[n]-4) || pastPot[n] > (currentPot[n]+4) ) {
freeMode(n,currentPot[n]);
}
pastPot[n]=currentPot[n];
delay (10);
}
}
//++++++++++++++++++++++Functions++++++++++++++++++++++++++++++++++++++++
void midiThru(){
if (Serial.available() > 0) {
midiByte = Serial.read();
}
}
// Send a MIDI control message.
void control(byte channel, byte control, byte range) {
midiMsg( (0xB0 | channel), control, range);
}
void pitchBend(byte range){
int PB =map(range, 81,968,-8000, 8000);
byte change = 0x2000+PB;
byte low = change &0x7F;
byte high = (change >>7)&0x7F;
midiMsg(0xE0,low,high);
}
// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
Serial.write(cmd);
Serial.write(data1);
Serial.write(data2);
//Serial.println(cmd);
//Serial.println(data1);
//Serial.println(data2);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setMidi(byte select) {
int t=0;
switch (select) {
case 1:
// release
LFO=LFO_grp[t];
Resonance =Res_grp[t];
Cutoff=Cut_grp[t];
digitalWrite(ledPin[0], HIGH);
digitalWrite(ledPin[1], LOW);
MIDIchannel=Mchan[t];
break;
case 2:
// release
t =1;
LFO=LFO_grp[t];
Resonance =Res_grp[t];
Cutoff=Cut_grp[t];
digitalWrite(ledPin[1], HIGH);
digitalWrite(ledPin[0], LOW);
MIDIchannel=Mchan[t];
break;
}
}
void freeMode(byte select, int range) {
switch (select) {
case 0:
//The Pitchbend (X) axis.
pitchBend(range);
break;
case 1:
//Modulation Case
range = range/2;
if (range > 300){
int MOD = map(range, 300, 511, 0, 127);
control(MIDIchannel,Modulation, MOD);
}
else {
int RES = map(range, 299, 83, 0, 127);
control(MIDIchannel,Resonance, RES);
}
break;
case 2:
// LFO
int LFOSig = map(range, 1023, 0, 0, 127);
control(MIDIchannel,LFO, LFOSig);
break;
case 3:
//Cutoff
int CutSig = map(range, 1023, 0, 0, 127);
control(MIDIchannel,Cutoff, CutSig);
break;
}
}