I found the last code I used, maybe it can be helpful:
//reads 16 analog inputs of an arduino mega & convert it to midi
// just for memory, the MIDI status byte is 176 to define we send a MIDI CC on Channel 1.
//define the MIDI channel here
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
int midiChannel = 2; // Midi Channel
// Variables
// define variables for the controller data
int AnalogValue[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// define variables for the digital inputs
int DigitalValue[10]= {0,0,0,0,0,0,0,0,0,0};
// define the "lastValue" variables for analog inputs
int lastAnalogValue[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// define the last Digital value variable
int lastDigitalValue[10] = {0,0,0,0,0,0,0,0,0,0};
//select the midi controller number for each digital input
int digmidiCCselect[10] = {110,111,36,37,38,39,40,41,42,43};
// select the midi Controller Number for each analogue input
int midiCCselect[16] = {20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35};
// select threshold for each analog input
int thresh[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // was 1 on the original program
// select number of desired analog inputs and digital inputs (max 16 on arduino mega)
int an_input_no = 16;
int dig_input_no= 10; //will scan digital inputs 2 to 9 (0 & 1 are used for serial communication)
void setup() {
// set the states of the I/O pins:
// pinMode(switchPin, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
// Set MIDI baud rate:
// NOT NECESSARY IN THE TRANSMITTER SKETCH !
// Serial.begin(31250);
//Notes: :)
/* Pins:
* Hardware SPI:
* MISO -> 12 // 50 //on arduino mega
* MOSI -> 11 // 51 //on mega
* SCK -> 13 // 52 //on mega
*
* Configurable:
* CE -> 8
* CSN -> 7
*/
Mirf.cePin = 49; // CE sur Digital pin dispo de l'arduino Mega
Mirf.csnPin = 53; // CSN sur Digital pin dispo de l'arduino Mega
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.channel = 0;
Mirf.payload = 3*sizeof(byte);
Mirf.config();
Mirf.setTADDR((byte *)"singer01t"); //a checker ds sketch recepteur
Mirf.setRADDR((byte *)"singer01r"); //a checker
//a essayer: 0x06 = 1Mbs data rate - 0x26 = 250 Kbs data rate (longest range)
Mirf.configRegister(RF_SETUP,0x06);
}
// main program loop
void loop() {
//scan the digital inputs 2 to "dig_input_no" and send CC
for(int i=2;i<dig_input_no;i++) {
DigitalValue[i] = (digitalRead(i))*127; //scale 0-1 digital inputs to have a midi value 0-127
if ( DigitalValue[i] != lastDigitalValue [i] ) {
//send control change on cc#i on midiChannel
midiCC((175+midiChannel), digmidiCCselect[i], DigitalValue[i]);
// update lastAnalogValue variable
lastDigitalValue[i] = DigitalValue[i];
//end if
}
//end for
}
//scan the analog inputs and send CC
for(int i=0;i<an_input_no;i++){
AnalogValue[i] = (analogRead(i))/8; // read the value from the analog input and divide by 8 for range 0-127
// check if value is greater than defined threshold (good for resistive touchpads etc)
if ( AnalogValue [i]>thresh[i] ) {
// check if analog input has changed
if ( AnalogValue[i] != lastAnalogValue[i] ) {
//send control change on cc#i on midiChannel
midiCC((175+midiChannel), midiCCselect[i], AnalogValue[i]);
// update lastAnalogValue variable
//lastAnalogValue[i] = AnalogValue[i];
//End if
}
//End if
}
//A ESSAYER POUR STABILISER LES VALEURS
else if (AnalogValue [i]<=thresh[i] ) {
// AnalogValue[i]=0;
// midiCC((175+midiChannel), midiCCselect[i], AnalogValue[i]);
// midiCC((175+midiChannel), midiCCselect[i], 0);
// lastAnalogValue[i] = AnalogValue[i];
//delay(1);
//endif
}
//End for
}
//End Loop
}
// This function sends a Midi CC.
//void midiCC(char CC_data, char c_num, char c_val){
//Serial.print(CC_data, BYTE);
//Serial.print(c_num, BYTE);
//Serial.print(c_val, BYTE);
// }
// This function sends a Midi CC over the NRF20L01+ transceiver
void midiCC(char CC_data, char c_num, char c_val){
byte data[Mirf.payload];
data[0] = CC_data;
data[1] = c_num;
data[2] = c_val;
Mirf.send((byte*)&data);
while(Mirf.isSending());
}