From e04f19873cad414aa9aa2a3fa5d3f658d72bda10 Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 25 Oct 2020 18:19:33 +0100 Subject: [PATCH] Make the button code more concise --- Flight_Sim_Controller.ino | 143 +++++++++++--------------------------- 1 file changed, 42 insertions(+), 101 deletions(-) diff --git a/Flight_Sim_Controller.ino b/Flight_Sim_Controller.ino index c309a6d..3364a60 100644 --- a/Flight_Sim_Controller.ino +++ b/Flight_Sim_Controller.ino @@ -1,119 +1,60 @@ // Requires Arduino Joystick Library https://github.com/MHeironimus/ArduinoJoystickLibrary #include Joystick_ Joystick; - -int JoystickX; -int JoystickY; -int JoystickZ; -int Throttle; -int currentButtonState0; -int lastButtonState0; -int currentButtonState1; -int lastButtonState1; -int currentButtonState2; -int lastButtonState2; -int currentButtonState3; -int lastButtonState3; -int currentButtonState4; -int lastButtonState4; -int currentButtonState5; -int lastButtonState5; -int currentButtonState6; -int lastButtonState6; + +const uint8_t LINEAR_PINS[] = { + A9, // JoystickX, Hall effect + A8, // JoystickY, Hall effect + A6, // JoystickZ, Hall effect + A4, // Throttle, Poti +}; +const uint8_t SWITCH_PINS[] = { + 7, + A0, + 6, + A1, + A2, + A3, + A5, +}; + +int lastButtonState[sizeof(SWITCH_PINS)]; + void setup() { - pinMode(6, INPUT_PULLUP); - pinMode(7, INPUT_PULLUP); - pinMode(A0, INPUT_PULLUP); - pinMode(A1, INPUT_PULLUP); - pinMode(A2, INPUT_PULLUP); - pinMode(A3, INPUT_PULLUP); - pinMode(A4, INPUT_PULLUP); - pinMode(A5, INPUT_PULLUP); - pinMode(A6, INPUT_PULLUP); - pinMode(A7, INPUT_PULLUP); - pinMode(A8, INPUT_PULLUP); - pinMode(A9, INPUT_PULLUP); - + // Setup analog inputs + for (size_t i = 0; i < sizeof(LINEAR_PINS); i++) { + pinMode(LINEAR_PINS[i], INPUT_PULLUP); + } + // Setup digital switches + for (size_t i = 0; i < sizeof(SWITCH_PINS); i++) { + pinMode(SWITCH_PINS[i], INPUT_PULLUP); + } -// Initialize Joystick Library + // Initialize Joystick Library Joystick.begin(); - Joystick.setXAxisRange(0, 1024); + Joystick.setXAxisRange(0, 1024); Joystick.setYAxisRange(0, 1024); Joystick.setZAxisRange(0, 1024); Joystick.setThrottleRange(0, 1024); } void loop() { - -// Read Joystick - JoystickX = analogRead(A9); // Hall effect sensor connects to this analog pin - JoystickY = analogRead(A8); // Hall effect sensor connects to this analog pin - -// Read Rudder Pedals - JoystickZ = analogRead(A6); // Hall effect sensor connects to this analog pin - -// Read Throttle - Throttle = analogRead(A4); // Potentiometer signal connects to this analog pin - - -// Read Switches -int currentButtonState0 = !digitalRead(7); // Button 0 - if (currentButtonState0 != lastButtonState0) - { - Joystick.setButton(0, currentButtonState0); - lastButtonState0 = currentButtonState0; - } - -int currentButtonState1 = !digitalRead(A0); // Button 1 - if (currentButtonState1 != lastButtonState1) - { - Joystick.setButton(1, currentButtonState1); - lastButtonState1 = currentButtonState1; - } - -int currentButtonState2 = !digitalRead(6); // Button 2 - if (currentButtonState2 != lastButtonState2) - { - Joystick.setButton(2, currentButtonState2); - lastButtonState2 = currentButtonState2; + // Read Switches + for (size_t i = 0; i < sizeof(SWITCH_PINS); i++) { + int currentButtonState = !digitalRead(SWITCH_PINS[i]); + if (currentButtonState != lastButtonState[i]) { + Joystick.setButton(i, currentButtonState); + lastButtonState[i] = currentButtonState; + } } -int currentButtonState3 = !digitalRead(A1); // Button 3 - if (currentButtonState3 != lastButtonState3) - { - Joystick.setButton(3, currentButtonState3); - lastButtonState3 = currentButtonState3; - } - -int currentButtonState4 = !digitalRead(A2); // Button 4 - if (currentButtonState4 != lastButtonState4) - { - Joystick.setButton(4, currentButtonState4); - lastButtonState4 = currentButtonState4; - } - -int currentButtonState5 = !digitalRead(A3); // Button 5 - if (currentButtonState5 != lastButtonState5) - { - Joystick.setButton(5, currentButtonState5); - lastButtonState5 = currentButtonState5; - } - -int currentButtonState6 = !digitalRead(A5); // Button 6 - if (currentButtonState6 != lastButtonState6) - { - Joystick.setButton(6, currentButtonState6); - lastButtonState6 = currentButtonState6; - } - -// Output Controls - Joystick.setXAxis(JoystickX); - Joystick.setYAxis(JoystickY); - Joystick.setZAxis(JoystickZ); - Joystick.setThrottle(Throttle); + // Output Controls + Joystick.setXAxis(analogRead(LINEAR_PINS[0])); + Joystick.setYAxis(analogRead(LINEAR_PINS[1])); + Joystick.setZAxis(analogRead(LINEAR_PINS[2])); + Joystick.setThrottle(analogRead(LINEAR_PINS[3])); Joystick.sendState(); - -} +}