Rotary encoder ending CC messages.
Hi, i´m new to Arduino and i just make this little piece of code to read a rotary encoder and send cc messages based on the reads, at this time the code is working as expected, i hope this could be useful to other people.
The code is based on a sketch by Max Wolf using the Arcore fork by Rkistner to send midi over usb with Arduino Leonardo you can google it to use with some atmega32u4 boards.
------------------------------------------------------------------------------------
int PinA = 0; //declara las entradas del encoder
int PinB = 1;
int PinALast = LOW; // declara el ultimo valor de A
int a = LOW;
int b = LOW;
void setup() {
pinMode (PinA,INPUT);
pinMode (PinB,INPUT);
digitalWrite (PinA, HIGH);
digitalWrite (PinB, HIGH);
}
// this is the part provided by Rkistner.
void CC(byte channel, byte ccnumb, byte value) {
MIDIEvent CC = {0x0B, 0xB0 | channel, ccnumb, value};
MIDIUSB.write(CC);
}
void loop() { // the loop will read the values from the encoder
a = digitalRead(PinA);
b = digitalRead(PinB);
delay (3); // delay to debounce, No needed if you connect a .10uf capacitor
// between each pin (A, B) and the common pin
if (a != PinALast) { // compares the pin with it's last value, if it has changed then...
if(a == b) //if they are equals
{
CC(0, 16, 127); //send a 127 value on channel 0 to the midi CC channel number 16
}
else //otherwise send a 0 value on channel 0 to the midi CC channel number 16
{
CC(0, 16, 0);
}
PinALast = a;
}
}
-----------------------------------------------------------------------------------
You can see the code working here: https://www.youtube.com/watch?v=ZpI2iBhp52w