IDE:Arduino 1.0.4
一、线路连接
S-Y ——> P5(A0)
S-X ——> P2(A1)
S-K ——> P0
VCC ——> VCC
GND ——> GND
二、Arduino代码
//from A0 start,the max is 1023 ,the max Vol is 5V //A0 ==> P5 //from 470 begin ,less this can't working //A1 ==> P2 //A2 ==> P4 but the value is smaller and can't read VCC ,maybe beacause it is used by USB conmunication //#include "DigiKeyboard.h" #include "DigiMouse.h" const int buttonPin = 0; const int ledPin = 1; int buttonState = 1; void setup() { } void loop() { int x = analogRead(A1); int y = analogRead(A0); /*DigiKeyboard.sendKeyStroke(0);// prevent missing the first character after a delay: DigiKeyboard.print("("); DigiKeyboard.print(x); DigiKeyboard.print(","); DigiKeyboard.print(y); DigiKeyboard.println(")"); DigiKeyboard.delay(1000);*/ DigiMouse.moveY(y-537); //down 10 DigiMouse.delay(100); DigiMouse.moveX(x-521); //right 20 DigiMouse.delay(100); //DigiMouse.scroll(5);//pg up //DigiMouse.delay(500); buttonState = digitalRead(buttonPin); if (buttonState == LOW) { //the JoyStick button down is low level digitalWrite(ledPin, HIGH); // three buttons are the three LSBs of an unsigned char DigiMouse.setButtons(1<<0); //1<<0 left click;1<<1 right click DigiMouse.delay(500); DigiMouse.setButtons(0); //unclick all //DigiMouse.delay(500); } else { digitalWrite(ledPin, LOW); } }
说明:
1、DigiMouse.moveX(char deltaX)、DigiMouse.moveY(char deltaY)函数是相对当前位置进行移动;
2、DigiMouse.scroll(5);为向上翻页;
3、DigiMouse.setButtons(1<<0) 为鼠标左键单击,DigiMouse.setButtons(1<<1) 为鼠标右键单击,DigiMouse.setButtons(0)为清除单击事件;
4、鼠标单击函数执行后,设置合适的延时,可以实现双击。
ps:该Digispark kickstarter似乎存在一点问题,P5脚,即A0脚模拟量无法读取全部模拟量,只能从约470的AD值开始读(最大AD值为1023)。