Hello people,
Yesterday I managed to multiplex 4 rotary encoders using 2 hc4051c multiplexers.
Although everything was going ok when I tried for the first time, after some modifications (disconnected and reconnected my encoders and made an extra Serial.print in my code) only the rotary encoder on pin 7 was playing correctly, all other encoders had constantly their b pin high. Did I burn something?
When I was trying to make an encoder work with my arduino I figured I needed a pullup resistor otherwise I could make any code work.
Now that I have multiplexers in between, although I have set the input pins on the arduino to INPUT_PULLUP where the multiplexers send values, I don't know if this is the proper thing to do.
my code is:
int val;
int encoder0PinA = 5;
int encoder0PinB = 6;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int k = LOW;
byte encoders[4] = {4, 6, 7, 5};
int encoderValues[4] = {0, 0, 0, 0};
int previousEncoderPinAState[4] = {LOW, LOW, LOW, LOW};
int previousEncoderPinBState[4] = {LOW, LOW, LOW, LOW};
int midiNoteValues[8]; // array to hold the midi note number
int durationValues[8];
int velocityValues[8];
bool dirtyFlags[] = { true, true, true, true, true, true, true, true };
int *modeValues[] = { midiNoteValues, durationValues, velocityValues };
//char *notes = "aAbcCdDefFgG";
byte mode = 0;
const byte muxInput = A0; // where the multiplexer in/out port is connected
const byte s0 = 4;
const byte s1 = 3;
const byte s2 = 2;
bool potValueChanged = true; //bool to know if pot value changed, needed for display update
int readSensor (const byte which)
{
// select correct MUX channel on both muxes
digitalWrite (s2, (which & 1) ? HIGH : LOW); // low-order bit
digitalWrite (s1, (which & 2) ? HIGH : LOW);
digitalWrite (s0, (which & 4) ? HIGH : LOW); // high-order bit
//return analogRead (muxInput); // now read the sensor
//read the values on encoderPinA and encoderPinB (the one comes from the first mux, and the other from the other).
n = digitalRead(encoder0PinA);
k = digitalRead(encoder0PinB);
if ((previousEncoderPinAState[which] == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoderValues[which] = encoderValues[which] - 1;
} else {
encoderValues[which] = encoderValues[which] + 1;
}
Serial.print("Encoder ");
Serial.print(which);
Serial.print(" value: ");
Serial.println (encoderValues[which]);
}
previousEncoderPinAState[which] = n;
}
void getPotValues()
{
for (int i = 0; i <= 3; i ++)
{
readSensor(encoders[i]);
}
}
void setup() {
pinMode (encoder0PinA, INPUT_PULLUP);
pinMode (encoder0PinB, INPUT_PULLUP);
pinMode (s0, OUTPUT);
pinMode (s1, OUTPUT);
pinMode (s2, OUTPUT);
Serial.begin (9600);
}
void loop() {
getPotValues();
}
Thank you!