Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • πŸ”΄SMD Red
      • Troubleshooting Guide
    • πŸ”΅SMD Blue
    • 🟒SMD Green
    • Gateway Modules
      • Arduino Gateway Module
      • USB Gateway Module
    • Electrical Motors
      • Brushed DC Motors (BDC)
      • Stepper DC Motors (SDC)
      • Brushless DC Motor (BLDC)
      • Linear Actuator with Feedback – 75 lbs
    • Add-on Modules
      • Ambient Light Sensor Module
      • Button Module
      • Buzzer Module
      • IMU Module
      • Joystick Module
      • Potentiometer Module
      • Reflectance Sensor Module
      • RGB LED Module
      • Servo Module
      • Ultrasonic Distance Sensor Module
  • SMD Kits
    • Starter Kit
      • What You Can Build
    • Education Kit
      • What You Can Build
    • Motion Kit
      • What You Can Build
  • Software
    • Libraries
      • Python Library
      • Arduino Library
      • Java Library
      • Matlab Library
    • SMD UI
    • SMD Blockly
      • Introducing Customized Blockly Blocks
  • SMD Applications
    • Basics
      • Blink
      • Action - Reaction
      • Autonomous Lighting
      • Smart Doorbell
      • Security System
      • Distance Buzzer Warning
      • Distance Auto Stop
      • Smart Light Control
    • Interactive
      • Automatic Trash Bin
      • Radar
      • Chrome Dino Game Player
      • Play Chrome Dino Game With Joystick
      • Snake Game With Joystick
      • Pan-Tilt with Joystick Module
      • Joystick Mouse Control
      • Rev Up the Engine
      • Motor Rotation Based on Turn Input Value
      • Basic Motor Speed Control Application
      • Basic Motor Control Application Using PWM Input
      • Basic Motor Position Control Application
      • Basic Motor Torque Control Application
      • Motor Rotation Based on Joystick Counting
    • Robotics
      • Differential Robot Projects
      • Mouse Cursor Tracker Motion Robot
      • Waypoint tracker robot
      • Braitenberg Robot
      • Line-Follower Robot
      • Teleoperation Robot
      • Obstacle Avoidance Robot
      • ESP32 Wireless Controlled Mobile Robot
  • AI
    • Object Tracking Robot
    • Groq Chatbot-Controlled Robot
  • ROS
    • Teleoperation Robot with ROS
  • Mechanics
    • Building Set
      • Plates
        • 2x2 Plate Package
        • 2x3 120Β° Plate Package
        • 3x3 Plate Package
        • 3x5 Plate Package
        • 3x9 Plate Package
        • 11x19 Plate
        • 9x19 Plate
        • 5x19 Plate
        • 3x19 Plate
        • 9x11 Plate
        • 5x13 Plate
      • Joints
        • 60Β° Joint Package
        • 90Β° Joint Package
        • 120Β° Joint Package
        • Slot Joint M2 Package
        • Slot Joint M3 Package
        • U Joint Package
      • Mounts
        • Add-on Mount Package
        • Motor L Mount Package
        • Pan-Tilt Package
      • Wheels
        • Ball Wheel Package
        • Caster Wheel Package
        • Wheel Package
      • Cables
        • Power Cable 10 cm Package
        • Power Cable 20 cm Package
        • Power Cable 35 cm Package
        • RJ-11 Cable 7.5 cm Package
        • RJ-11 Cable 20 cm Package
        • RJ-11 Cable 35 cm Package
        • RJ-45 Cable 7.5 cm Package
        • RJ-45 Cable 20 cm Package
        • RJ-45 Cable 35 cm Package
      • Fasteners
        • M2x5 Allen Hex Screw Package
        • M3x6 Allen Hex Screw Package
        • M3x8 Allen Hex Screw Package
        • M3 Hex Nut Package
  • Help
    • Manual
    • Shops
    • Reach Us
Powered by GitBook
On this page
  • Step 1: Hardware & Software Overview
  • Step 2: Assemble
  • Step 3: Run & Test
  • Codes
  1. SMD Applications
  2. Interactive

Joystick Mouse Control

PreviousPan-Tilt with Joystick ModuleNextRev Up the Engine

Last updated 2 months ago

This project is an interactive system that controls the computer's mouse cursor using a connected to the . The user can adjust the mouse sensitivity dynamically through a graphical interface and perform mouse clicks using the . The system continuously monitors inputs and responds accordingly, providing a seamless user experience for controlling the mouse cursor.

