Close

Final code

A project log for FPS alt controller

Alternative controller with a gun and joystick for FPS games

shrey-modyShrey Mody 12/17/2024 at 08:340 Comments

We have made a few modifications to the code, here is the new version:

#include <Mouse.h>
#include "Keyboard.h"

// Joystick Pins
int horzPin = A0;    // Analog pin for horizontal joystick
int vertPin = A1;    // Analog pin for vertical joystick
int selPin = 8;      // Joystick button (select pin)

// Joystick Center Calibration
int vertZero, horzZero;      // Joystick center positions
int vertValue, horzValue;    // Joystick axis readings
const float sensitivity = 0.5; // Adjust mouse sensitivity (lower = slower movement)
int deadZone = 10;           // Minimum offset to ignore small joystick movements

// Button Pin Assignments
const int buttonWheelUp = 2;     // Button for Mouse Wheel Up (Zoom)
const int buttonMouseClick = 3;  // Button for Left Mouse Click

void setup() {
  Serial.begin(9600); // Start Serial Monitor for debugging
    // Joystick Setup
  pinMode(horzPin, INPUT);
  pinMode(vertPin, INPUT);
  pinMode(selPin, INPUT_PULLUP);

  // Button Setup
  pinMode(buttonWheelUp, INPUT_PULLUP);
  pinMode(buttonWheelDown, INPUT_PULLUP);
  pinMode(buttonMouseClick, INPUT_PULLUP);

  // Calibrate Joystick Neutral Position
  vertZero = analogRead(vertPin);
  horzZero = analogRead(horzPin);
    Serial.println("Joystick Calibration:");
  Serial.print("Center Vertical Value: ");
  Serial.println(vertZero);
  Serial.print("Center Horizontal Value: ");
  Serial.println(horzZero);

  Mouse.begin();    // Initialize Mouse Emulation
  Keyboard.begin(); // Initialize Keyboard Emulation
}

void loop() {
  // --- Joystick Movement ---
  vertValue = analogRead(vertPin) - vertZero; // Vertical deviation from center
  horzValue = analogRead(horzPin) - horzZero; // Horizontal deviation from center

  // Dead Zone Filter to ignore small offsets
  if (abs(vertValue) < deadZone) vertValue = 0;
  if (abs(horzValue) < deadZone) horzValue = 0;

  // Debug output for raw joystick values
  Serial.print("Raw Vertical: ");
  Serial.print(vertValue);
  Serial.print(" | Raw Horizontal: ");
  Serial.println(horzValue);

  // Scale joystick values for smoother and slower mouse movement
  int scaledVertValue = map(vertValue, -512, 512, 5, -5);  // Inverted Y-axis
  int scaledHorzValue = map(horzValue, -512, 512, 5, -5);  // Inverted X-axis

  // Debug output for scaled values
  Serial.print("Scaled Vertical: ");
  Serial.print(scaledVertValue);
  Serial.print(" | Scaled Horizontal: ");
  Serial.println(scaledHorzValue);

  // Apply Mouse Movement (inverted axes)
  if (scaledVertValue != 0) { // Move only if outside the dead zone
    Mouse.move(0, scaledVertValue); // Vertical movement (Y-axis)
  }
  if (scaledHorzValue != 0) {
    Mouse.move(scaledHorzValue, 0); // Horizontal movement (X-axis)
  }

  // --- Joystick Button (Space Bar) ---
  if (digitalRead(selPin) == LOW) {     Keyboard.press(' ');  // Press Spacebar
    delay(50);            // Debounce delay
    Keyboard.release(' ');
  }

  // --- Button 1: Mouse Wheel Up (Zoom) ---
  if (digitalRead(buttonWheelUp) == LOW) {     Mouse.move(0, 0, 1);  // Simulate Mouse Wheel Up
    delay(50);            // Debounce delay
  }

  // --- Button 2: Left Mouse Click ---
  if (digitalRead(buttonMouseClick) == LOW) {     Mouse.click(MOUSE_LEFT); // Perform Left Mouse Click
    delay(50);               // Debounce delay
  }
}

Discussions