How to close/turn OFF MIDI.read();

In an IF statement, I need the arduino to stop looking at the MIDI.read(). So something like MIDI.read(off);
Any idea on how to do that?

tks
Seby

Anyone please?

As MIDI.read() is a function you call then you simply don’t call it.
If you want to start again and skip those midi messages that were sent when you didn’t want to look at them then empty the buffer before calling it again.

Having said that your question does not make much sense. Why would want to do that?

Read the how to use this forum sticky post, it will tell you that just over an hour is an unacceptable time for bumping a thread. You have to leave it a minimum of 24 hours.

Grumpy_Mike:
As MIDI.read() is a function you call then you simply don’t call it.
If you want to start again and skip those midi messages that were sent when you didn’t want to look at them then empty the buffer before calling it again.

Having said that your question does not make much sense. Why would want to do that?

Read the how to use this forum sticky post, it will tell you that just over an hour is an unacceptable time for bumping a thread. You have to leave it a minimum of 24 hours.

Hi, tks for the answer, but that does not help me in my case. So I call it to start in a if, but IF in the ELSE , I want to cancel that call so the midi in wil not receive anything in.

tks
Seby

I call it to start in a if, but IF in the ELSE , I want to cancel that call so the midi in wil not receive anything in.

Sorry I can’t understand anything you said there.

Please post the code you have now and mark the place where you think you need to stop the MIDI from coming in. And if you can say why you need that it might also help.

Steve

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();




// Setting up the counter
int reading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;

// Timing for polling the encoder
unsigned long currentTime;
unsigned long lastTime;


// Pin definitions
const int pinA = 8;
const int pinB = 9;

// Storing the readings

boolean encA;
boolean encB;
boolean lastA = false;

byte midiCh = 1; // MIDI channel to be used
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC

void setup() {
  pinMode(ledPin, OUTPUT);
  // set the two pins as inputs with internal pullups
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  // Set up the timing of the polling
  currentTime = millis();
  lastTime = currentTime; 
  // Start the serial monitor for debugging
  MIDI.begin();
  MIDI.turnThruOff();
  Serial.begin(9600);
   MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
} 


void loop()
{
  MIDI.read();

  // Read elapsed time
  currentTime = millis(); 
  // Check if it's time to read
  if(currentTime >= (lastTime + 5))
  {
    // read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {
      // check if B is high 
      if (encB)
      {
        
        
        // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt; 
        }
      }
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt; 
        }
      }
      // Output reading for debugging
       MIDI.sendControlChange(cc, reading, midiCh);
       
      //Serial.println(reading);
    }
    // store reading of A and millis for next loop
    lastA = encA;
    lastTime = currentTime;

  }

}

///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;
  
 // Serial.println(value);
/* if (value != 0){
 digitalWrite(ledPin, HIGH);
 }else
 digitalWrite(ledPin, LOW);
 */
}


void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity


}


void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity


}

During the turning of the encoder, I want the arduino to stop listening because I have a midi feedback from the device i'm sending the midi to. This device is like in a thru mode and not possible to put it off. So I need a way to cancell the midi in while I'm turning the encoder. See in the MIDI IN section, I have a reading = value, wich I need but only when nothing is turned on the arduino.

tks
Seby

I see what you mean now, a bit of context helps enormously.
It is not MIDI.read() you want to turn off, as I said it dosn't make sense as it will only trigger any callback functions already received.

It seems to me what you want to do is to either stop MIDI being received in the first place, or flush the MIDI buffer to ignore any messages stored in the buffer.

I haven't tried this myself, but a good place to start would be to try something like this:-

      MIDI.disconnectCallbackFromType(midi::ControlChange);
      MIDI.read(); // look at all the messages and ignore the CC ones
      MIDI.setHandleControlChange(handleControlChange); // re enable the call back function

Grumpy_Mike:
I see what you mean now, a bit of context helps enormously.
It is not MIDI.read() you want to turn off, as I said it dosn't make sense as it will only trigger any callback functions already received.

It seems to me what you want to do is to either stop MIDI being received in the first place, or flush the MIDI buffer to ignore any messages stored in the buffer.

I haven't tried this myself, but a good place to start would be to try something like this:-

      MIDI.disconnectCallbackFromType(midi::ControlChange);

MIDI.read(); // look at all the messages and ignore the CC ones
     MIDI.setHandleControlChange(handleControlChange); // re enable the call back function

