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 100 Hz to 5000 Hz
tone(buzzerPin, freq); // Play the current frequency
delayMicroseconds(20); // Smooth frequency transition (short delay)
}
noTone(buzzerPin); // Turn off the buzzer after the sweep
}
// --- Button 2: Mouse Wheel Down (Dezoom) ---
if (digitalRead(buttonWheelDown) == LOW) { Mouse.move(0, 0, -1); // Simulate Mouse Wheel Up
delay(50); // Debounce delay
for (int freq = 2000; freq >= 200; freq--) { // Frequency increases from 100 Hz to 5000 Hz
tone(buzzerPin, freq); // Play the current frequency
delayMicroseconds(20); // Smooth frequency transition (short delay)
}
noTone(buzzerPin); // Turn off the buzzer after the sweep
}
// --- Button 3: Left Mouse Click ---
int buttonState = digitalRead(buttonMouseClick);
// Perform Mouse Click
if (buttonState == LOW) { Mouse.click(MOUSE_LEFT); // Perform Left Mouse Click
digitalWrite(ledPin, HIGH); // Turn on the LED when button is pressed
} else {
digitalWrite(ledPin, LOW); // Turn off the LED otherwise
}
}
![](https://cdn.hackaday.io/images/8315831734731051760.jpg)
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.