// Pin Definitions
const int commonAPin = 4; // Common pin for all A signals
const int encoderBPins[] = {5, 6, 7, 8, 9, 10, 16, 14}; // B pins for each encoder
const int numEncoders = 2;
// State tracking
int lastAState;
int lastBStates[numEncoders];
long positions[numEncoders] = {0}; // To store the positions of each encoder
void setup() {
Serial.begin(9600);
// Initialize the A pin as input
pinMode(commonAPin, INPUT_PULLUP);
lastAState = digitalRead(commonAPin);
// Initialize all B pins as input
for (int i = 0; i < numEncoders; i++) {
pinMode(encoderBPins[i], INPUT_PULLUP);
lastBStates[i] = digitalRead(encoderBPins[i]);
}
}
void loop() {
readEncoders();
/*
int aState = digitalRead(commonAPin); // Read the current state of A
if (aState != lastAState)
{
Serial.print("aState: ");
Serial.println(aState);
// Check each encoder
for (int i = 0; i < numEncoders; i++)
{
int bState = digitalRead(encoderBPins[i]); // Read the current state of B
// Determine the direction of rotation based on the B state
if (bState != lastBStates[i])
{
if (bState != aState)
{
positions[i]++;
}
else
{
positions[i]--;
}
// Print the position
Serial.print("Encoder ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(positions[i]);
}
// Update the last B state
lastBStates[i] = bState;
}
// Update the last A state
lastAState = aState;
}
*/
delay(5); // Short delay to debounce and reduce serial output noise
}
// Function to read and process the encoders
void readEncoders0() {
int aState = digitalRead(commonAPin); // Read the common A pin state
// Iterate over each encoder
for (int i = 0; i < numEncoders; i++) {
int bState = digitalRead(encoderBPins[i]); // Read the individual B pin state
// Check if A is LOW and detect valid state transitions of B to count steps
if (aState == LOW && bState != lastBStates[i]) {
int delta = 0; // Delta value to track relative movement
// Determine the direction based on A and B pin transitions
if (lastAState == LOW && bState == HIGH) {
delta = 1; // Clockwise step
} else if (lastAState == LOW && bState == LOW) {
delta = -1; // Counterclockwise step
}
Serial.print("Encoder ");
Serial.print(i+1);
Serial.print(" - has changed to: ");
Serial.println(delta);
// Only update and send MIDI if there is a detected movement
if (delta != 0) {
positions[i] += delta; // Update the encoder's position
// sendEncoderMidiEvent(i, delta); // Send MIDI event based on the delta
}
}
// Update last states
lastBStates[i] = bState;
}
// Update last A state
lastAState = aState;
}
// Function to read and process the encoders
void readEncoders() {
int aState = digitalRead(commonAPin); // Read the common A pin state
// Iterate over each encoder
for (int i = 0; i < numEncoders; i++) {
int bState = digitalRead(encoderBPins[i]); // Read the individual B pin state
// Process a full step when a complete A -> B -> A transition occurs
if (aState == LOW && bState != lastBStates[i]) {
// Encoder turns clockwise when B goes HIGH after A is LOW
if (lastBStates[i] == LOW && bState == HIGH) {
positions[i]++;
// sendEncoderMidiEvent(i, 1);
}
// Encoder turns counterclockwise when B goes LOW after A is LOW
else if (lastBStates[i] == HIGH && bState == LOW) {
positions[i]--;
// sendEncoderMidiEvent(i, -1);
}
Serial.print("Encoder ");
Serial.print(i+1);
Serial.print(" - has changed to: ");
Serial.println(positions[i]);
}
// Update the last state of B
lastBStates[i] = bState;
}
// Update the last A state
lastAState = aState;
}