Using ELSE in nested IF statement

I need to test five inputs individually to see if they change states. I test each input with three if statements. The first one tests for a change. Then it waits for 10ms and tests it again to see if it stayed the same or not (my version of de-bouncing). Then, if it did stay changed, it tests if the input is high or low, sets a bit in a variable (call it "b"), and does the same with the other four inputs. After all inputs are tested, the variable is tested against another variable (let's call it "a") to see if they are the same. If they are the same, nothing happens and the process restarts. If they are different, "a" is set to "b" and "b" is sent out on the serial port.

Snippet of code:

  if(digitalRead(land) != lastB){
    delay(db);
    if(digitalRead(land) != lastB){
      if(digitalRead(land) == 1){
        switches = switches | 0b00000010;
      }
      else{
        switches = switches & 0b11111101;
      }
    }
  }

This works if the state goes from 0 to 1. But it will not work if it goes from 1 to 0.

Full code:

/*
 * This program will read five switches and send their states to a computer program over serial port.
 * If the switches are not changed, no data is sent.
 * If they are changed, their new states are stored and a new data byte will be constructed.
 * Once the new byte is constructed (after reading all the switches), it will be sent.
 */

// variables
bool lastA = 0; // the last known state of switch one
bool lastB = 0; // the last known state of switch two
bool lastC = 0; // the last known state of switch three
bool lastD = 0; // the last known state of switch four
bool lastE = 0; // the last known state of switch five
byte switches = 0; // data byte to send to the PC
byte lByte = 0b01000000; // to test if the byte changed

//constants
const int bcn = 3;
const int land = 4;
const int taxi = 5;
const int nav = 6;
const int strb = 7;
const int db = 10;

void setup() {
  // begin a serial communication
  Serial.begin(9600);

  // set the left-most bits to 010.
  switches = switches | 0b01000000;

}

void loop() {
  // read switches, send data if any are changed
  if(digitalRead(bcn) != lastA){
    delay(db);
    if(digitalRead(bcn) != lastA){
      if(digitalRead(bcn) == 1){
        switches = switches | 0b00000001;
      }
      else{
        switches = switches & 0b11111110;
      }
    }
  }

  if(digitalRead(land) != lastB){
    delay(db);
    if(digitalRead(land) != lastB){
      if(digitalRead(land) == 1){
        switches = switches | 0b00000010;
      }
      else{
        switches = switches & 0b11111101;
      }
    }
  }

  if(digitalRead(taxi) != lastC){
    delay(db);
    if(digitalRead(taxi) != lastC){
      if(digitalRead(taxi) == 1){
        switches = switches = switches | 0b00000100;
      }
      else{
        switches = switches = switches & 0b11111011;
      }
    }
  }

  if(digitalRead(nav) != lastD){
    delay(db);
    if(digitalRead(nav) != lastD){
      if(digitalRead(nav) == 1){
        switches = switches | 0b00001000;
      }
      else{
        switches = switches & 0b11110111;
      }
    }
  }

  if(digitalRead(strb) != lastE){
    delay(db);
    if(digitalRead(strb) != lastE){
      if(digitalRead(strb) == 1){
        switches = switches | 0b00010000;
      }
      else{
        switches = switches & 0b11101111;
      }
    }
  }

  if(switches != lByte){
    Serial.println(switches,BIN);
    lByte = switches;
  }
}

You are using lastA, lastB etc… to hold your last known state, yet you do not actually update them at any point in your code. They begin as 0, are compared to 1, so the first pass works, but are never updated to be 1.

Also switches &= ~(0b00000001); is better and more readable.

While I advise using proper debounce it sounds like you know it isn’t great to use delay and are using this as a temporary process or something.

It figures it'd be something dumb like that. I know using delays is a poor method of debouncing, but I have no idea of what else to do. It looked like the examples I found weren't much different. Also, I'm trying to keep part count low, so no caps.

Have you looked at the Debouce example code?

You could literately remove.

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }

with your

      if(buttonState == HIGH){
        switches = switches | 0b00000010;
      }
      else{
        switches = switches & 0b11111101;
      }

and you would be 20% done.

You would just need 5 timer variables, lastButtonStates & buttonStates.

I much prefer brevity, for instance:

      if(buttonState == HIGH){
        switches = switches | 0b00000010;
      }
      else{
        switches = switches & 0b11111101;
      }

Is actually inserting a single bit in the variable switches, not really a conditional
operation, but the store of one bit, which can be done thus:

    switches = (switches & 0b11111101) | (buttonState << 1) ;

Because HIGH and LOW are synonyms for 1 and 0.