// fade LED_BUILTIN

int i, speed = 1000;
bool j;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (i > speed) { // end of cycle
    j = !j; // change phase
    i = 0; // restart cycle
  }

  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));  // start with HIGH level
  switch (j) {
    case (0):
      delayMicroseconds(speed * 0 + i);  // % duty cycle @ 1KHz
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      delayMicroseconds(speed * 1 - i);
      break;
    case (1):
      delayMicroseconds(speed * 1 - i);
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      delayMicroseconds(speed * 0 + i);
      break;
  }

  // digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));  // start with HIGH level
  // if (j)                               // odd (1) phase
  //   delayMicroseconds(speed * j - i);  // % duty cycle @ 1KHz
  // else                                 // even (0) phase
  //   delayMicroseconds(speed * j + i);  // % duty cycle @ 1KHz

  // digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  // if (j)
  //   delayMicroseconds(i);
  // else
  //   delayMicroseconds(speed - i);

  i++;
}