int potPin1 = A0;
int potValue1 = 0;
int potPin2 = A1;
int potValue2 = 0;
int potPin3 = A2;
int potValue3 = 0;
int potPin4 = A3;
int potValue4 = 0;
//Joystick/thumbstic Pins
const int SW_pin = 8; // digital pin connected to switch output
const int X_pin = A4; // analog pin connected to X output
const int Y_pin = A5; // analog pin connected to Y output
const int pushButton1 = 7;
const int pushButton2 = 5;
const int pushButton3 = 6;
const int Toggle = 9;
int toggleState = 0;
int LED1 = 10;
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
pinMode(pushButton1, INPUT);
digitalWrite(pushButton1, HIGH);
pinMode(pushButton2, INPUT);
digitalWrite(pushButton1, HIGH);
pinMode(pushButton3, INPUT);
digitalWrite(pushButton1, HIGH);
pinMode(Toggle, INPUT);
digitalWrite(pushButton1, HIGH);
pinMode(LED1, OUTPUT);
Serial.begin(11520);
}
void loop() {
//Joystick
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
//Buttons
int button1State = digitalRead(pushButton1);
// print out the state of the button
Serial.print("button1 =");
Serial.println(button1State);
int button2State = digitalRead(pushButton2);
// print out the state of the button
Serial.print("button2 =");
Serial.println(button2State);
int button3State = digitalRead(pushButton3);
// print out the state of the button
Serial.print("button3 =");
Serial.println(button3State);
int button4State = digitalRead(Toggle);
// print out the state of the button
Serial.print("toggle =");
Serial.println(button4State);
//Potentiometers
potValue1 = analogRead(potPin1);
Serial.print("potentiometer1 = " );
Serial.println(potValue1);
potValue2 = analogRead(potPin2);
Serial.print("potentiometer2 = " );
Serial.println(potValue2);
potValue3 = analogRead(potPin3);
Serial.print("potentiometer3 = " );
Serial.println(potValue3);
potValue4 = analogRead(potPin4);
Serial.print("potentiometer4 = " );
Serial.println(potValue4);
//Toggle turning on LED
toggleState = digitalRead(Toggle); //Toggle
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (toggleState == HIGH) {
// turn LED on:
digitalWrite(LED1, HIGH);
} else {
// turn LED off:
digitalWrite(LED1, LOW);
}
}
Basically what you've got is a "input hardware test" program. If that's working, great.
Do you have a "traditional" serial MIDI connection with the round connector, or are you using USB-MIDI?
Next, I'd suggest you figure-out how to send just one or two MIDI commands (with no pots, switches or any input) before you try integrating all of that hardware.
Then if you want to program a switch to send note-on and note-off commands or if you want to [u]map()[/u] the default 0-1023 pot reading to a 0-127 MIDI velocity, etc., that should be easy enough. But, I'd recommend that you work on one control/message at a time/
If you want more features, such as MIDI input, you can check out my Control Surface Library. It's the successor of the MIDI Controller library, but I haven't finished all examples and documentation yet.
The advantage of using one of these libraries is that they handle many things for you, like debouncing of the buttons, and filtering and mapping of the analog inputs. They also support multiplexers and other hardware expanders if you ever need more inputs.
Thank you so much, this is perfect ! knobs, buttons and joystick works very well, however having issues with rotary encoder: it only goes two ways, either to 1 or 127, what could be a problem?
hdxn:
however having issues with rotary encoder: it only goes two ways, either to 1 or 127, what could be a problem?
That's normal, rotary encoders only send out relative position changes, rather than an absolute position. This is actually an advantage for MIDI controllers, since the initial value in the DAW software doesn't have to match the physical position of the knob.
Your DAW should have a setting for relative MIDI control. What software are you using?
In relative mode, you just sum (accumulate) all values you receive. There's a catch, though: all values are encoded as 7-bit two's complement. So if a value is greater than or equal to 64 (0x40), you have to subtract 128 (0x80).