// Dual-function / Full-range Throttle Lever with Mode Switch
const int leverPin = A0; // Slider
const int modeSwitchPin = 6; // Mode switch (dual / full-range)
const int buttonPins[4] = {2, 3, 4, 5}; // Buttons
// Configurable parameters
int CENTER = 512; // Center position of lever
int DEADZONE = 20; // Neutral range ± deadzone
int MIN_VAL = 0;
int MAX_VAL = 1023;
void setup() {
Serial.begin(9600);
// Initialize mode switch
pinMode(modeSwitchPin, INPUT_PULLUP);
// Initialize buttons
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
}
void loop() {
int leverVal = analogRead(leverPin);
bool fullRangeMode = !digitalRead(modeSwitchPin); // switch pressed = full range
int throttlePct = 0;
int brakePct = 0;
if (fullRangeMode) {
// Full-range throttle mode: 0-100% throttle, no brake
throttlePct = map(leverVal, MIN_VAL, MAX_VAL, 0, 100);
} else {
// Dual-function mode
if (leverVal > CENTER + DEADZONE) {
// Forward -> throttle
throttlePct = map(leverVal, CENTER + DEADZONE, MAX_VAL, 0, 100);
} else if (leverVal < CENTER - DEADZONE) {
// Backward -> brake
brakePct = map(leverVal, CENTER - DEADZONE, MIN_VAL, 0, 100);
}
// Center ± deadzone = neutral
}
// Print lever values
Serial.print("Mode: ");
Serial.print(fullRangeMode ? "Full Throttle" : "Dual");
Serial.print(" | Throttle: ");
Serial.print(throttlePct);
Serial.print("% | Brake: ");
Serial.print(brakePct);
Serial.print("% | Buttons: ");
// Print button states
for (int i = 0; i < 4; i++) {
int pressed = !digitalRead(buttonPins[i]); // active low
Serial.print(pressed);
Serial.print(" ");
}
Serial.println();
delay(100);
}