I am working on a project which i am going to use the arduino leonardo board and the ADXL 335 accelerometer to make a mouse.
I want to use the ADXL 335 to control the movement of the cursor. However, there is something wrong happened.
const int switchPin = 2; // switch to turn on and off mouse control
const int mouseButton = 0; // input pin for the mouse pushButton
const int xAxis = A1; // joystick X axis
const int yAxis = A2; // joystick Y axis
int fsrReading;
// parameters for reading the joystick:
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range/4; // resting threshold
int center = range/2; // resting position value
boolean mouseIsActive = true; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state
void setup() {
pinMode(mouseButton, INPUT);
// take control of the mouse:
Mouse.begin();
}
void loop() {
// read and scale the two axes:
int xReading = readAxis(A1);
int yReading = readAxis(A2);
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
}
int clickState = analogRead(mouseButton);
// if the mouse button is pressed:
if (clickState > 0) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// 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);
}
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
reading = map(reading, 0, 1023, 0, range);
int distance = reading - center;
return distance;
}
The above code are modified from the arduino examples. ( mouse button control and the joysticks mouse control )
For the mouse button control, I have already done it and there is no problem.
But for the mouse cursor control, after running the program, I found that the cursor keeps moving upwards.
Can someone point out where is the problem?