Hi All
I have code for an encoder that works on an old ++2 and a new Teensy 4 but will not work on a 3.2. The pullups measure ok. I have tried various pins but still no joy. Is there a known issue when it comes to rotary encoders on 3.2 boards?
Regards,
Paul
I have code for an encoder that works on an old ++2 and a new Teensy 4 but will not work on a 3.2. The pullups measure ok. I have tried various pins but still no joy. Is there a known issue when it comes to rotary encoders on 3.2 boards?
Regards,
Paul
Code:
#include <Encoder.h>
#include <USB-MIDI.h>
// Rotary Encoder pins
const int encoderPinA = 2;
const int encoderPinB = 3;
const int encoderButtonPin = 4; // Optional if using the encoder button
// MIDI channel
const int midiChannel = 7; // MIDI channels are 0-15, so channel 7 corresponds to MIDI channel 8
// Encoder object
Encoder myEncoder(encoderPinA, encoderPinB);
int lastEncoderValue = 0;
int currentNote = 60; // Middle C
void setup() {
// Initialize the encoder
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(encoderButtonPin, INPUT_PULLUP); // Optional if using the encoder button
// Start MIDI communication
usbMIDI.begin();
}
void loop() {
// Read the encoder
int newEncoderValue = myEncoder.read() / 4; // Divide by 4 to reduce sensitivity
if (newEncoderValue != lastEncoderValue) {
// Determine the direction of rotation
if (newEncoderValue > lastEncoderValue) {
// Rotate clockwise: Note On
sendMidiNoteOn(currentNote, 127); // 127 is the maximum velocity
} else {
// Rotate counter-clockwise: Note Off
sendMidiNoteOff(currentNote, 0); // 0 velocity to indicate Note Off
}
// Update the last encoder value
lastEncoderValue = newEncoderValue;
}
// Read the encoder button (if used)
if (digitalRead(encoderButtonPin) == LOW) {
// Handle the encoder button press
// For example, you can change the note or send another MIDI message
}
// Delay for debounce (optional)
delay(10);
}
void sendMidiNoteOn(byte note, byte velocity) {
usbMIDI.sendNoteOn(note, velocity, midiChannel);
}
void sendMidiNoteOff(byte note, byte velocity) {
usbMIDI.sendNoteOff(note, velocity, midiChannel);
}