// Pin definitions (note the use if uint8_t instead of int to save space on Arduino micro's memory)
const uint8_t encoder1PinA {2}; // Interrupt pin for A
const uint8_t encoder1PinB {4}; // Interrupt pin for B
const uint8_t encoder1Btn {5};
const uint8_t twoWayPin1 {11};
const uint8_t twoWayPin2 {12};
// Used to store rotary encoder values
int encoder1Value {0}; // Counter for the rotary encoder
int8_t encoder1Dir {0};
// Rotary encoder internal value
uint8_t encoder1BtnValue {0}; // Counter for the rotary encoder
uint8_t lastA {0}; // Last state of A
uint8_t lastB {0}; // Last state of B
uint8_t twoWayPinValue {0};
int8_t arrayOffset {0};
const uint8_t numOfRotaryInputs {3};
const uint8_t numOfTwoWayInputs {2};
//std::array<u_int8_t, numOfRotaryInputs*numOfTwoWayInputs> joyBtn {0};
char joyBtn[numOfRotaryInputs*numOfTwoWayInputs] {'0'};
// Use class as initialise Rotary encoder, not useful for now
// but if you have many rotary encoder it makes your code much clearer.
// class RotaryEncoder {
// // Each Rotary Encoder will have two 'sub' rotary encoder
// uint8_t m_pinA1 {};
// // const uint8_t m_pinA2 {};
// uint8_t m_pinB1 {};
// // const uint8_t m_pinB2 {};
// uint8_t m_btn {};
// int8_t m_rotationDir {0};
// uint8_t m_btnState {0};
// uint8_t m_lastA1 {0};
// // uint8_t m_lastA2 {0};
// uint8_t m_lastB1 {0};
// // uint8_t m_lastB2 {0};
// void setPinNumber(uint8_t pinA1, uint8_t pinB1, uint8_t btn);
// };
// void RotaryEncoder::setPinNumber(uint8_t pinA1, uint8_t pinB1, uint8_t btn) {
// m_pinA1 = pinA1;
// m_pinB1 = pinB1;
// m_btn = btn;
// }
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// RotaryEncoder enc1, enc2;
// enc1.setPinNumber(2, 4, 5);
// Set encoder pins as inputs
pinMode(encoder1PinA, INPUT_PULLUP);
pinMode(encoder1PinB, INPUT_PULLUP);
pinMode(encoder1Btn, INPUT_PULLUP);
pinMode(twoWayPin1, INPUT_PULLUP);
pinMode(twoWayPin2, INPUT_PULLUP);
// Attach interrupts to the encoder pins
attachInterrupt(digitalPinToInterrupt(encoder1PinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder1PinB), updateEncoder, CHANGE);
}
void loop() {
// Check the button state
encoderButtonState(encoder1Btn, encoder1BtnValue);
//Serial.println(encoder1BtnValue);
twoWayState(twoWayPin1, twoWayPin2, twoWayPinValue);
//Serial.println(twoWayPinValue);
// When two ways pin pushes to 1, it shifts the array by number of rotary inputs
arrayOffset = (twoWayPinValue == 0) ? 0 : numOfRotaryInputs;
//Serial.println(arrayOffset);
if (encoder1Value == -1) {
//Serial.println(0+arrayOffset);
joyBtn[0+arrayOffset] = '1';
} else if (encoder1Value == 1) {
joyBtn[1+arrayOffset] = '1';
} else if (encoder1BtnValue == 1) {
joyBtn[2+arrayOffset] = '1';
} else {
for (int i = 0; i < (numOfRotaryInputs*numOfTwoWayInputs); ++i) {
joyBtn[i] = '0';
}
}
// delay(1000);
//}
Serial.println(joyBtn);
encoder1Value = 0; // Reset the direction after reading
delay(100); // Delay for readability in serial monitor
}
void encoderButtonState(uint8_t encoderButton, uint8_t &encoderButtonValue) {
if (digitalRead(encoderButton) == 0) { // Button is pressed (assuming active-0 configuration)
if (!encoderButtonValue) { // Only act on the first detection of button press
encoderButtonValue = 1;
}
} else {
encoderButtonValue = 0; // Reset button pressed state
}
}
void twoWayState(uint8_t twoWayPin1, uint8_t twoWayPin2, uint8_t &twoWayPinValue){
uint8_t pin1 = digitalRead(twoWayPin1);
uint8_t pin2 = digitalRead(twoWayPin2);
twoWayPinValue = (pin1 == 1 && pin2 == 0) ? 0 : 1;
}
// Interrupt service routine for the rotary encoder
void updateEncoder() {
uint8_t currentA = digitalRead(encoder1PinA);
uint8_t currentB = digitalRead(encoder1PinB);
if (currentA != lastA || currentB != lastB) { // Only process if there is a change
// Determine the direction of rotation
if (lastA == 0 && currentA == 1) {
if (currentB == 0) {
encoder1Value=1;
} else {
encoder1Value=-1;
}
} else if (lastA == 1 && currentA == 0) {
if (currentB == 1) {
encoder1Value=1;
} else {
encoder1Value=-1;
}
}
// Save the current state for the next change
lastA = currentA;
lastB = currentB;
}
}