hi, tks for the answer, but where do I put those in my code please. lol. I'm a noob in coding. But from what I see, when I MIDI.setHandleControlChange(handleControlChange); the loop is so fast, it's like it's always enabled.

tks
Seby

EDIT: The loop is too fast with this try

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();




// Setting up the counter
int reading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;

// Timing for polling the encoder
unsigned long currentTime;
unsigned long lastTime;


// Pin definitions
const int pinA = 8;
const int pinB = 9;

// Storing the readings

boolean encA;
boolean encB;
boolean lastA = false;

byte midiCh = 1; // MIDI channel to be used
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC

void setup() {
  pinMode(ledPin, OUTPUT);
  // set the two pins as inputs with internal pullups
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  // Set up the timing of the polling
  currentTime = millis();
  lastTime = currentTime; 
  // Start the serial monitor for debugging
  MIDI.begin();
  MIDI.turnThruOff();
  Serial.begin(9600);
   MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
} 


void loop()
{
  MIDI.read();
  

  // Read elapsed time
  currentTime = millis(); 
  // Check if it's time to read
  if(currentTime >= (lastTime + 5))
  {
    MIDI.disconnectCallbackFromType(midi::ControlChange);
    // read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {
      
      // check if B is high 
      if (encB)
      {
        
        
        // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt; 
        }
      }
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt; 
        }
      }
      // Output reading for debugging
       MIDI.sendControlChange(cc, reading, midiCh);
       
       
      //Serial.println(reading);
    }
    // store reading of A and millis for next loop
    lastA = encA;
    lastTime = currentTime;

  }else{
    
    MIDI.setHandleControlChange(handleControlChange);
  }

}

///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;
  
 // Serial.println(value);
/* if (value != 0){
 digitalWrite(ledPin, HIGH);
 }else
 digitalWrite(ledPin, LOW);
 */
}


void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity


}


void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity


}

but where do I put those in my code please.

You do this when you want to flush the buffer. If it is too fast then add a small delay after the read and before you reenable the call back functions

Grumpy_Mike:
You do this when you want to flush the buffer. If it is too fast then add a small delay after the read and before you reenable the call back functions

Probably I do it wrong, cause when Iput a delay, everything stop.
tks
Seby

seby20:
Probably I do it wrong, cause when Iput a delay, everything stop.
tks
Seby

Yes you probably did it wrong. But who knows what is wrong because you haven’t posted your code.

Look, only you have access to your system, we can only go off what you tell us and what you show us. To date you have been pretty vague about things.
When you post code you should say what you want it to do and what it actually does. For example when you said:-

The loop is too fast with this try

You were not saying what it did. You were saying what you thought was wrong. Given that you previously said:-

I'm a noob in coding.

How on earth can you make a diagnosis like that?

Ok, here's exactly whatr I'm trying to do. Thank you very much for your help and your time, I really appreciate it.
Seby

What i'm trying to do in a video example

My Code in PasteBin

Thanks for that.
Please post code according to the rules in How to use this forum as we don't like going off site for code. This is what as posted.

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
 
 
// Setting up the counter
int reading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;
 
// Timing for polling the encoder
unsigned long currentTime;
unsigned long lastTime;
 
 
// Pin definitions
const int pinA = 8;
const int pinB = 9;
 
// Storing the readings
 
boolean encA;
boolean encB;
boolean lastA = false;
 
byte midiCh = 1; // MIDI channel to be used
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC
 
void setup() {
  pinMode(ledPin, OUTPUT);
  // set the two pins as inputs with internal pullups
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  // Set up the timing of the polling
  currentTime = millis();
  lastTime = currentTime;
  // Start the serial monitor for debugging
  MIDI.begin();
  MIDI.turnThruOff();
  Serial.begin(9600);
   MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
}
  
void loop()
{
  if(lastTime <= (currentTime ))
  {
  MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
  MIDI.read();
  }
 
  // Read elapsed time
  currentTime = millis();
  // Check if it's time to read
  if(currentTime >= (lastTime + 5))
  {
    MIDI.disconnectCallbackFromType(midi::ControlChange);
 
    // read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {
     
      // check if B is high
      if (encB)
      {
       
        // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt;
        }
      }
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt;
        }
      }
      // Output reading for debugging
       MIDI.sendControlChange(cc, reading, midiCh);
             
      //Serial.println(reading);
    }
    // store reading of A and millis for next loop
    lastA = encA;
    lastTime = currentTime;
    
  }
 
}
 
