Go Down

Topic: How to close/turn OFF MIDI.read(); (Read 1 time) previous topic - next topic

seby20

Dec 17, 2018, 05:47 pm Last Edit: Dec 17, 2018, 08:52 pm by seby20
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

seby20


Grumpy_Mike

#2
Dec 17, 2018, 11:53 pm Last Edit: Dec 17, 2018, 11:54 pm by 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.

seby20

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

Grumpy_Mike

Quote
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.

slipstick

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

seby20

#6
Dec 18, 2018, 01:18 pm Last Edit: Dec 18, 2018, 01:21 pm by seby20
Code: [Select]

#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

Grumpy_Mike

#7
Dec 18, 2018, 02:42 pm Last Edit: Dec 18, 2018, 02:43 pm by 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:-

Code: [Select]
      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

seby20

#8
Dec 18, 2018, 05:05 pm Last Edit: Dec 18, 2018, 06:11 pm by seby20
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:-

Code: [Select]
     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
Code: [Select]

#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


}

Grumpy_Mike

Quote
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

seby20

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

Grumpy_Mike

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:-
Quote
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:-
Quote
I'm a noob in coding.
How on earth can you make a diagnosis like that?

seby20

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

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.

Code: [Select]
#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. 

seby20

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.

Code: [Select]
#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. 
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.

Code: [Select]
#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

Go Up