hi i need help with multiple buttons

hi i want to serial print when button is pressed but it should only print when there is change in state of button i wrote a code for that but its not working can anyone help me out.

int sw1 = 5;
int sw2 = 4;
int sw3 = 2;
int sw4 = 3;

int sta1 = 0;
int sta2 = 0;
int sta3 = 0;
int sta4 = 0;

bool st1 = false;
bool st2 = false;
bool st3 = false;
bool st4 = false;
void setup () {
  
  Serial.begin(9600);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  
  }
void loop ()
{
  sta1 = digitalRead(sw1);
  sta2 = digitalRead(sw2);
  sta3 = digitalRead(sw3);
  sta4 = digitalRead(sw4);
  if (sta1 == HIGH && st1 == false){
    Serial.write("sw1 on");
    st1 = true;
    delay(1);
  }
    else if (sta1 == LOW && sta1 == true){
      Serial.write("sw1 off");
      st1 = false;
      delay(1);
    }

   if (sta2 == HIGH && st2 == false){
    Serial.write("sw2 on");
    st2 = true;
    delay(1);
  }
    else if (sta2 == LOW && sta2 == true){
      Serial.write("sw2 off");
       st2 = false;
      delay(1);
    }

   if (sta3 == HIGH && st3 == false){
    Serial.write("sw3 on");
     st3 = true;
    delay(1);
  }
    else if (sta3 == LOW &&  sta3 == true){
      Serial.write("sw3 off");
       st3 = false;
      delay(1);
    }
   if (sta4 == HIGH &&  st4 == false){
    Serial.write("sw4 on");
    st4 = true;
    delay(1);
  }
    else if (sta4 == LOW&& sta4 == true){
      Serial.write("sw4 off");
      st4 = false;
      delay(1);
    }
    
}
    else if (sta1 == LOW && sta1 == true){

Can sta1 ever be LOW and true at the same time ?

thank you so much this saved my day :slight_smile:
here is my final working code

int sw1 = 5;
int sw2 = 4;
int sw3 = 2;
int sw4 = 3;

int sta1 = 0;
int sta2 = 0;
int sta3 = 0;
int sta4 = 0;

bool st1 = false;
bool st2 = false;
bool st3 = false;
bool st4 = false;
void setup () {
  
  Serial.begin(9600);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  
  }
void loop ()
{
  sta1 = digitalRead(sw1);
  sta2 = digitalRead(sw2);
  sta3 = digitalRead(sw3);
  sta4 = digitalRead(sw4);
  if (sta1 == HIGH && st1 == false){
    Serial.write("sw1 on");
    st1 = true;
    delay(1);
  }
    else if (sta1 == LOW && st1 == true){
      Serial.write("sw1 off");
      st1 = false;
      delay(1);
    }

   if (sta2 == HIGH && st2 == false){
    Serial.write("sw2 on");
    st2 = true;
    delay(1);
  }
    else if (sta2 == LOW && st2 == true){
      Serial.write("sw2 off");
       st2 = false;
      delay(1);
    }

   if (sta3 == HIGH && st3 == false){
    Serial.write("sw3 on");
     st3 = true;
    delay(1);
  }
    else if (sta3 == LOW &&  st3 == true){
      Serial.write("sw3 off");
       st3 = false;
      delay(1);
    }
   if (sta4 == HIGH &&  st4 == false){
    Serial.write("sw4 on");
    st4 = true;
    delay(1);
  }
    else if (sta4 == LOW&& st4 == true){
      Serial.write("sw4 off");
      st4 = false;
      delay(1);
    }
    
}

Your code looks to be too long to me.

if (sta1 != st1){
  Serial.write(sta1 ? "sw1 on" : "sw1 off");
  st1 = sta1;
  delay(1);
}

Repeat for all switches.

Even better use an array of switch pins and a loop.

This is what the code would look like using arrays and loops. I also added switch debounce. You can easily change the number of switch pins and their order by changing the array of pin numbers. (If you want the switches to be known by a name rather than a number you could make an array of pin names.)

I use the internal pull-up resistors so you don't need external pull-up or pull-down resistors. Connect the switches between the pin and Ground. The pin will read HIGH (due to the pull-up) when the switch is OPEN and read LOW when the switch is closed.

const byte SwitchPins[] = {5, 4, 2, 3};
const byte SwitchCount = sizeof SwitchPins / sizeof SwitchPins[0];
const unsigned long DebounceInterval = 15;


boolean LastState[SwitchCount];  // Defaults to 'false'


void setup ()
{
  Serial.begin(115200);


  for (byte i = 0; i < SwitchCount; i++)
    pinMode(SwitchPins[i], INPUT_PULLUP); // Active LOW: Switch between pin and Ground
}


void loop ()
{
  // For each switch...
  for (byte i = 0; i < SwitchCount; i++)
  {
    bool state = digitalRead(SwitchPins[i]) == LOW; // Active LOW: Switch between pin and Ground
    static unsigned long lastStateChange = 0;
    unsigned long currentMillis = millis();


    // Looks for state change (with debounce)
    if (state != LastState[i] && currentMillis - lastStateChange > DebounceInterval)
    {
      lastStateChange = currentMillis;
      LastState[i] = state;


      Serial.print("Switch ");
      Serial.print(i + 1);
      if (state)
      {
        // Switch just closed
        Serial.println(" Closed");
      }
      else
      {
        // Switch just opened
        Serial.println(" Open");
      }
    }  // End: if (state != LastState
  } // End: for (byte i=0;
}