///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;
 
 // Serial.println(value);
/* if (value != 0){
 digitalWrite(ledPin, HIGH);
 }else
 digitalWrite(ledPin, LOW);
 */
}
 
 
void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity

}
 
 
void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
  
}

I must admit I am having difficulties in seeing how this code addresses what you are trying to do. Also I am not convinced that what you are trying to do will actually address your problem.

The way I see it is you are using a rotary encoder to change a parameter in a VI and changing that parameter is causing the VI to output data as if it came from the VI itself, which if I have it right, is what should happen.

However I can not make sense of your code. You seem to be disabling the call back function while you check the rotary encoder, I can't see how that will help with your problem.

If I have it right the problem is that conceptually you are stuck in a loop of cause and effect, if so there is no way out of it.

Grumpy_Mike:
Thanks for that.
Please post code according to the rules in How to use this forum as we don't like going off site for code. This is what as posted.

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

// Setting up the counter
int reading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;

// Timing for polling the encoder
unsigned long currentTime;
unsigned long lastTime;

// Pin definitions
const int pinA = 8;
const int pinB = 9;

// Storing the readings

boolean encA;
boolean encB;
boolean lastA = false;

byte midiCh = 1; // MIDI channel to be used
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC

void setup() {
  pinMode(ledPin, OUTPUT);
  // set the two pins as inputs with internal pullups
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  // Set up the timing of the polling
  currentTime = millis();
  lastTime = currentTime;
  // Start the serial monitor for debugging
  MIDI.begin();
  MIDI.turnThruOff();
  Serial.begin(9600);
  MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
}
 
void loop()
{
  if(lastTime <= (currentTime ))
  {
  MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
  MIDI.read();
  }

// Read elapsed time
  currentTime = millis();
  // Check if it's time to read
  if(currentTime >= (lastTime + 5))
  {
    MIDI.disconnectCallbackFromType(midi::ControlChange);

// read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {
   
      // check if B is high
      if (encB)
      {
     
        // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt;
        }
      }
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt;
        }
      }
      // Output reading for debugging
      MIDI.sendControlChange(cc, reading, midiCh);
           
      //Serial.println(reading);
    }
    // store reading of A and millis for next loop
    lastA = encA;
    lastTime = currentTime;
   
  }

}

///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;

// Serial.println(value);
/* if (value != 0){
digitalWrite(ledPin, HIGH);
}else
digitalWrite(ledPin, LOW);
*/
}

void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity

}

void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
 
}




I must admit I am having difficulties in seeing how this code addresses what you are trying to do. Also I am not convinced that what you are trying to do will actually address your problem.

The way I see it is you are using a rotary encoder to change a parameter in a VI and changing that parameter is causing the VI to output data as if it came from the VI itself, which if I have it right, is what should happen.

However I can not make sense of your code. You seem to be disabling the call back function while you check the rotary encoder, I can't see how that will help with your problem.

If I have it right the problem is that conceptually you are stuck in a loop of cause and effect, if so there is no way out of it.

Grumpy_Mike:
Thanks for that.
Please post code according to the rules in How to use this forum as we don't like going off site for code. This is what as posted.

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

// Setting up the counter
int reading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;

// Timing for polling the encoder
unsigned long currentTime;
unsigned long lastTime;

// Pin definitions
const int pinA = 8;
const int pinB = 9;

// Storing the readings

boolean encA;
boolean encB;
boolean lastA = false;

byte midiCh = 1; // MIDI channel to be used
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC

void setup() {
  pinMode(ledPin, OUTPUT);
  // set the two pins as inputs with internal pullups
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  // Set up the timing of the polling
  currentTime = millis();
  lastTime = currentTime;
  // Start the serial monitor for debugging
  MIDI.begin();
  MIDI.turnThruOff();
  Serial.begin(9600);
  MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
}
 
void loop()
{
  if(lastTime <= (currentTime ))
  {
  MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
  MIDI.read();
  }

// Read elapsed time
  currentTime = millis();
  // Check if it's time to read
  if(currentTime >= (lastTime + 5))
  {
    MIDI.disconnectCallbackFromType(midi::ControlChange);

// read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {
   
      // check if B is high
      if (encB)
      {
     
        // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt;
        }
      }
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt;
        }
      }
      // Output reading for debugging
      MIDI.sendControlChange(cc, reading, midiCh);
           
      //Serial.println(reading);
    }
    // store reading of A and millis for next loop
    lastA = encA;
    lastTime = currentTime;
   
  }

}

