Hi there,
I have a Pro Mini attempting to talk to 4 4051s, and have some odd behaviour:
When selecting inputs 0, 1, 2, ..., 7 I actually get inputs 1, 2, 3, ..., 0.
I've tried getting the arduino to give me debug info via serial (hopefully clear from the code, as I'm having trouble thinking of how to describe it), but it's not making any sense to me...
Digital pins 6, 7, 8 are connected to the select pins on all 4051s, and each 4051 output has it's own input to the arduino, via an RC debounce (hence the 2ms delay on each time round the read loop - I now realise why software debounce is preferred!)
// inout pin numbers
const byte inA = 5; // Mux outs -> Arduino ins
const byte inB = 4;
const byte inC = 3;
const byte inD = 2;
const byte mux[3] = {8, 7, 6}; // Mux control lines
// Switch positions
byte switchesA[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // 8 switches on inA - switchesA[0] is encoder switch
byte switchesB[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // 8 switches on inB - switchesB[0] is encoderA pulse
byte switchesC[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // 8 switches on inC - switchesC[0] is encoderB pulse
byte switchesD[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // 8 switches on inD
void setup() {
pinMode(inA, INPUT_PULLUP);
pinMode(inB, INPUT_PULLUP);
pinMode(inC, INPUT_PULLUP);
pinMode(inD, INPUT_PULLUP);
pinMode(mux[0], OUTPUT);
pinMode(mux[1], OUTPUT);
pinMode(mux[2], OUTPUT);
Serial.begin(9600);
}
void loop() {
for(byte i=0; i <= 7; i++) {
for(byte j=2; j <3; j--) { //write to mux control lines (Done backwards just to help me visualise the counting more easily)
if(bitRead(i, j)) {
digitalWrite(mux[j], HIGH);
Serial.print(1);
// Serial.print(mux[j]);
// Serial.print("HIGH");
// Serial.print(i);
// Serial.print('\t');
}
else {
digitalWrite(mux[j], LOW);
Serial.print(0);
// Serial.print(mux[j]);
// Serial.print("LOW");
// Serial.print(i);
// Serial.print('\t');
}
// Serial.print(i);
// Serial.print(j);
// Serial.print("\t");
}
switchesA[i] = digitalRead(inA);
switchesB[i] = digitalRead(inB);
switchesC[i] = digitalRead(inC);
switchesD[i] = digitalRead(inD);
Serial.print(' ');
Serial.print(i);
Serial.print(' ');
Serial.print(switchesA[i]);
Serial.print(switchesB[i]);
Serial.print(switchesC[i]);
Serial.print(switchesD[i]);
Serial.print('\t');
delay(10);
// Serial.print('\n');
}
// for(byte i=0; i <= 7; i++)
// Serial.print(switchesA[i]);
//
// Serial.print('\t');
//
// for(byte i=0; i <= 7; i++)
// Serial.print(switchesB[i]);
//
// Serial.print('\t');
//
// for(byte i=0; i <= 7; i++)
// Serial.print(switchesC[i]);
//
// Serial.print('\t');
//
// for(byte i=0; i <= 7; i++)
// Serial.print(switchesD[i]);
Serial.print('\n');
delay(200);
}
Any suggestions would be appreciated!