here is the actual final arduino code, we added some LEDs and sounds to mimic the effects of an actua sound, for example a sound when you're aiming and when you shoot a little light will turn on mimicking the muzzle flash of an actual gun.
#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.3; // Adjust mouse sensitivity (lower = slower movement)
int deadZone = 3; // Minimum offset to ignore small joystick movements
// Button Pin Assignments
const int buttonWheelUp = 2; // Button for Mouse Wheel Up (Zoom)
const int buttonWheelDown = 3;
const int buttonMouseClick = 4; // Button for Left Mouse Click
const int buzzerPin = 6; // Pin connected to the buzzer
const int ledPin = 7; // Pin connected to the LED
void setup() {
Serial.begin(9600); // Start Serial Monitor for debugging
// Joystick Setup
pinMode(horzPin, INPUT);
pinMode(vertPin, INPUT);
pinMode(selPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin+, OUTPUT);
// 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
for (int freq = 200; freq <= 2000; freq++) { // Frequency increases from...