///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;

// Serial.println(value);
/* if (value != 0){
digitalWrite(ledPin, HIGH);
}else
digitalWrite(ledPin, LOW);
*/
}

void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity

}

void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
 
}




I must admit I am having difficulties in seeing how this code addresses what you are trying to do. Also I am not convinced that what you are trying to do will actually address your problem.

The way I see it is you are using a rotary encoder to change a parameter in a VI and changing that parameter is causing the VI to output data as if it came from the VI itself, which if I have it right, is what should happen.

However I can not make sense of your code. You seem to be disabling the call back function while you check the rotary encoder, I can't see how that will help with your problem.

If I have it right the problem is that conceptually you are stuck in a loop of cause and effect, if so there is no way out of it.

What I want is when I turn the encoder is to NOT receive anything from midi in and then still listen for maybe 1 seconf after there's no more movement. If no more movement, then BREAK the while/loop and then continue and re-enable the midi in.

tks
Seby

This is the way I would go about this in code. It compiles but I have not tried it out so it might need some tweaking.

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
// Setting up the counter
int reading = 0;
int lastReading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;
boolean moveing = false;
 
// Timing for polling the encoder
unsigned long restartTime = 0;
 
 
// Pin definitions
const int pinA = 8;
const int pinB = 9;
 
boolean encA;
boolean encB;
boolean lastA = false;
 
byte midiCh = 1; // MIDI channel to be used which is Channel 2
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC
 
void setup() {
  pinMode(ledPin, OUTPUT);
  // set the two pins as inputs with internal pullups
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);

  MIDI.begin();
  MIDI.turnThruOff();
    // Start the serial monitor for debugging
  // Serial.begin(9600); // I don't thing you can do this with the MIDI.h libiary 
   MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
}
  
void loop()
{
  if( ! moveing ) MIDI.read(); // real MIDI read
   
  if(millis() > restartTime && moveing){ // restore call back
     MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
     moveing = false;
  }
 if(readEncoder() != -1){ // a change in the reading has occoured
  {
    MIDI.disconnectCallbackFromType(midi::ControlChange); 
    MIDI.sendControlChange(cc, reading, midiCh);
    restartTime = millis() + 7; // or what ever
    moveing = true;
    delay(2); // to allow time for echo
    MIDI.read(); // to flush out the buffer
    }    
  }
 
}

int readEncoder(){ // see if the encoder has moved
// read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    reading = lastReading;
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {    
      // check if B is high
      if (encB)
      {       
       // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt;
        }
      } // end of checking 1
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt;
        }
      }
    // ristrain the reading value  
    if( reading < 0) reading = 0;
    if( reading > 127) reading = 127;
    lastA = encA;
    if(reading != lastReading){
      lastReading=reading;
      return reading;
    }
    else{
      return -1;  // if there is no change
    }
  }
}
///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;
 
 // Serial.println(value);
/* if (value != 0){
 digitalWrite(ledPin, HIGH);
 }else
 digitalWrite(ledPin, LOW);
 */
}
 
 
void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity

}
 
 
void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
  
}

Grumpy_Mike:
This is the way I would go about this in code. It compiles but I have not tried it out so it might need some tweaking.

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();
// Setting up the counter
int reading = 0;
int lastReading = 0;
int lowest = 0;
int highest = 127;
int changeamnt = 1;
int ledPin = 13;
boolean moveing = false;

// Timing for polling the encoder
unsigned long restartTime = 0;

// Pin definitions
const int pinA = 8;
const int pinB = 9;

boolean encA;
boolean encB;
boolean lastA = false;

byte midiCh = 1; // MIDI channel to be used which is Channel 2
byte note = 36; // Lowest MIDI note
byte cc = 1; // Lowest MIDI CC

void setup() {
 pinMode(ledPin, OUTPUT);
 // set the two pins as inputs with internal pullups
 pinMode(pinA, INPUT_PULLUP);
 pinMode(pinB, INPUT_PULLUP);

MIDI.begin();
 MIDI.turnThruOff();
   // Start the serial monitor for debugging
 // Serial.begin(9600); // I don't thing you can do this with the MIDI.h libiary
  MIDI.setHandleControlChange(handleControlChange); // Listens to control change
 //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
 //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
}
 
