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

So you are going to have to send me a diagram of how you have connected up the encoder's pins.

Grumpy_Mike:
So you are going to have to send me a diagram of how you have connected up the encoder's pins.

Hi, here's my connected uno with the encoder, the only difference I have is that on the other side of my encoder, I have 2 more pins, but not connected.
Seby

Thanks for that.

Unfortunately when I wire up my rotary encoder your code to count the pulses will not work. This is because there are two types of rotary encoder, one that produces a steady state of the encoder sequence each click, and the other that produces a steady state of zero and only outputs the steps of the encoder during the click. I have the latter type.
So that meant I had to write my own code for reading the encoder.

When I did, unfortunately what I had in mind to flush the buffer didn't work. The buffer was not flushed and it held onto the CC message until the handling routine was enabled again.

My second idea was to switch the CC handling function between a real one and a dummy one. This only worked intermittently and I have not been able to make it consistently use the dummy routine, despite a lot of fiddling with delays.

So after working on it all afternoon I have come to the conclusion that what you want can't be done, sorry.

Grumpy_Mike:
Thanks for that.

Unfortunately when I wire up my rotary encoder your code to count the pulses will not work. This is because there are two types of rotary encoder, one that produces a steady state of the encoder sequence each click, and the other that produces a steady state of zero and only outputs the steps of the encoder during the click. I have the latter type.
So that meant I had to write my own code for reading the encoder.

When I did, unfortunately what I had in mind to flush the buffer didn't work. The buffer was not flushed and it held onto the CC message until the handling routine was enabled again.

My second idea was to switch the CC handling function between a real one and a dummy one. This only worked intermittently and I have not been able to make it consistently use the dummy routine, despite a lot of fiddling with delays.

So after working on it all afternoon I have come to the conclusion that what you want can't be done, sorry.

Ok, tks, I really appreciate. Have a nice day
Seby

OK, I have done a bit more fiddling and just used flags, and that seems to be more reliable.
By using the moving variable I have used this to change the action of the handleControlChange function. Use it like this:-

void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
 if( moveing ){ // ignore any echo
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
   }
 else {
  // do what you would normally do with a control mesage
   digitalWrite(ledPin2, HIGH);
   delay(20);
  digitalWrite(ledPin2, LOW);
 }
}
}

Where ledPin2 is the pin driving another LED

With the loop function looking like this:-

void loop() {
  MIDI.read(); 
  if(millis() > restartTime && moveing){ 
     moveing = false;  // stop ignoring cc messages
  }
 if(readEncoder() != -1){ // a change in the reading has occoured 
    MIDI.sendControlChange(cc, reading, midiCh);
    restartTime = millis() + 30; // or what ever
    moveing = true;      // switch action of the handleControlChange function
  }
}

Then control change messages sent out and then echoed are diverted to the top part of the handleControlChange function and messages originating from your DAW are handled by the bottom half of the function.

So messages from the DAW flash LED2 and from the rotary encoder flash the on board LED.

Grumpy_Mike:
OK, I have done a bit more fiddling and just used flags, and that seems to be more reliable.
By using the moving variable I have used this to change the action of the handleControlChange function. Use it like this:-

void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value

if( moveing ){ // ignore any echo
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
  }
else {
  // do what you would normally do with a control mesage
  digitalWrite(ledPin2, HIGH);
  delay(20);
  digitalWrite(ledPin2, LOW);
}
}
}



Where ledPin2 is the pin driving another LED

With the loop function looking like this:-


void loop() {
  MIDI.read();
  if(millis() > restartTime && moveing){
    moveing = false;  // stop ignoring cc messages
  }
if(readEncoder() != -1){ // a change in the reading has occoured
    MIDI.sendControlChange(cc, reading, midiCh);
    restartTime = millis() + 30; // or what ever
    moveing = true;      // switch action of the handleControlChange function
  }
}




Then control change messages sent out and then echoed are diverted to the top part of the handleControlChange function and messages originating from your DAW are handled by the bottom half of the function.

So messages from the DAW flash LED2 and from the rotary encoder flash the on board LED.

Hi, sorry to ask you this, but i'm a little lost now lol, is it possible to copy and paste the full code with the new changes please?
tks
Seby

OK it is here but please note that the rotary encoder software while being functional is not robust. It has a tendency to randomly swap the direction it thinks the knob is being turned in. This is due to contact bounce and quick and dirty coding so I would strongly emphasise to anyone reading this later that the encoder code is not to be used as any sort of example of how a rotary encoder code should be written.
Also because your rotary encoder is different to the one I have this code will not work with the hardware you have so don't expect it to.

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
// Setting up the counter
int reading = 0;
int lastReading = 0;
int lowest = 0;
int highest = 127;
int changeIncrement = 1;
int ledPin = 13;
int ledPin2 = 12; // extra green LED
boolean moving = false;
 
// Timing for polling the encoder
unsigned long restartTime = 0;
 
// Pin definitions
const int pinA = 8;
const int pinB = 9;
 
boolean encA, last_encA;
boolean encB, last_encB;
 
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);
  pinMode(ledPin2, 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(115200);
   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(); 
  if(millis() > restartTime && moving){ 
     moving = false;  // stop ignoring cc messages
  }
 if(readEncoder() != -1){ // a change in the reading has occurred 
    MIDI.sendControlChange(cc, reading, midiCh);
    restartTime = millis() + 30; // or what ever
    moving = true;      // switch action of the handleControlChange function
  }
}

int readEncoder(){ // see if the encoder has moved
// read the two pins
    static boolean dir = true; // direction
    encA = digitalRead(pinA);
    encB = digitalRead(pinB); 
    lastReading = reading;
    //if(encA != encB){ Serial.print(encA);Serial.print(" ");Serial.println(encB);} // test print
    // check if A has gone from high to low
    if (encA != encB) {
    if( last_encA == encA && last_encB == encB) dir = ! dir; // reverse direction
      
      last_encA = encA;
      last_encB = encB;  
      if (dir)
      {       
       // clockwise
          reading += changeIncrement;
        }
      else 
      {
        // anti-clockwise
          reading -= changeIncrement;
      }
      
    // restrain the reading value  
    if( reading < 0) reading = 0;
    if( reading > 127) reading = 127;

    //Serial.print("reading  "); // test
    //Serial.println(reading);   // test
    while(encA != encB){    // wait until transitory condition has passed
      delay(20);   // debounce delay
      encA = digitalRead(pinA); 
      encB = digitalRead(pinB); 
      }
    }
    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
 if( moving ){ // ignore any echo
    // just indicate that a CC message has been put into a sink by flashing the onboard LED
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
   }
 else {
  // do what you would normally do with a control message
   // just indicate that a CC message has been received by flashing an external LED
   digitalWrite(ledPin2, HIGH);
   delay(20);
   digitalWrite(ledPin2, LOW);
 }
}
 
void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity

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