so i have this sketch ive been messing with to get an encoder to send midi program changes (ideally it will move up or down one program at a time).
in the sketch i had it set to go from program 70-60, and it worked fine when i was doing it with a switch in a different sketch.
now when i move the rotary encoder 1 click this is the midi data sent out:
Program Change, Channel: 1, Number: 66
Program Change, Channel: 1, Number: 87
Program Change, Channel: 1, Number: 68
Program Change, Channel: 1, Number: 13
Program Change, Channel: 1, Number: 10
Program Change, Channel: 1, Number: 66
Program Change, Channel: 1, Number: 87
Program Change, Channel: 1, Number: 68
Program Change, Channel: 1, Number: 13
Program Change, Channel: 1, Number: 10
could anyone tell me why one click of the encoder spams out that much? to top it off all the numbers are random rather than in order.
here is the sketch ive butchered:
#include <MIDI.h>
// Digital pin definitions
enum enDigitalPins
{
// Rotary encoder input lines
dpInEncoderA=8,
dpInEncoderB=10,
dpInEncoderPress=12,
};
int blackButton = 8;
int redButton = 10;
int blackButtonState = 0;
int redButtonState = 0;
static void _ResetPins()
{
// Rotary encoder input lines
// Configure as input, turn on pullup resistors
pinMode(dpInEncoderA, INPUT);
digitalWrite(dpInEncoderA, HIGH);
pinMode(dpInEncoderB, INPUT);
digitalWrite(dpInEncoderB, HIGH);
pinMode(dpInEncoderPress, INPUT);
digitalWrite(dpInEncoderPress, HIGH);
}
void _lowlevel_ReadEncoder(int &rotate, int& press)
{
rotate = (digitalRead(dpInEncoderB) * 2) + digitalRead(dpInEncoderA);
press = digitalRead(dpInEncoderPress);
}
void ReadEncoder()
{
int Position, Press;
int isForward = 0;
_ResetPins();
Serial.println("Reading the encoder... press a key to abort.");
_lowlevel_ReadEncoder(Position, Press);
while (!Serial.available())
{
int Position2, Press2;
do
{
_lowlevel_ReadEncoder(Position2, Press2);
} while ((Position2 == Position) && (Press2 == Press));
if (Position2 != Position)
{
// "Forward" is shown by the position going from (0 to 1) or (1 to 3)
// or (3 to 2) or (2 to 0). Anything else indicates that the user is
// turning the device the other way. Remember: this is Gray code, not
// binary.
int isFwd = ((Position == 0) && (Position2 == 1)) ||
((Position == 1) && (Position2 == 3)) ||
((Position == 3) && (Position2 == 2)) ||
((Position == 2) && (Position2 == 0));
Serial.println(isFwd ? "FWD" : "BWD");
}
if (Press2 != Press)
{
Serial.println(Press ? "Press" : "Release");
}
Position = Position2;
Press = Press2;
}
}
void setup()
{
Serial.begin(31250);
pinMode(blackButton, INPUT);
pinMode(redButton, INPUT);
Serial.println("Ready to begin");
// configure the pins
_ResetPins();
}
void loop()
{
ReadEncoder();
int tempBlackState = digitalRead(blackButton);
if(blackButtonState != tempBlackState){
blackButtonState = tempBlackState;
if (blackButtonState == HIGH) {
MIDI.send(NoteOn,64,127,1);
} else {
MIDI.send(NoteOff,64,0,1);
}
}
int tempRedState = digitalRead(redButton);
if(redButtonState != tempRedState){
redButtonState = tempRedState;
if (redButtonState == HIGH) {
MIDI.send(PC,69,127,1);
} else {
MIDI.send(PC,70,0,1);
}
}
}

