 	// switch pin input
 	if (switchPins[i]>0) { pinMode(switchPins[i],INPUT_PULLUP); }
     // make LED pins outputs+turn off
+	lastLedState[i] = 0;
 	if (onPins[i]>0) {
           pinMode(onPins[i],OUTPUT);
           digitalWrite(onPins[i],LOW);

const int throttlePin = A0;  // P0

const uint8_t nPins = 7; // długość tabel poniżej
const uint8_t switchPins[] = {4, 5, 8, 12, 0, 0, 0};	// Dx: stage, RCS, SAS, gear switch, overheat, lowpower, lowfuel
const uint8_t onPins[] = {0, 13, 9, 0, 0, 0, 0};	// Gx: stage, RCS, SAS, overheat, lowpower, lowfuel ON (green)
const uint8_t offPins[] = {0, 0, 0, 0, 6, 7, 10};	// Rx: stage, RCS, SAS, overheat, lowpower, lowfuel OFF (red)
uint8_t lastPinState[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};

// throttle control
int lastThrottleValue = 0;

    id = Serial.parseInt();  // skip '='
    val = Serial.parseInt(); // skip newline
//    Serial.print(c); Serial.print(id); Serial.println(val);  // echo for ack
	if (id>0 && id<=nPins) {
                nval = lastLedState[id];
                if (c=='G') {
                  nval = nval & 0b11111010;
                  if (val==1) nval=nval | 0x01;
                  if (val==2) nval=nval | 0x01 | 0x04;
                }
                if (c=='R') {
                  nval = nval & 0b11110101;
                  if (val==1) nval=nval | 0x02;
                  if (val==2) nval=nval | 0x02 | 0x08;
                }
		lastLedState[id] = nval;
	}
  }
  }
}

// set all LEDs according to state
void updateLeds() {
  unsigned long currentMillis = millis();
  bool blink = false;
  // is it time for tick?
  if (((unsigned long)(currentMillis - lastBlink) >= blinkInterval)) {
	  blink = true;
	  lastBlink = currentMillis;
  }
//  Serial.print("blink="); Serial.println(blink);
  // update all leds
  for (uint8_t i=0; i<nPins; i++) {
	uint8_t nstate;
	uint8_t gPin = onPins[i];
	if (gPin!=0) {
		nstate = (lastLedState[i] & 0x01) ? HIGH : LOW;
		if (nstate==HIGH && (lastLedState[i] & 0x04) && blink) {
			nstate = !digitalRead(gPin);
		}
//Serial.print("L"); Serial.print(gPin); Serial.print(nstate);
		digitalWrite(gPin,nstate);
	}
	uint8_t rPin = offPins[i];
	if (rPin!=0) {
		nstate = (lastLedState[i] & 0x02) ? HIGH : LOW;
		if (nstate==HIGH && (lastLedState[i] & 0x08) && blink) {
			nstate = !digitalRead(rPin);
		}
//Serial.print("R"); Serial.print(rPin); Serial.print(nstate);
		digitalWrite(rPin,nstate);
	}
  }
}