void loop()
{
 if( ! moveing ) MIDI.read(); // real MIDI read
 
 if(millis() > restartTime && moveing){ // restore call back
    MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
    moveing = false;
 }
if(readEncoder() != -1){ // a change in the reading has occoured
 {
   MIDI.disconnectCallbackFromType(midi::ControlChange);
   MIDI.sendControlChange(cc, reading, midiCh);
   restartTime = millis() + 7; // or what ever
   moveing = true;
   delay(2); // to allow time for echo
   MIDI.read(); // to flush out the buffer
   }    
 }

}

int readEncoder(){ // see if the encoder has moved
// read the two pins
   encA = digitalRead(pinA);
   encB = digitalRead(pinB);
   reading = lastReading;
   // check if A has gone from high to low
   if ((!encA) && (lastA))
   {    
     // check if B is high
     if (encB)
     {      
      // clockwise
       if (reading + changeamnt <= highest)
       {
         reading = reading + changeamnt;
       }
     } // end of checking 1
     else
     {
       // anti-clockwise
       if (reading - changeamnt >= lowest)
       {
         reading = reading - changeamnt;
       }
     }
   // ristrain the reading value  
   if( reading < 0) reading = 0;
   if( reading > 127) reading = 127;
   lastA = encA;
   if(reading != lastReading){
     lastReading=reading;
     return reading;
   }
   else{
     return -1;  // if there is no change
   }
 }
}
///////////////////////////////////////////
// MIDI IN
void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
reading = value;

// Serial.println(value);
/* if (value != 0){
digitalWrite(ledPin, HIGH);
}else
digitalWrite(ledPin, LOW);
*/
}

void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity

}

void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
 
}

Hi, here's the result that I got from your code.
Tks from your help
Seby

OK. The minus one comes from the readEncoder function. It returns -1 when their is no change between the reading from the encoders and the reading last time.
The line

if(readEncoder() != -1)

Calls the readEncoder() function and compares it to -1. If it is NOT -1 then that means their has been a change in the value of the reading and hence sends out the data.

The fact that the readEncoder at first sight appears not to be sending back -1 would suggest that there is something wrong with that function. I just took the code you wrote and moved it into a function to make the code a lot tidier. I must admit I did not understand your logic with this code. I did add code to prevent it going above 127, the MIDI maximum and below 0 the MIDI minimum value for the number you sent.
As a test replace

return reading;

with

return -1;

If that doesn't stop the continuous send then the problem is elsewhere.

Using 9600 baud for hairless is a bit slow, set this to 11520 baud at both ends. This will not immediately solve the current problem but will prevent problems further down the line.

I just had a quick look at the readEncoder function and I think there is a misplaced bracket. Try using this one:-

int readEncoder(){ // see if the encoder has moved
// read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    reading = lastReading;
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {    
      // check if B is high
      if (encB)
      {       
       // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt;
        }
      } // end of checking 1
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt;
        }
      }
      
    // ristrain the reading value  
    if( reading < 0) reading = 0;
    if( reading > 127) reading = 127;
    lastA = encA;
    }
    if(reading != lastReading){
      lastReading=reading;
      return reading;
    }
    else{
      return -1;  // if there is no change
    }
}

Grumpy_Mike:
I just had a quick look at the readEncoder function and I think there is a misplaced bracket. Try using this one:-

int readEncoder(){ // see if the encoder has moved

// read the two pins
    encA = digitalRead(pinA);
    encB = digitalRead(pinB);
    reading = lastReading;
    // check if A has gone from high to low
    if ((!encA) && (lastA))
    {   
      // check if B is high
      if (encB)
      {     
      // clockwise
        if (reading + changeamnt <= highest)
        {
          reading = reading + changeamnt;
        }
      } // end of checking 1
      else
      {
        // anti-clockwise
        if (reading - changeamnt >= lowest)
        {
          reading = reading - changeamnt;
        }
      }
     
    // ristrain the reading value 
    if( reading < 0) reading = 0;
    if( reading > 127) reading = 127;
    lastA = encA;
    }
    if(reading != lastReading){
      lastReading=reading;
      return reading;
    }
    else{
      return -1;  // if there is no change
    }
}

Ok, tks again, I change my code for the one above, now, the problem, is when I turn the encoder, nothing is happening. But no more loop.