Pots to midi with hairless

This is the new code... Can someone verify it, please?

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();
// Variables:
int cc = 0;
int AnalogValue = 0; // define variables for the controller data
int lastAnalogValue = 0; // define the "lastValue" variables

void setup() {
 
   
  //  launch MIDI
  MIDI.begin(1);
   Serial.begin(9600);
}

void loop() {
 
AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  cc = AnalogValue/8;
  // check if analog input has changed
  if (lastAnalogValue != cc) {
   
    // update lastAnalogValue variable
    lastAnalogValue = cc;
  }
  }   //  endif
 void MIDImessage(int command, int MIDInote, int MIDIvelocity){
 Serial.write(0xB0);
 Serial.write(50);
 Serial.write(2);}

I still don't understand why you need to use the MIDI library to send a MIDI message over the serial link. Of course, it would probably help if the program actually called the MIDImessage() function.

I found this:
http://www.codeproject.com/Articles/38203/Arduino-Based-MIDI-Expression-Pedal

It's exactly what I want but the code is old and have to be modernized: expression like BYTE are no more available... Can someone help me, please?

//#define DEBUG                 1

// Constants
const int LED_PIN = 13;             // LED connected to digital pin 13
const int POT_PIN = 0;              // Pot connected to analog pin 0
const int POT_THRESHOLD = 7;        // Threshold amount to guard against false values
const int MIDI_CHANNEL = 0;         // MIDI Channel 1

#ifdef DEBUG
const int DEBUG_RATE = 9600;        // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250;   // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif


void setup()
{
    pinMode(LED_PIN, OUTPUT);          // Sets the digital pin as output
    digitalWrite(LED_PIN, HIGH);       // Turn the LED on
    Serial.begin(SERIAL_PORT_RATE);     // Starts communication with the serial port
}

void loop()
{
    static int s_nLastPotValue = 0;
    static int s_nLastMappedValue = 0;

    int nCurrentPotValue = analogRead(POT_PIN);
    if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)
        return;
    s_nLastPotValue = nCurrentPotValue;

    int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 127); // Map the value to 0-127
    if(nMappedValue == s_nLastMappedValue)
        return;
    s_nLastMappedValue = nMappedValue;

    MidiVolume(MIDI_CHANNEL, nMappedValue);
}

void MidiVolume(byte channel, byte volume)
{
#ifdef DEBUG
    Serial.println(volume, DEC);
#else
    Serial.print(0xB0 | (channel & 0x0F), BYTE);    // Control change command
    Serial.print(0x07, BYTE);                       // Volume command
    Serial.print(volume & 0x7F, BYTE);              // Volume 0-127
#endif
}

Replace all of the

Serial.print(something, BYTE);

commands with

    Serial.write(something);

As I suggested, the program does not use the MIDI library but does use the odd MIDI baud rate.

It works but I cannot change the value on the program... it seems that the midi command doesn't send a change value of the data but a channel's change value... Mmm

After a lot of days... This is the best I've done!! There is still some problem of rate.. but.. it works!!
Has someone any idea how to improve the code?

/*
MIDI On/Off Messages
By Amanda Ghassaei
July 2012
http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

*/
int noteOFF = 128;//128
 const int analogInPin = A0;
 const int POT_THRESHOLD = 3;// in +
 const int HISTORY_BUFFER_LENGTH = 6;// in +
 int velocity = 0;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;//176 = in binary, controlchange command
 int NoteOn =145;
 //int noteOFF = 128;//128 = 10000000 in binary, note off command
static int s_history[HISTORY_BUFFER_LENGTH];// in +

//BUTTONS

int buttonState1;
int val1;
int buttonState2;
int val2;
int buttonState3;
int val3;
int led = 13;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;//led




void setup() {
  //  Set MIDI baud rate:
  Serial.begin(57600);
   // Initialize he buffer
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)// in +
    {// in +
        s_history[i] = -1;// in +
    }// in +
    
   
   //BUTTON 
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(led, OUTPUT);//led
   buttonState1 = digitalRead(2);//HIGH
   buttonState2 = digitalRead(3); //HIGH
