 void updateThrottle() {
   // read the analog in value:
   throttleValue = analogRead(throttlePin);
-  // calibrate
+  // auto calibrate
   if (throttleValue>throttleMax) { throttleMax=throttleValue; }
   if (throttleValue<throttleMin) { throttleMin=throttleValue; }
   // map it to the range of the analog out:
   throttleValue = map(throttleValue, throttleMin, throttleMax, 0, 255);
   // is it different enough from last reading?
-  if (abs(throttleValue-lastThrottleValue)>4) {
+  if (abs(throttleValue-lastThrottleValue)>THROTTLE_THRESHOLD) {
+    if (throttleValue<THROTTLE_THRESHOLD) {
+      throttleValue = 0;
+    }
+    if (throttleValue>255-THROTTLE_THRESHOLD) {
+      throttleValue = 255;
+    }
     // dump status
     Serial.print("P0=");
     Serial.println(throttleValue,HEX);
+    lastThrottleValue = throttleValue;
   }
-  lastThrottleValue = throttleValue;
 }
 
 uint8_t updateButton(int pin, int id, uint8_t lastState) {
-  uint8_t state = digitalRead(pin);
+  // reverse logic because switches are to GND with internal pullup, so pushed state (ON) is 0V
+  uint8_t state = !digitalRead(pin);
   if (state != lastState) {
     Serial.print('D');
     Serial.print(id);

        digitalWrite(ledpin,HIGH);
      }
    }
  }
}



#include <Arduino.h>

// P0:
// potencjometr od nawilżacza
// nieprzylutowane -> GND, środek (czerwony pasek) -> A0, przylutowane -> 5V
// kalibracja: po włączeniu przekręcić w obie skrajne pozycje
// D0:
// zielona strona, pomiędzy GND a D4

// circuit
const int throttlePin = A0;  // P0: Analog input pin that the potentiometer is attached to
const int stagePin = 4;      // D0: Digital input pin that STAGE button is attached to

const int RCSPin = 5;        // D1: RCS switch
const int ledRCSonPin = 13; //6;   // G1: RCS status ON (green)
const int ledRCSoffPin = 7;  // R1: RCS status OFF (red)

const int SCSPin = 8;        // D2: SCS switch
