maintaining a constant data type in a sketch

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));
    }
}

currentState[channel] = ~currentState[channel];

This line looks wrong to me try currentState[channel] = inputState;

Mark

I also think that your debounce code is wrong, First the delay is far to long at 1/10 of a second and second i think you need to read the input a second time after the bounce has finished, as your polling the inputs and you don't now if the input went from high to low or low to high.

By the way you don't need all the b's just 0 and 1 will do ( or high/low or true/false ).

Mark

There are lots of problems here.

How many channels, 8 or 16 ?

Where you read inputstate, and then wait 0.1 seconds, you don't try reading inputstate again. So the variable inputstate will not have changed.

You then test if the input is high, and if you do, you invert the current recorded value of the input. This means that, if the input goes high and stays high, you will be flipping the recorded state every time you go through the loop. That doesn't make a lot of sense, unless you actually intended to do that.

You refer to your array with elements 0..15. But you declare it with [15] elements. That's incorrect. 0..15 is [16] elements and that is how you should declare the array.

Finally, if you flip the state of a byte containing 0, you will get 11111111, so if you printed that in binary, that's what you would see.

Finally, if you flip the state of a byte containing 0, you will get 11111111, so if you printed that in binary, that's what you would see.

OF COURSE !!!!!

Mark

Finally, if you flip the state of a byte containing 0, you will get 11111111, so if you printed that in binary, that's what you would see.

Apparently I need to go read more about binary, thank you for clarifying this for me

I will adjust the debounce code, according to your suggestions., reading the inputstate twice, etc.

Their are actually 16, before I had 8, which is why the comments say 8, I have adjusted this.

To address some confusion,

This line looks wrong to me try currentState[channel] = inputState;

Let me outline the sketches goal.
To be able to switch the state of a particular array element based on a momentary push buttom, but maintin the state.

So lets say I push a button 1, inputState registers a 1 on channel 1.
Then I invert currentState[0] to the opposite of whatever its origional state was and keeping it that way, until button 1 is pushed again. (the goal was to not have to constantly hold down the button, but have a single push button)

As you have pointed out, ~ is turning 0 into 11111111, apparently as it should, but as I was unaware of this property thats what was messing me up.
So i guess I will write an if statement to do the switching instead.

To say it another way, each value 0 or 1 representes an individual device state, see the below psudo code thing

initially
device states = 00000000
current states = 00000000
Start state, Nothing has been pressed, nothing is registered

next
device states = 10000000
current states = 10000000
I pressed a button, the current state records the button press. 

next 
device states = 00000000
current states = 10000000
current state continues to tell me if i had or had not pressed a button in the past 

Next
device states = 10000000
current states = 00000000
I pushed the button again, the current state records the button press.

finally
device states = 00000000
current states = 00000000
current state continues to tell me if i had or had not pressed a button in the past

Make sense?

Thank you all again, for the help.
I am going to go implement these changes, and I will report back later today =)