Hello, I decided to upgrade to a different Arduino because my project was going to require a lot more pins. on the way I have bumped into an issue. On multiplexing some encoders I discovered that the code I had written on my Leonardo that works perfectly doesn't work on a DUE. It doesn't register at all if I use the same pins on the Due as I did on the Leonardo (digital pins 2 - 11) but register if I move them to digital pins 23 + on the Due. Also, on the Leonardo it will read all 29 encoders perfectly but on the Due it gets weird readings. it will detect multiple encoders moving when I just move one. sometimes the encoders are being read as moving even though nothing is being turned. its all very strange. my initial assumption was maybe power to the muxes? and on checking the PCB I made the issues seem to be focused on the encoders that I routed underneath the SMD Mux components even though they are on a completely different layer. Any help would be much appreciated as I have exhausted my fault find capabilities. Code attached.
byte encLast[29] = {};
//MUX INPUTS
#define BIT1 2
#define BIT2 3
#define BIT3 4
#define BIT4 5
// MUX OUTPUTS
#define ENC1 6
#define ENC2 7
#define ENC3 8
#define ENC4 9
const byte muxRead[29][3] = {
{ 0 , ENC1 , ENC2 },
{ 1 , ENC1 , ENC2 },
{ 2 , ENC1 , ENC2 },
{ 3 , ENC1 , ENC2 },
{ 4 , ENC1 , ENC2 },
{ 5 , ENC1 , ENC2 },
{ 6 , ENC1 , ENC2 },
{ 7 , ENC1 , ENC2 },
{ 8 , ENC1 , ENC2 },
{ 9 , ENC1 , ENC2 },
{ 10 , ENC1 , ENC2 },
{ 11 , ENC1 , ENC2 },
{ 12 , ENC1 , ENC2 },
{ 13 , ENC1 , ENC2 },
{ 14 , ENC1 , ENC2 },
{ 15 , ENC1 , ENC2 },
{ 0 , ENC3 , ENC4 },
{ 1 , ENC3 , ENC4 },
{ 2 , ENC3 , ENC4 },
{ 3 , ENC3 , ENC4 },
{ 4 , ENC3 , ENC4 },
{ 5 , ENC3 , ENC4 },
{ 6 , ENC3 , ENC4 },
{ 7 , ENC3 , ENC4 },
{ 8 , ENC3 , ENC4 },
{ 9 , ENC3 , ENC4 },
{ 10 , ENC3 , ENC4 },
{ 11 , ENC3 , ENC4 },
{ 12 , ENC3 , ENC4 },
};
const byte muxPins[16][4] = {
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //channel 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
{1, 1, 1, 1}, //channel 15
};
void setup() {
SerialUSB.begin(9600);
//INPUT PULLUP MUX
pinMode(ENC1, INPUT_PULLUP);
pinMode(ENC2, INPUT_PULLUP);
pinMode(ENC3, INPUT_PULLUP);
pinMode(ENC4, INPUT_PULLUP);
//ASSIGN OUTPUT PINS
pinMode(BIT1, OUTPUT);
pinMode(BIT2, OUTPUT);
pinMode(BIT3, OUTPUT);
pinMode(BIT4, OUTPUT);
//ASSIGN OUTPUT PINS VALUES
digitalWrite(BIT1, LOW);
digitalWrite(BIT2, LOW);
digitalWrite(BIT3, LOW);
digitalWrite(BIT4, LOW);
}
void loop() {
for (byte i = 0; i < 29 ; i ++) {
byte Pin = muxRead[i][0];
digitalWrite(BIT1, muxPins[Pin][0]);
digitalWrite(BIT2, muxPins[Pin][1]);
digitalWrite(BIT3, muxPins[Pin][2]);
digitalWrite(BIT4, muxPins[Pin][3]);
byte firstPin = digitalRead(muxRead[i][1]);
byte secondPin = digitalRead(muxRead[i][2]);
if (firstPin != encLast[i]) {
SerialUSB.print(i), SerialUSB.print(" : "), SerialUSB.println(encLast[i]);
encLast[i] = firstPin;
}
}
}