Hello,
I seem to be having an issue with keeping a parameter of mine, to stay a particular data type.
The goal is to have binary 0 or 1. defining particular state of a switch.
The switches are read through a CD4067 multiplexer.
The issue lies in to when I output my data via serial.
The value of 1, is output as 11111111, While 0 is just 0
So the serial is spitting out 8 zeros when everything is off, 00000000, as it should.
and when say switch 2 is on I get 0111111110000000, when it should be 010000000
Below is my code, can anyone see the error i made? I presume it to be related to how i am operating on different things.
Thank you for any responses in advance
/*
* 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 pin: digital I/O pin 6,7
- LEDs attached from each of the CD4067's output channels
to ground
*/
const int ABCD[] = {2, 3, 4, 5};
byte currentState[15];
byte inputState = 0b0;
void setup() {
Serial.begin(9600);
for (int abcd = 2; abcd < 6; abcd++) {pinMode(abcd, OUTPUT);}
pinMode(6,INPUT); //Input
pinMode(7,OUTPUT); //OUTPUT
}
void loop() {
// iterate over the 8 channels of the multiplexer:
for (int 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);
//Serial.println(inputState);
if(inputState == 0b1){
delay(100); //debounce
if(inputState == 0b1){
currentState[channel] = ~currentState[channel];
}
}
digitalWrite(7,currentState[channel]);
//Serial.print(deviceState);
Serial.print(currentState[channel],DEC);
}
Serial.println(" ");
}
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));
}
}