Arduino IDE come with a EsploraJoystickMouse example (in Arduino IDE > File > Examples > Esplora > Beginners > EsploraJoystickMouse, or here), it will report mouse position to your PC base on Esplora's Joystick.
WARNING: this sketch will take over your mouse movement. If you lose control of your mouse do the following:
1) unplug the Esplora.
2) open the EsploraBlink sketch
3) hold the reset button down while plugging your Esplora back in
4) while holding reset, click "Upload"
5) when you see the message "Done compiling", release the reset button.
It is possible that your mouse will auto-run when the joystick is in rest position. Because it assume the function Esplora.readJoystickX() and Esplora.readJoystickY() return 0 at center rest position. But it is not the actual case, most probably it return non-0 even the Joystick is in rest position.
The below example, modified from last example Read joystick and display on TFT, read the actual center postion at power up (assume it is in rest), to offset the x, y postion accordingly. And also it will compare the x, y values with the slider value to adjust the sensitivity. If the movement of x, y postion is less than slider, it will return no movement to PC.
#include <TFT.h>
#include <SPI.h>
#include <Esplora.h>
const int MAX_W = 160;
const int MAX_H = 128;
int xCenter = MAX_W/2;
int yCenter = MAX_H/2;
int xPos = xCenter;
int yPos = yCenter;
int xPrev = xCenter;
int yPrev = yCenter;
char printoutX[5];
char printoutY[5];
char printoutSlider[5];
int centerJoystickX, centerJoystickY;
int JoystickButton;
int prevJoystickButton;
void setup(){
Serial.begin(9600);
Mouse.begin();
EsploraTFT.begin();
EsploraTFT.background(0,0,0);
EsploraTFT.stroke(255,255,255);
EsploraTFT.line(xCenter, 0, xCenter, MAX_H);
EsploraTFT.line(0, yCenter, MAX_W, yCenter);
//preset dummy reading to print
String dummy = "0";
dummy.toCharArray(printoutX,5);
dummy.toCharArray(printoutY,5);
dummy.toCharArray(printoutSlider,5);
//Read Joystick center position at start-up
centerJoystickX = Esplora.readJoystickX();
centerJoystickY = Esplora.readJoystickY();
//preset JoystickSwitch states
JoystickButton = HIGH;
prevJoystickButton = HIGH;
}
void loop(){
int xValue = Esplora.readJoystickX();
int yValue = Esplora.readJoystickY();
int sliderValue = Esplora.readSlider();
//map stick value to TFT screen position
if(xValue >= centerJoystickX){
xPos = map(xValue, 512, centerJoystickX, 0, xCenter);
}else{
xPos = map(xValue, centerJoystickX, -512, xCenter, MAX_W);
}
int mouseX, mouseY;
if(xValue >= centerJoystickX){
xPos = map(xValue, 512, centerJoystickX, 0, xCenter);
//report mouse position only if position offset > slider
if((xValue - centerJoystickX) >= sliderValue){
mouseX = map( xValue, 512, centerJoystickX, -10, 0);
}else{
mouseX = 0;
}
}else{
xPos = map(xValue, centerJoystickX, -512, xCenter, MAX_W);
//report mouse position only if position offset > slider
if((centerJoystickX - xValue) >= sliderValue){
mouseX = map( xValue, centerJoystickX, 512, 0, -10);
}else{
mouseX = 0;
}
}
if(yValue >= centerJoystickY){
yPos = map(yValue, 512, centerJoystickY, MAX_H, yCenter);
//report mouse position only if position offset > slider
if((yValue - centerJoystickY) >= sliderValue){
mouseY = map( yValue, 512, centerJoystickY, 10, 0);
}else{
mouseY = 0;
}
}else{
yPos = map(yValue, centerJoystickY, -512, yCenter, 0);
//report mouse position only if position offset > slider
if((centerJoystickY - yValue) >= sliderValue){
mouseY = map( yValue, centerJoystickY, 512, 0, 10);
}else{
mouseY = 0;
}
}
Mouse.move(mouseX, mouseY, 0);
//clear previous print of reading
EsploraTFT.stroke(0,0,0);
EsploraTFT.text(printoutX,0,10);
EsploraTFT.text(printoutY,0,20);
EsploraTFT.text(printoutSlider,0,30);
String(xPos).toCharArray(printoutX,5);
String(yPos).toCharArray(printoutY,5);
String(sliderValue).toCharArray(printoutSlider,5);
EsploraTFT.stroke(255,255,255);
EsploraTFT.text(printoutX,0,10);
EsploraTFT.text(printoutY,0,20);
EsploraTFT.text(printoutSlider,0,30);
if(xPos!=xPrev || yPos!=yPrev){
EsploraTFT.line(xPrev, yPrev, xPos, yPos);
EsploraTFT.stroke(0,0,255);
EsploraTFT.point(xPos, yPos);
xPrev=xPos;
yPrev=yPos;
}
delay(10);
}
Next: Arduino Esplora Joystick Mouse with Button function
No comments:
Post a Comment