buttonState3 = digitalRead(4); //HIGH
     
   
}

void loop() {
  
    val1 =digitalRead(2);
    if(val1 != buttonState1){
    if (val1 ==LOW) {
    //Send an ASCII 'A',
    MIDImessage(145, 60 ,100);
   
 
  }}
else{MIDImessage(145, 60 ,0);} 

  
  
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(146, 61,100);
  
}
else{MIDImessage(146, 61,0);}
  }
  
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(147, 62,100);
    }
    else{MIDImessage(147, 62,0);}
  }
  delay(50);
  buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
  
  
  
    
    
  static int s_nLastPotValue = 0; //in +
  static int s_nLastMappedValue = 0;//in +
   int nCurrentPotValue = analogRead(A0);//in +
    if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)//in +
        return;//in +
    s_nLastPotValue = nCurrentPotValue;//in +

    int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue == s_nLastMappedValue)//in +
        return;//in +
    
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
    {//in +
        if(s_history[i] == nMappedValue)//in +
            return;//in +
    }//in +

    memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue;//in +
    s_nLastMappedValue = nMappedValue;//in +
   
   
   // sensorValue = analogRead (analogInPin);
   
    velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
    MIDImessage(176, 50, velocity);//turn note on
    delay(20);
    //MIDImessage(noteOFF, 50, velocity);//turn note off
    //wait 200ms until triggering next n
      
  }
 //send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);//send note on or note off command 
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}

Now I'm wondering how to add another potentiometer..the last code give me different errors.. It change the value of speed of the two pots at the same time.. Any idea?

Post the code with more than one pot and the errors that you got.

When I try to upload this is the error:

sketch_apr25b.ino:18:2: error: expected ',' or ';' before 'const'
sketch_apr25b.ino:27:22: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
sketch_apr25b.ino: In function 'void setup()':
sketch_apr25b.ino:52:20: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
sketch_apr25b.ino:54:9: error: 's_history' was not declared in this scope
sketch_apr25b.ino: In function 'void loop()':
sketch_apr25b.ino:127:20: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
sketch_apr25b.ino:129:12: error: 's_history' was not declared in this scope
sketch_apr25b.ino:133:13: error: 's_history' was not declared in this scope
sketch_apr25b.ino:133:57: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
Errore durante la compilazione

The code is:

/*
MIDI On/Off Messages
By Amanda Ghassaei
July 2012
http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

*/
int noteOFF = 128;//128 note off on midi command
 const int analogInPin = A0;
 const int analogInPin2 = A1;//pin del potenziometro 1
 const int POT_THRESHOLD = 3;
 const int POT_THRESHOLD2 = 3// in +
 const int HISTORY_BUFFER_LENGTH = 6;// in +
 int velocity = 0;
 int velocity2 = 0;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;
 int ControlChange2 =180; //176 = in binary, controlchange command
 int NoteOn =145;
 //int noteOFF = 128;//128 = 10000000 in binary, note off command
static int s_history[HISTORY_BUFFER_LENGTH];// in +

//BUTTONS

int buttonState1;
int val1;
int buttonState2;
int val2;
int buttonState3;
int val3;
int led = 13;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;//led

//TIMER
long previousMillis = 0;
long interval = 50; 




void setup() {
  //  Set MIDI baud rate:
  Serial.begin(57600);
   // Initialize he buffer
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)// in +
    {// in +
        s_history[i] = -1;// in +
    }// in +
    
   
   //BUTTON 
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(led, OUTPUT);//led
   buttonState1 = digitalRead(2);//HIGH
   buttonState2 = digitalRead(3); //HIGH
buttonState3 = digitalRead(4); //HIGH
     
   
}

