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