Hello all,
I am trying to take an array, and output it on bit at a time to a shift register.
and this works, But it appears to be off by one position. =/
Say I have 16 inputs and 2 shift registers in series.
Writing the array {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1} using a for loop
I would expect to see 1000000000000101 output on my led's
but instead I see 0000000000001011
Before you all get critical of my not using shiftOut. The reason, I am not using it has to do with the end goal for this project, which involves monitoring button pushes, and setting state changes, across a wide array of multiplexers for the purpose of monitoring a gas handling system in a piece of equipment we are seeking to control. using arrays, seems to be a bit cleaner way of handling all the states in terms of what i will have to do to it later.
Thank you for any help you can provide.
Below is my code
The input is being read from a CD4067 and the output is being sent to a 595 shift register.
/*
* CD4067 multiplexer attached as follows:
- address pin A: digital I/O 2
- address pin B: digital I/O 3
- address pin C: digital I/O 4
- address pin D: digital I/O 5
- input/output pins: digital I/O pin 6,7
- LEDs attached from each of the CD4067's output channels
to ground
*/
// put the address pin numbers in an array
// so they're easier to iterate over:
//shift register pins
int data = 11;
int clock = 12;
int latch = 8;
const int ABCD[] = {2, 3, 4, 5};
int currentState[16] ;
int inputState = 0;
int channel = 0;
// the output pin channel (mux's input):
void setup() {
Serial.begin(9600);
for (int abcd = 2; abcd < 6; abcd++) {pinMode(abcd, OUTPUT);}
pinMode(6,INPUT); //Input
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
void loop() {
// iterate over the 16 channels of the multiplexer:
digitalWrite(latch, 0);
for (channel = 0; channel < 16; channel++) {
// chooses 0-15, sets the channel pins based on the channel you want, iterates over the number of pins you're using:
addressSet(channel);
inputState = digitalRead(6);
if(inputState == 1){
delay(100); //debounce
inputState = digitalRead(6);
if(inputState == 1){
if(currentState[channel] == 0){currentState[channel] = 1;}
else{currentState[channel] = 0;}
delay(100);
}
}
digitalWrite(clock,1);
digitalWrite(data, currentState[channel]);
digitalWrite(clock, 0);
Serial.print(currentState[channel]);
}
Serial.println("");
digitalWrite(latch,1);
}
void addressSet (int Channel){
for (int PIN = 0; PIN < 4; PIN++) {
// chooses ABCD channel array value # 0,1,2,3 -> pins 2,3,4,5
// forms essentially a 0000 byte, representing channels 0-15 in binary
// sets the ABCD pins accordingly, to read from a particular channel
digitalWrite(ABCD[PIN],bitRead(Channel, 3-PIN));
}
}