Hi all
Still having trouble with this. Mike, wont your suggestion effectively halve the resolution?
Pieter I can't get your suggestions to work with my code.
See code below, (I didn't write this, it is hacked together and I don't really know what I am doing).
#define CC 0xB0
#define NUMBER_OF_ANALOG_INPUTS 16 // NOTE: if you change this value, also change the controllers array, to match the number of inputs and the number of controllers.
#define CHANNEL 1 // Send all messages on channel 1.
/* The list with the corresponding controller numbers: for example, the values of the potentiometer on A0 will be sent as the first controller number in this list, A1 as the second, etc. */
int controllers[] = {
0x7, 0x5, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0xE, 0x4A, 0x47, 0x58, 0x49, 0x4B, 0x4C, 0x48, 0x59, 0x54, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F, 0xF, 0x55, 0x56, 0x57, 0x4D, 0x4E, 0x4F, 0x10
};
int analogVal[NUMBER_OF_ANALOG_INPUTS]; // We declare an array for the values from the analog inputs
int analogOld[NUMBER_OF_ANALOG_INPUTS]; // We declare an array for the previous analog values.
int shift = 0;
/* The format of the message to send via serial. We create a new data type, that can store 3 bytes at once. This will be easier to send as MIDI. */
typedef struct {
uint8_t status; // first byte : status message (NOTE_ON, NOTE_OFF or CC (controlchange) and midi channel (0-15)
uint8_t data1; // second byte : first value (0-127), controller number or note number
uint8_t data2; // third byte : second value (0-127), controller value or velocity
}
t_midiMsg; // We call this data type 't_midiMsg'
void setup() // The setup runs only once, at startup.
{
pinMode(13, OUTPUT); // Set pin 13 (the one with the LED) to output
digitalWrite(13, LOW); // Turn off the LED
pinMode(11,INPUT_PULLUP);
for(int i = 0; i < NUMBER_OF_ANALOG_INPUTS; i++){ // We make all values of analogOld -1, so it will always be different from any possible analog reading.
analogOld[i]=-1;
}
Serial.begin(31250); // Start a serial connection @31250 baud or bits per second on digital pin 0 and 1, this is the connection to the ATmega16U2, which runs the HIDuino MIDI firmware. (31250 baud is the original MIDI speed.)
delay(1000); // Wait a second before sending messages, to be sure everything is set up
digitalWrite(13, HIGH);// Turn on the LED, when the loop is about to start.
}
void loop() // The loop keeps on repeating forever.
{
if(digitalRead(11) == LOW){
shift = 16;
}
else {
shift = 0;
}
t_midiMsg msg; // create a variable 'msg' of data type 't_midiMsg' we just created
for(int i = 0; i < NUMBER_OF_ANALOG_INPUTS; i++){ // Repeat this procedure for every analog input.
analogVal[i] = analogRead(i+A0)/8; // The resolution of the Arduino's ADC is 10 bit, and the MIDI message has only 7 bits, 10 - 7 = 3, so we divide by 2^3, or 8.
if(analogVal[i] != analogOld[i]){ // Only send the value, if it is a different value than last time.
msg.status = CC; // Controll Change
msg.status = msg.status | CHANNEL-1; // Channels are zero based (0 = ch1, and F = ch16). Bitwise or to add the status message and channel together:
/* status = 0bssss0000
* channel = 0b0000cccc
* | ------------------ (bitwise or)
* msg.status = 0bsssscccc
*/
msg.data1 = controllers[i+shift]; // Get the controller number from the array above.
msg.data2 = analogVal[i]; // Get the value of the analog input from the analogVal array.
Serial.write((uint8_t *)&msg, sizeof(msg)); // Send the MIDI message.
analogOld[i] = analogVal[i]; // Put the analog values in the array for old analog values, so we can compare the new values with the previous ones.
delay(10); // Wait for 10ms, so it doesn't flood the computer with MIDI-messages
}
}
}