Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
Update:
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton
saves the number of the most recent button:
void setup(){
pinmode(LED1PIN, OUTPUT);
pinmode(LED2PIN, OUTPUT);
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
}
void loop(){
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
{
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
}
else if(lastButton == 2)
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
}
}