// HOLD THE K1 BUTTON FOR MORE THAN AN INSTANT TO CHANGE DIRECTIONS

const byte HOW_MANY_LEDS_TO_LIGHT_UP = 5;
const byte NUM_LIMIT_LED = (1 << (HOW_MANY_LEDS_TO_LIGHT_UP - 1)) - 1;

unsigned int ledValue = 1;

byte k1;
byte k2;
byte k3;
byte k4;

bool directionRight = false;
bool changeDir = false;

void setup() {
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);

  DDRA = 0b11111111;
}

void loop() {
  // read BTNs status
  k1 = digitalRead(7);
  k2 = digitalRead(6);
  k3 = digitalRead(5);
  k4 = digitalRead(4);

  if (k1 == LOW) {
    changeDir = true;
  }  
  
  PORTA = ledValue;

  if (directionRight) {
    if (ledValue < 2) {
      ledValue = NUM_LIMIT_LED + 1;
    } else {
      ledValue >>= 1;
    }
  } else {
    if (ledValue > NUM_LIMIT_LED) {
      ledValue = 1;
    } else {
      ledValue <<= 1;
    }
  }
  
  delay(500);

  // SOFT DEBOUNCE
  if (changeDir) {
    directionRight = !directionRight;
    changeDir = false;
  }
}