void loop() {
  
  
    val1 =digitalRead(2);
    if(val1 != buttonState1){
    if (val1 ==LOW) {
    //Send an ASCII 'A',
    MIDImessage(145, 60 ,100);
   
 
  }
else{MIDImessage(129,60,0);}} 

  
  
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(146, 61,100);
  
}
else{MIDImessage(130, 61,0);}
  }
  
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(147, 62,100);
    }
    else{MIDImessage(131, 62,0);}
  }
 
  unsigned long currentMillis = millis();//delay(50);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   
 
  buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
  }
  
  
    
    
  static int s_nLastPotValue = 0; //in +
  static int s_nLastMappedValue = 0;//in +
   int nCurrentPotValue = analogRead(A0);//in +
    if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)//in +
        return;//in +
    s_nLastPotValue = nCurrentPotValue;//in +

    int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue == s_nLastMappedValue)//in +
        return;//in +
    
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
    {//in +
        if(s_history[i] == nMappedValue)//in +
            return;//in +
    }//in +

    memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue;//in +
    s_nLastMappedValue = nMappedValue;//in +
   
   
   // sensorValue = analogRead (analogInPin);
   
    velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
    MIDImessage(176, 50, velocity);//turn note on
    delay(20);
   
   
   
   //POT2
    static int s_nLastPotValue2 = 0; //in +
  static int s_nLastMappedValue2 = 0;//in +
   int nCurrentPotValue2 = analogRead(A1);//in +
    if(abs(nCurrentPotValue2 - s_nLastPotValue2) < POT_THRESHOLD2)//in +
        return;//in +
    s_nLastPotValue2 = nCurrentPotValue2;//in +

    int nMappedValue2 = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue2 == s_nLastMappedValue2)//in +
        return;//in +
    
  
    s_nLastMappedValue2 = nMappedValue2;//in +
   
   
   // sensorValue = analogRead (analogInPin);
   
    velocity2 = map (nCurrentPotValue2, 0, 1023, 0, 127);
    MIDImessage(180, 51, velocity2);//turn note on
    delay(20);
   
   
   
   
   
    blinkLed();
    
  }
 //send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);//send note on or note off command 
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}


void blinkLed(){ //led function

  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 170) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(10);
}

I think I've to create something like this... but for only 2 pots and not 6:

//#include <MIDI.h>
#include <usbDevices.h>

#define numPots 6  // numer of pots
#define b 1        // this is the delta needed in

unsigned int currentPot[6] = {0,0,0,0,0,0};   
unsigned int pot[6] = {0,0,0,0,0,0};
byte controlChange = 0xBC; //0xB0 or 176

void setup() {
usb.init(midi);
// MIDI.begin();
//Serial.begin(31250);
}                         
void loop() {
  for(unsigned char i=0; i<numPots; i++) {
    currentPot[i] = analogRead(i) / 8;   
    if(abs(currentPot[i]-pot[i]) > b) {
      sendMidi(controlChange, i+1, currentPot[i]);
      pot[i] = currentPot[i];
    }
  }
}

void sendMidi(byte controlChange, unsigned int controlNum, int val) {
//MIDI.sendControlChange(controlNum, val);

Serial.write(controlChange);
Serial.write(controlNum);
Serial.write(val);
  // Serial.write(unsigned char(controlChange));
  //   Serial.write(unsigned char(controlNum));
  //     Serial.write(unsigned char(val));
  }

const int POT_THRESHOLD2 = 3// in +What must variable declarations always have at the end ?
Answer - a semi-colon.

Yeah but it doesn't still work.. have you tried it? it change the value of both the pots

  int nMappedValue2 = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255

This is in your POT2 section of the code so shouldn't nCurrentPotValue be nCurrentPotValue2 in the map() function ?

It doesn't change nothing... The code should be modified to accept at least 2 analog in ...
But I don't understand how to do it

PS:

 for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
    {//in +
        if(s_history[i] == nMappedValue)//in +
            return;//in +
    }//in +

    memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue;//in +

Someone can explain me this part of code? if I want to create a second history for analog 2.. how should I do it?

VICTORYYYYY!!!!! :grinning: :grinning: :grinning: :grinning: :grinning:
The problem was with return function... I changed all return with if-else.. and it works!

