/*
@Name: Esplora Joystick Mouse
@Author: TONYLABS
@Website: http://www.tonylabs.com
@Shop: http://tonylabs.taobao.com
该例程可以实现如何通过 Arduono 编程读取摇杆 X、Y 轴的值,进而控制鼠标的坐标位置,Arduino Esplora 蜕变为一个鼠标。
重要提示: 本例程会导致您的鼠标无法控制电脑,如果您需要恢复,请按照一下步骤操作
1) 拔掉 Esplora 的
2) 打开 EsploraBlink 例程
3) 按住 Esplora 上的复位按键,同时重新插入 USB 线
4) 点击 Arduino 开发工具的上传按钮
5) 当程序下载完毕后,松开复位按键即可
*/
#include //@导入 Esplora 库头文件
void setup()
{
Serial.begin(9600); //@初始化串口通信
Mouse.begin(); //@获得鼠标控制
}
void loop()
{
int xValue = Esplora.readJoystickX(); //@读取摇杆 X 轴的值
int yValue = Esplora.readJoystickY(); //@读取摇杆 Y 轴的值
int button = Esplora.readJoystickSwitch(); //@读取摇杆按键开关的电平
Serial.print("Joystick X: "); //@串口输出 “摇杆 X 轴”
Serial.print(xValue); //@串口输出摇杆 X 轴的值
Serial.print("\tY: "); //@串口输出一个 tab 空格
Serial.print(yValue); //@串口输出摇杆 Y 轴的值
Serial.print("\tButton: "); //@串口输出一个 tab 空格和 "Button"
Serial.print(button); //@串口输出摇杆按键开关的电平值
int mouseX = map( xValue,-512, 512, 10, -10); //@把摇杆 X 轴运动坐标镜像给鼠标
int mouseY = map( yValue,-512, 512, -10, 10); //@把摇杆 Y 轴运动坐标镜像给鼠标
Mouse.move(mouseX, mouseY, 0); //@移动鼠标
delay(10); //@延时10毫秒
}
/*
@Name: Esplora Joystick Mouse
@Translation: TONYLABS
@Website: http://www.tonylabs.com
@Shop: http://tonylabs.taobao.com
*/ void loop() { int xValue = Esplora.readJoystickX(); //@读取摇杆 X 轴的值 int yValue = Esplora.readJoystickY(); //@读取摇杆 Y 轴的值 int button = Esplora.readJoystickSwitch(); //@读取摇杆按键开关的电平 Serial.print("Joystick X: "); //@串口输出 “摇杆 X 轴” Serial.print(xValue); //@串口输出摇杆 X 轴的值 Serial.print("\tY: "); //@串口输出一个 tab 空格 Serial.print(yValue); //@串口输出摇杆 Y 轴的值 Serial.print("\tButton: "); //@串口输出一个 tab 空格和 "Button" Serial.print(button); //@串口输出摇杆按键开关的电平值 int mouseX = map( xValue,-512, 512, 10, -10); //@把摇杆 X 轴运动坐标镜像给鼠标 int mouseY = map( yValue,-512, 512, -10, 10); //@把摇杆 Y 轴运动坐标镜像给鼠标 Mouse.move(mouseX, mouseY, 0); //@移动鼠标 delay(10); //@延时10毫秒 } /* @Name: Esplora Joystick Mouse @Translation: TONYLABS @Website: http://www.tonylabs.com @Shop: http://tonylabs.taobao.com */