About Tools and Materials:

()

()

()

Step 1: Hardware & Software Overview

Key Components:

  1. The acts as the communication hub between the and the computer. It collects data, processes user input, and translates it into mouse movements and clicks.

  2. The serves as the main input device. The user can move the to control the mouse pointer’s X and Y coordinates on the screen. Additionally, the button can be used to trigger mouse clicks.

  3. GUI (Tkinter) A graphical user interface (GUI) is used to display real-time values (X and Y axes), sensitivity levels, and the button status. The GUI also allows the user to manually adjust the sensitivity for finer control.

Project Features:

  1. Real-Time Joystick Mouse Control The system continuously reads the X and Y values from the and moves the mouse pointer based on those inputs. The ’s button is mapped to mouse clicks, allowing users to interact with on-screen elements.

  2. Dynamic Sensitivity Adjustment The system features dynamic sensitivity control, which adjusts the mouse movement speed based on the ’s input magnitude. Users can also manually set the base sensitivity via the GUI for greater precision.

  3. Graphical Interface for Feedback The GUI provides real-time feedback to the user, displaying the current X and Y values of the , the sensitivity, and whether the button is pressed or released. This ensures transparency in how the movements are translated into mouse actions.

  4. Manual Sensitivity Input The user can adjust the base sensitivity through an input field in the GUI. Once entered, the new sensitivity is applied, and the system updates the sensitivity label accordingly.

  5. Multithreading for Real-Time Control To ensure the mouse control happens smoothly without interrupting the GUI, the system uses a separate thread for handling the joystick inputs and mouse control. This ensures that both the GUI and joystick functionalities run in parallel without delays.

Step 2: Assemble

Workflow:

Getting Started

Project Wiring Diagram

Step 3: Run & Test

  1. Run the Application:

    • Execute the provided Python script to start the Joystick Mouse Control application.

  2. Adjust Sensitivity: Use the GUI to adjust the sensitivity settings for finer control as needed.

Codes

from smd.red import *
import time
import pyautogui
import tkinter as tk
from threading import Thread
from serial.tools.list_ports import comports
from platform import system

pyautogui.FAILSAFE = False

baudrate = 115200          # Baud rate of communication
ID = 0                     # ID of the SMD
joystick_id = 5            # ID of the joystick module
button_id = 5              # ID of the button module


def USB_Port():
    """
    Scans and identifies a compatible USB port for the current operating system.

    Returns:
        str: The detected USB port or None if no suitable port is found.
    """
    # Get a list of available ports
    ports = list(comports())

    # Known USB port names for different operating systems
    usb_names = {
        "Windows": ["USB Serial Port"],
        "Linux": ["/dev/ttyUSB"],
        "Darwin": [
            "/dev/tty.usbserial",
            "/dev/tty.usbmodem",
            "/dev/tty.SLAB_USBtoUART",
            "/dev/tty.wchusbserial",
            "/dev/cu.usbserial",
            "/dev/cu.usbmodem",
            "/dev/cu.SLAB_USBtoUART",
            "/dev/cu.wchusbserial",
        ]
    }

    # Detect the operating system
    os_name = system()
    print(f"Operating System: {os_name}")

    if ports:
        for port in ports:
            # Check if the port matches any known USB names
            if any(name in port.device or name in port.description for name in usb_names.get(os_name, [])):
                print(f"USB device detected on port: {port.device}")
                return port.device  # Return the first matching port
        # If no suitable port is found, print the list of available ports
        print("No suitable USB device found. Available ports:")
        for port in ports:
            print(f"Port: {port.device}, Description: {port.description}, HWID: {port.hwid}")
    else:
        print("No ports detected!")
    return None


# Find and initialize the USB port
SerialPort = USB_Port()
if not SerialPort:
    raise Exception("No compatible USB port found. Please check your connection.")

# Initialize the SMD module
master = Master(SerialPort, baudrate)
master.attach(Red(ID))
print("Connected Modules:", master.scan_modules(ID))


# Sensitivity settings
base_sensitivity = 0.5  # Default sensitivity
dynamic_multiplier = 1.0  # Multiplier for dynamic sensitivity


# GUI setup
window = tk.Tk()
window.title("Joystick Mouse Control")
window.geometry("300x300")