/*
MIDI On/Off Messages
By Amanda Ghassaei
July 2012
http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

*/
int noteOFF = 128;//128 note off on midi command
 const int analogInPin = A0;
 const int analogInPin2 = A1;//pin del potenziometro 1
 const int POT_THRESHOLD = 3;
 const int POT_THRESHOLD2 = 3;// in +
 const int HISTORY_BUFFER_LENGTH = 6;// in +
 int velocity = 0;
 int velocity2 = 0;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;
 int ControlChange2 =180; //176 = in binary, controlchange command
 int NoteOn =145;
 //int noteOFF = 128;//128 = 10000000 in binary, note off command
static int s_history[HISTORY_BUFFER_LENGTH];
static int s_history2[HISTORY_BUFFER_LENGTH];// in +

//BUTTONS

int buttonState1;
int val1;
int buttonState2;
int val2;
int buttonState3;
int val3;
int led = 13;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;//led

//TIMER
long previousMillis = 0;
long interval = 50;




void setup() {
  //  Set MIDI baud rate:
  Serial.begin(57600);
   // Initialize he buffer
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)// in +
    {// in +
        s_history[i] = -1;// in +
    }// in +
     for(int o=0; o<HISTORY_BUFFER_LENGTH; o++)// in +
    {// in +
        s_history2[o] = -1;// in +
    }// in +
    
  
   //BUTTON
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(led, OUTPUT);//led
   buttonState1 = digitalRead(2);//HIGH
   buttonState2 = digitalRead(3); //HIGH
buttonState3 = digitalRead(4); //HIGH
    
  
}

void loop() {
  
  
    val1 =digitalRead(2);
    if(val1 != buttonState1){
    if (val1 ==LOW) {
    //Send an ASCII 'A',
    MIDImessage(145, 60 ,100);
  
 
  }
else{MIDImessage(129,60,0);}}

  
  
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(146, 61,100);
  
}
else{MIDImessage(130, 61,0);}
  }
  
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(147, 62,100);
    }
    else{MIDImessage(131, 62,0);}
  }
 
  unsigned long currentMillis = millis();//delay(50);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
 
  buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
  }
  
  
    
    
  static int s_nLastPotValue = 0; //in +
  static int s_nLastMappedValue = 0;//in +
   int nCurrentPotValue = analogRead(A0);//in +
    if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD){}//in +
        else{//in +
    s_nLastPotValue = nCurrentPotValue;}//in +

    int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue == s_nLastMappedValue){}//in +
        else{//in +
    
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
    {//in +
        if(s_history[i] == nMappedValue){}//in +
            else{
    memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue;//in +
    s_nLastMappedValue = nMappedValue;//in +
  
  
    velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
    MIDImessage(176, 50, velocity);//turn note on//in +
    }}}//in +

    delay(10);
  
  
  //POT2
  
  
  static int s_nLastPotValue2 = 0; //in +
  static int s_nLastMappedValue2 = 0;//in +
   int nCurrentPotValue2 = analogRead(A1);//in +
    if(abs(nCurrentPotValue2 - s_nLastPotValue2) < POT_THRESHOLD2){}//in +
        else{//in +
    s_nLastPotValue2 = nCurrentPotValue2;}//in +

    int nMappedValue2 = map(nCurrentPotValue2, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue2 == s_nLastMappedValue2){}//in +
        else{//in +
    
    for(int o=0; o<HISTORY_BUFFER_LENGTH; o++)//in +
    {//in +
        if(s_history2[o] == nMappedValue2){}//in +
           else{
             memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history2[HISTORY_BUFFER_LENGTH - 1] = nMappedValue2;//in +
    s_nLastMappedValue2 = nMappedValue2;//in +
  
 
  
    velocity2 = map (nCurrentPotValue2, 0, 1023, 0, 127);
    MIDImessage(180, 51, velocity2);//turn note on//in +
    }}}//in +

    
    delay(10);
  
  
  
  
  
    blinkLed();
    
  }
 //send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);//send note on or note off command
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}


void blinkLed(){ //led function

  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 170) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(10);
}

Now that it works improve it by making the pot reading and data transmission into a function that you can call with parameters, thus avoiding repetition of what is basically the same code.

