// Flight Simulator Device: platform angles to a USB joystick. // Board: Arduino Pro Micro / Leonardo (ATmega32U4). // Library: "Joystick" by Matthew Heironimus (ArduinoJoystickLibrary), install from GitHub zip. // // Wiring // AS5600 roll (front base end, magnet on the roll axle) OUT -> A0, VCC -> VCC (5V), GND -> GND, DIR -> GND // AS5600 pitch (gimbal, magnet on the pitch axle) OUT -> A1, VCC -> VCC (5V), GND -> GND, DIR -> GND // Grip, each switch between the pin and GND (internal pullups): // trigger 2, button 2 -> 3, button 3 -> 4, button 4 -> 5 // hat up 6, right 7, down 8, left 9 // // The AS5600 OUT pin is an analog voltage proportional to the magnet angle in its // default configuration, so no I2C multiplexer is needed for two sensors. // // Recenter: level the cockpit at its neutral balance point, then hold trigger + button 4 // for 3 seconds. The center is saved to EEPROM. #include #include const float ROLL_RANGE_DEG = 28.0; // stop to stop / 2, matches the padded side stops const float PITCH_UP_DEG = 10.5; // center to the nose up stop const float PITCH_DOWN_DEG = 9.5; // center to the nose down stop const float DEADBAND_DEG = 0.6; const bool INVERT_ROLL = false; // flip if right roll reads as left in the sim const bool INVERT_PITCH = false; const uint8_t BUTTON_PINS[4] = {2, 3, 4, 5}; const uint8_t HAT_PINS[4] = {6, 7, 8, 9}; // up, right, down, left Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 4, 1, // buttons, hat switches true, true, false, // X, Y, Z false, false, false, // Rx, Ry, Rz false, false, false, false, false); struct Center { uint16_t magic; int16_t roll; int16_t pitch; }; Center center = {0xC0DE, 512, 512}; float rollF = 0, pitchF = 0; unsigned long comboSince = 0; // ADC counts (0-1023 over 360 degrees) to a signed angle around the stored center. float countsToDeg(int raw, int mid) { int d = raw - mid; if (d > 512) d -= 1024; if (d < -512) d += 1024; return d * (360.0 / 1024.0); } int readAvg(uint8_t pin) { long s = 0; for (uint8_t i = 0; i < 8; i++) s += analogRead(pin); return s / 8; } int16_t toAxis(float deg, float negRange, float posRange) { if (fabs(deg) < DEADBAND_DEG) return 0; float n = deg >= 0 ? deg / posRange : deg / negRange; n = constrain(n, -1.0, 1.0); return (int16_t)(n * 32767); } void setup() { for (uint8_t p : BUTTON_PINS) pinMode(p, INPUT_PULLUP); for (uint8_t p : HAT_PINS) pinMode(p, INPUT_PULLUP); Center saved; EEPROM.get(0, saved); if (saved.magic == 0xC0DE) center = saved; Joystick.setXAxisRange(-32767, 32767); Joystick.setYAxisRange(-32767, 32767); Joystick.begin(false); } void loop() { int rawRoll = readAvg(A0), rawPitch = readAvg(A1); float roll = countsToDeg(rawRoll, center.roll) * (INVERT_ROLL ? -1 : 1); float pitch = countsToDeg(rawPitch, center.pitch) * (INVERT_PITCH ? -1 : 1); rollF += (roll - rollF) * 0.35; // light smoothing, the platform itself is slow pitchF += (pitch - pitchF) * 0.35; // Joystick Y is negative when pushed forward, and nose down is pushed forward. Joystick.setXAxis(toAxis(rollF, ROLL_RANGE_DEG, ROLL_RANGE_DEG)); Joystick.setYAxis(-toAxis(pitchF, PITCH_DOWN_DEG, PITCH_UP_DEG)); for (uint8_t i = 0; i < 4; i++) Joystick.setButton(i, digitalRead(BUTTON_PINS[i]) == LOW); bool up = !digitalRead(HAT_PINS[0]), right = !digitalRead(HAT_PINS[1]); bool down = !digitalRead(HAT_PINS[2]), left = !digitalRead(HAT_PINS[3]); int hat = -1; if (up && right) hat = 45; else if (right && down) hat = 135; else if (down && left) hat = 225; else if (left && up) hat = 315; else if (up) hat = 0; else if (right) hat = 90; else if (down) hat = 180; else if (left) hat = 270; Joystick.setHatSwitch(0, hat); Joystick.sendState(); bool combo = digitalRead(BUTTON_PINS[0]) == LOW && digitalRead(BUTTON_PINS[3]) == LOW; if (!combo) comboSince = 0; else if (comboSince == 0) comboSince = millis(); else if (millis() - comboSince > 3000) { center.roll = rawRoll; center.pitch = rawPitch; EEPROM.put(0, center); rollF = pitchF = 0; comboSince = 0; } delay(4); // about 250 Hz }