x_label = tk.Label(window, text="X Value: 0", font=("Helvetica", 14))
x_label.pack(pady=10)

y_label = tk.Label(window, text="Y Value: 0", font=("Helvetica", 14))
y_label.pack(pady=10)

sensitivity_label = tk.Label(window, text="Sensitivity: 0.5", font=("Helvetica", 14))
sensitivity_label.pack(pady=10)

button_status_label = tk.Label(window, text="Button Status: Released", font=("Helvetica", 14))
button_status_label.pack(pady=10)

sensitivity_entry = tk.Entry(window)
sensitivity_entry.pack(pady=10)
sensitivity_entry.insert(0, str(base_sensitivity))


def update_gui(x_val, y_val, sensitivity, button_status):
    """
    Updates GUI elements with joystick values, sensitivity, and button status.
    """
    x_label.config(text=f"X Value: {x_val:.2f}")
    y_label.config(text=f"Y Value: {y_val:.2f}")
    sensitivity_label.config(text=f"Sensitivity: {sensitivity:.2f}")
    button_status_label.config(text=f"Button Status: {button_status}")


def update_sensitivity():
    """
    Updates the base sensitivity for joystick control based on user input.
    """
    global base_sensitivity
    try:
        base_sensitivity = float(sensitivity_entry.get())
        sensitivity_label.config(text=f"Sensitivity: {base_sensitivity:.2f}")
    except ValueError:
        sensitivity_label.config(text="Invalid sensitivity value")


apply_button = tk.Button(window, text="Apply Sensitivity", command=update_sensitivity)
apply_button.pack(pady=10)


def joystick_control():
    """
    Handles joystick input for mouse control.
    """
    global base_sensitivity, dynamic_multiplier

    while True:
        joystick = master.get_joystick(ID, joystick_id)
        if joystick is None:
            print("Joystick not connected!")
            continue

        x_val, y_val = joystick[0], joystick[1]
        button_status = "Pressed" if master.get_button(ID, button_id) == 1 else "Released"

        # Update dynamic multiplier
        dynamic_multiplier = max(1.0, min(5.0, (abs(x_val) + abs(y_val)) / 50))

        # Adjust sensitivity
        x_val *= base_sensitivity * dynamic_multiplier
        y_val *= base_sensitivity * dynamic_multiplier
        y_val *= -1  # Invert Y-axis for proper mouse movement

        # Update mouse position
        if abs(x_val) > 5 or abs(y_val) > 5:
            pyautogui.moveRel(x_val, y_val)

        # Handle button click
        if joystick[2] == 1:
            pyautogui.click()

        # Update GUI
        update_gui(joystick[0], joystick[1], base_sensitivity, button_status)

        time.sleep(0.01)


# Start joystick control in a separate thread
joystick_thread = Thread(target=joystick_control)
joystick_thread.daemon = True
joystick_thread.start()

# Run the GUI loop
window.mainloop()

Data Monitoring The system continuously reads the 's X, Y, and button values. These inputs control the mouse pointer’s movements and clicks.

Mouse Movement Control Based on the ’s X and Y axis values, the system calculates the new mouse pointer position and moves the cursor accordingly. The movement speed is influenced by both the base sensitivity and dynamic multiplier, which adjusts based on the 's movement magnitude.

Button Click Simulation If the ’s button is pressed, the system simulates a mouse click. The button press also temporarily increases the base sensitivity for quicker movements.

GUI Updates The GUI continuously updates with the latest values, sensitivity, and button status, providing visual feedback to the user.

Connect the SMD: Connect the to your PC using a .

Set Up the Joystick Module: Connect the to the SMD using an RJ-45 cable.

Power Connections: Ensure that the is powered and that all connections are securely made.

Once the application is running, move the to control the mouse cursor on your screen. Experiment with different movements to understand how they translate to mouse actions.

Conclusion: This project demonstrates how the can be used to create an interactive joystick-controlled system for managing mouse movements. By integrating real-time input with dynamic sensitivity control and a graphical interface for user feedback, the system provides an intuitive and customizable solution for precise cursor control.

Joystick Module
ACROME SMD
Joystick Module
Joystick Module
SMD Red
Purchase Here
SMD USB Gateway
Purchase Here
Joystick Module
Purchase Here
ACROME SMD
ACROME SMD
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
ACROME SMD
USB Gateway Module
Joystick Module
SMD
Joystick Module
Joystick Module
ACROME SMD
Joystick Module