Or

Use arrays to hold the data and iterate through it using a for loop. This would make it easier to expand the number of post if required.

The second possibility is what I think the best One.. Can you help me? I don't understand the logic of for loop

I have not studied your code in detail but this is the sort of thing I had in mind using arrays and a for loop.

for (int pot = 0; pot < 2; pot++)
{
  s_nLastPotValue[pot] = 0; //declared as a global array
  s_nLastMappedValue[pot] = 0; //as above
  int nCurrentPotValue[pot] = analogRead(potPins[pot]);  //potPins held in a global array
  if (abs(nCurrentPotValue[pot] - s_nLastPotValue[pot]) < POT_THRESHOLD) {} //in +
  else { //in +
    s_nLastPotValue[pot] = nCurrentPotValue[pot];
  }//in +

  int nMappedValue[pot] = map(nCurrentPotValue[pot], 0, 1023, 0, 255); //in + // Map the value to 0-255
  if (nMappedValue[pot] == s_nLastMappedValue)[pot] {} //in +
    else { //in +

      for (int i = 0; i < HISTORY_BUFFER_LENGTH; i++) //in +
      { //in +
        if (s_history[i] == nMappedValue[pot]) {} //in +
        else {
          memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
          s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue[pot];//in +
          s_nLastMappedValue[pot] = nMappedValue[pot];//in +


          velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
          MIDImessage(176, 50, velocity);//turn note on//in +
        }
      }
    }//in +
  delay(10);
}

This is your code (good or bad !) modified to use arrays of values. The same code is executed twice (or more if you change the for loop and array sizes) using the value of pot from the for loop to access the relevant values held in arrays. I cannot guarantee that it will work but see what you make of it.

Thanks!! I will try in the next days! Thank you so much!!

Ok this is my conclusion:

/*
MIDI On/Off Messages
By Amanda Ghassaei
July 2012
http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

*/

byte control_now[2];
byte control_past[2];
byte n;
const int POT_THRESHOLD = 1;// in +
byte velocity[2];
int control_past0 = 0;
int control_past1 = 0;
 
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;//176 = in binary, controlchange command
 int NoteOn =145;
 int noteOFF = 128;//128 note off on midi command

//BUTTONS

int buttonState1;
int val1;
int buttonState2;
int val2;
int buttonState3;
int val3;
int led = 13;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;//led

//TIMER
long previousMillis = 0;
long interval = 50; 




void setup() {
  //  Set MIDI baud rate:
  Serial.begin(9600);
   
    
   
   //BUTTON 
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(led, OUTPUT);//led
   buttonState1 = digitalRead(2);//HIGH
   buttonState2 = digitalRead(3); //HIGH
buttonState3 = digitalRead(4); //HIGH
     
   
}

void loop() {
  
  
    val1 =digitalRead(2);
    if(val1 != buttonState1){
    if (val1 ==LOW) {
    //Send an ASCII 'A',
    MIDImessage(145, 60 ,100);
   
 
  }
else{MIDImessage(129,60,0);}} 

  
  
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(145, 61,100);
  
}
else{MIDImessage(129, 61,0);}
  }
  
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(145, 62,100);
    }
    else{MIDImessage(129, 62,0);}
  }
 
  unsigned long currentMillis = millis();//delay(50);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   
 
  buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
  }
  
  
  for (n =0; n<2; n++){  
    
int control_past0 = 0;
int control_past1 = 0;
   control_now[n]   =  map( analogRead(n), 0, 1023, 0, 127);//in +
    if(abs(control_now[n] - control_past[n]) < POT_THRESHOLD)//in +
       {}
        else{//in +
  control_past[n] = control_now[n];//in +

    
   velocity[2] = control_now[n];
  
    MIDImessage(176, 50+n, velocity[2]);//turn note on
    delay(5);
   
    
    
  }}
  blinkLed();}
 //send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);//send note on or note off command 
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}


void blinkLed(){ //led function

  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 170) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(5);
}

The problem is that if I change the Threshold to 3 it cannot reach the value of 127 and I don't understand why