int Steps = 4;
int DMS = A4;
int UMS = A5;
int i;
int DownFlag = 0;
int UpFlag = 0;

void setup() {
  for (i = 2 ; i <= Steps + 1; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(DMS, INPUT);
  pinMode(UMS, INPUT);
}
void DownLedOn() {
  for (i = 2; i <= Steps + 1 ; i++) {
    digitalWrite(i, HIGH);
    delay(500);
  }
}
void DownLedOff() {
  for (i = 2; i <= Steps + 1 ; i++) {
    digitalWrite(i, LOW);
    delay(500);
  }
  // break;
}
void UpLedOn() {
  for (i = Steps + 1; i >= 2 ; i--) {
    digitalWrite(i, HIGH);
    delay(500);
  }
}
void UpLedOff() {
  for (i = Steps + 1; i >= 2 ; i--) {
    digitalWrite(i, LOW);
    delay(500);
  }
}

void DownSideMotion() {
  if ((digitalRead(DMS) == HIGH) && (DownFlag == 0)) {
    UpLedOn();
    DownFlag = 1;
  }
  if ((digitalRead(UMS) == HIGH) && (DownFlag == 1)) {
    UpLedOff();
    DownFlag = 0;
    UpFlag = 0;
  }
}

void UpSideMotion() {
  if ((digitalRead(UMS) == HIGH) && (UpFlag == 0)) {
    DownLedOn();
    UpFlag = 1;
  }
  if ((digitalRead(DMS) == HIGH) && (UpFlag == 1)) {
    DownLedOff();
    UpFlag = 0;
    DownFlag = 0;
  }
}


void loop() {
  DownSideMotion();
  UpSideMotion();
}