XIAO RA4M1 鼠标
所需材料
Seeed Studio XIAO RA4M1 | Grove 拇指摇杆 | Grove 连接线 |
---|---|---|
![]() | ![]() | ![]() |
初步准备
点亮 XIAO RA4M1 的内置 LED
如果您之前从未使用过 XIAO RA4M1,请点击这里学习如何点亮内置 LED,这可以确保您的开发环境正常工作。
读取 Grove 摇杆的 X 和 Y 轴数据
步骤 1. 连接设备
首先,您需要按照下表通过引脚将 XIAO RA4M1 和摇杆连接在一起。
XIAO RA4M1 | Grove - 拇指摇杆 |
---|---|
3.3V | 红色 |
GND | 黑色 |
A0 | 白色 |
A1 | 黄色 |
现在使用 Grove 线缆按照下图连接它们。

步骤 2. 上传测试程序
复制以下代码,粘贴到 Arduino IDE 中,然后上传到 XIAO RA4M1
void setup()
{
Serial.begin(9600);
}
void loop()
{
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
Serial.print("The X and Y coordinate is:");
Serial.print(sensorValue1, DEC);
Serial.print(",");
Serial.println(sensorValue2, DEC);
Serial.println(" ");
delay(200);
}

现在你移动摇杆,轴数据会同时改变。这意味着 XIAO RA4M1 和摇杆工作完美。
使用 XIAO RA4M1 制作鼠标
步骤 3. 上传鼠标控制器程序
#include <Mouse.h>
const int xaxis = A0;
const int yaxis = A1;
const int sensitivity = 5;//adjust the mouse sensetive
void setup()
{
Mouse.begin();
delay(1000);//at least 1000ms to initilize
Serial.begin(9600);
}
void loop()
{
int xValue = analogRead(xaxis);
int yValue = analogRead(yaxis);
/*
when you click the central button of Grove joystick, xValue will equal to 1023,
so when xValue < 1000, we move the mouse; when xValue>1000, we click the mouse
*/
if(xValue<1000){
//define mouse move distance
int xDistance;
int yDistance;
/*
The joystick reads the x and y values with 500 as the center of the coordinate axis,
so we need to process the data by subtracting 500, while also eliminating some minor vibrations.
*/
//Eliminate X minor vibrations.
if(abs(xValue-500)<10){
xDistance = 0;
}else{
xDistance = (xValue - 500);
}
//Eliminate X minor vibrations.
if(abs(yValue-500)<10){
yDistance = 0;
}else{
yDistance = (yValue - 500);
}
//read value from joystick
Serial.print("value: ");
Serial.print(xValue);
Serial.print(",");
Serial.println(yValue);
Serial.println(" ");
//read distance the mouse will move
Serial.print("distance: ");
Serial.print(xDistance);
Serial.print(",");
Serial.println(yDistance);
Serial.println(" ");
//make the mouse not so sensetive
Mouse.move(xDistance/sensitivity,-yDistance/sensitivity,0);
}else{
//when xValue>1000, mouse click
Mouse.click();
}
delay(100);
}

现在当你控制摇杆时,你会看到光标在移动~
恭喜!你成功构建了这个简单的小鼠标。还有更多有趣的HID(人机接口设备)功能等待你通过XIAO RA4M1来探索。想了解更多?请点击这里跳转~
技术支持与产品讨论
感谢您选择我们的产品!我们在这里为您提供不同的支持,以确保您使用我们产品的体验尽可能顺畅。我们提供多种沟通渠道,以满足不同的偏好和需求。