Hi, this is my very first post here as I am new to the community so hello everybody, also I am new to programming and can't wrap my head around the problem that I am heaving.
My setup: I have PS2 mouse sensor attached to Arduino Leonardo and switch, Arduino, is hooked to a PC using USB port and I want to read mouse sensor data and send it to PC.
When mouse is active (I am using switch button to activate mouse ) that part of project is working I am getting x and y coordinates and sending them to pc and moving cursor, now I want to add peace of code that sense, when x and y data are different from 0 and press left mouse click and when they are 0 to release left mouse click, this part is working also but not the way I want, it to send ton of mouse clicks and releases I just want to press it and hold it while x and y are !=0 and to release it when x, y = 0 so if anybody has an idea how to achieve this or can guide me full code is below.
Thanks.
/*
USB Pinout (Left to Right, USB symbol up)
4: GND
3: Clk
2: Data
1: Vcc
*/
#include "PS2Mouse.h"
#include <Mouse.h>
const int switchPin = 2; // switch to turn on and off mouse control
const int ledPin = LED_BUILTIN; // Mouse control LED
// parameters for reading the mouse:
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 = false; // whether or not to control the mouser
int lastSwitchState = LOW; // previous switch state
PS2Mouse mouse(6,5);
void setup(){
Serial.begin(9600);
while(!Serial);
Serial.print("Setup...");
mouse.begin();
Serial.println("complete!");
// initialize digital pin LED_BUILTIN as an output.
pinMode(switchPin, INPUT); // the switch pin
pinMode(ledPin, OUTPUT); // the LED
// take control of the mouse:
Mouse.begin();
delay(5000);
}
void loop(){
// read the switch:
int switchState = digitalRead(switchPin);
// if it's changed and it's high, toggle the mouse state:
if (switchState != lastSwitchState) {
if (switchState == HIGH) {
mouseIsActive = !mouseIsActive;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
}
// save switch state for next comparison:
lastSwitchState = switchState;
uint8_t stat;
int x,y;
mouse.getPosition(stat,x,y);
// read and scale the two axes:
int xReading = x;
int yReading = -y;
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
Serial.println("ON");
if (xReading + yReading != 0) {
Mouse.press();
Serial.println("click");
}
else{
Mouse.release();
Serial.println("click release");
}
}
else{
Serial.println("OFF");
}
Serial.print("\tx=");
Serial.print(x);
Serial.print("\ty=");
Serial.println(y, DEC);
//delay(1000);
}
/*
reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to <range>
*/
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);
// if the output reading is outside from the
// rest position threshold, use it:
int distance = reading - center;
if (abs(distance) < threshold) {
distance = 0;
}
// return the distance for this axis:
return distance;
}