通过一个Joystick手柄组件和一个 Arduino Leonardo 或者 Pro Micro 来制作一个可以控制电脑鼠标,并点击的控制器。Joystick实际上是一个变阻器,通过改变x、y轴的阻止来发送信号,经由IDE编译并刷新相对的x、y信号来控制鼠标指针移动。

本文为面包社区原创内容,未经作者及本站允许请勿转载至任何其他平台!

joystick-pc-mouse
延伸应用:街机项目、游戏操作项目、控制机器人移动等

所需部件

  • Joystick Module 1个(没有的话也可以使用 Blynk 中的虚拟Joystick来实现)
  • Arduino Leonardo 或 Pro Micro 1个
  • Arcade button 1个(充当鼠标左键,可以不使用)
  • 一些 Jumper wires

电路连接:(仅限Leonardo/Micro/PRO Micro等支持MOUSE函数的设备)

Schematic.png
  • VCC → 5V
  • GND → GND
  • Y/VRy → A0
  • X/VRx → A1
  • KEY/SW → D2

Arduino代码

手柄输入的数据范围是0到1023,我们将通过把这个范围的值编译为-10到10来控制鼠标,如果你想要改变鼠标的移动速度,可以自行修改代码来实现。如果使用的是 Pro Micro,上传代码的时候也可以使用“Arduino Leonardo”板,或者自行下载 Pro Micro 板,并选择对应端口。

使用时需要先按一下Joystick自带的按钮来开始操控。

// Define Pins

const int startEmulation = 2;      // switch to turn on and off mouse emulation
const int mouseLeftButton = 4;    // input pin for the mouse left Button
const int joystickX = A1;         // joystick X axis  
const int joystickY = A0;         // joystick Y axis

// parameters for reading the joystick:
int cursorSpeed = 10;               // output speed of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = cursorSpeed/4;      // resting threshold
int center = cursorSpeed/2;         // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state

void setup() {
  pinMode(startEmulation, INPUT_PULLUP);   // the switch pin
  pinMode(mouseLeftButton, INPUT_PULLUP);  // the left mouse button pin

  Mouse.begin();  // take control of the mouse
}

void loop() {
  // read the switch:
  int switchState = digitalRead(startEmulation);

  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == LOW) {
      mouseIsActive = !mouseIsActive;
    }
  }

  // save switch state for next loop:
  lastSwitchState = switchState;

  // read and scale the two axes:
  int xReading = readAxis(A1);
  int yReading = readAxis(A0);

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, yReading, 0); // (x, y, scroll mouse wheel)
  }  

  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseLeftButton) == HIGH) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
      delay(100); // delay to enable single and double-click
    }
  }

  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }

  delay(responseDelay);
}

/*
  reads an axis (0 or 1 for x or y) and scales the
 analog input range to a range from 0 to <range>
 */

int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, cursorSpeed);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  // return the distance for this axis:
  return distance;
}