switch 1(momentary switch) as right click of the mouse
switch 2 as (momentary switch) as left click of the mouse
joystick to move the mouse arrow
I managed to move the arrow of the mouse with joystick and can click the switch one (right click) working ok
but there is a problem with adding switch 2 it keep clicking with automatically
if (digitalRead(selpin2) == HIGH) {
Mouse.click();
}
Why it is keep clicking and how to fix it as the right switch
Here is the code
#include <Mouse.h>
int horzPin = A0; // Analog output of horizontal joystick pin
int vertPin = A1; // Analog output of vertical joystick pin
int selPin = 7; // select button pin of right click
int selpin2 = 9; // select button pin for left click
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
int vertValue, horzValue; // Stores current analog output of each axis
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0;
//int invertMouse = 1; //Invert joystick based on orientation
int invertMouse = -1; //Noninverted joystick based on orientation
void setup()
{
pinMode(horzPin, INPUT); // Set both analog pins as inputs
pinMode(vertPin, INPUT);
pinMode(selPin, INPUT); // set button select pin as input
pinMode(selpin2, INPUT); // set button select pin as input
digitalWrite(selPin, HIGH); // button select pin high
delay(1000); // short delay to let outputs settle
vertZero = analogRead(vertPin); // get the initial values
horzZero = analogRead(horzPin); // Joystick should be in neutral position when reading these
Mouse.begin(); //Init mouse emulation
}
void loop()
{
vertValue = analogRead(vertPin) - vertZero; // read vertical offset
horzValue = analogRead(horzPin) - horzZero; // read horizontal offset
if (digitalRead(selpin2) == HIGH) {
Mouse.click();
}
if (vertValue != 0)
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis
if (horzValue != 0)
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) // if the button switch 1 right is pressed
{
mouseClickFlag = 1;
Mouse.press(MOUSE_RIGHT); // click the right button down
}
else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the button switch 1 right is not pressed
{
mouseClickFlag = 0;
Mouse.release(MOUSE_RIGHT); // release the right button
}
}
Plenty of videos around on how and why to connect switches to Arduino, preferred methods and ways to debounce both software (preferred by most) or hardware.
Can you include complete schematic of your setup?
No fritzing PLEASE.!!!!!
You need to send a mouse click when a button gets pressed, not if a button is pressed.
To do that, you need to keep track of the previous state of the button and compare it to the current state. Look at the State Change Detection example (File->examples->02.digital->State Change Detection)
You have to do this independently for both switches.
According to your schematic, you have the button wired between the pin and ground which is good, but you need to declare the pin as INPUT_PULLUP to make it work. It will read HIGH when not pressed and LOW when pressed.
pinMode(selPin, INPUT_PULLUP); // set button select pin as input
pinMode(selpin2, INPUT_PULLUP); // set button select pin as input
I managed to solve the problem by making both switch working independently
#include <Mouse.h>
int horzPin = A0; // Analog output of horizontal joystick pin
int vertPin = A1; // Analog output of vertical joystick pin
int selPin = 7; // button of sw 1
int selpin2 = 9; // button of sw 2
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
int vertValue, horzValue; // Stores current analog output of each axis
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0; //click check sw 1
int b1flag = 0; //click check sw2
//int invertMouse = 1; //Invert joystick based on orientation
int invertMouse = -1; //Noninverted joystick based on orientation
void setup()
{
pinMode(horzPin, INPUT); // Set both analog pins as inputs
pinMode(vertPin, INPUT);
pinMode(selPin, INPUT); // set button select pin as input
pinMode(selpin2, INPUT); // set button select pin as input
digitalWrite(selPin, HIGH); // Pull button select pin high
digitalWrite(selpin2, HIGH); // Pull button select pin high
delay(1000); // short delay to let outputs settle
vertZero = analogRead(vertPin); // get the initial values
horzZero = analogRead(horzPin); // Joystick should be in neutral position when reading these
Mouse.begin(); //Init mouse emulation
}
void loop()
{
vertValue = analogRead(vertPin) - vertZero; // read vertical offset
horzValue = analogRead(horzPin) - horzZero; // read horizontal offset
if (vertValue != 0)
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis
if (horzValue != 0)
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) // if the button 1 is pressed
{
mouseClickFlag = 1;
Mouse.press(MOUSE_RIGHT); // click the right button down
}
else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the button 1 is not pressed
{
mouseClickFlag = 0;
Mouse.release(MOUSE_RIGHT); // release the right button
}
// second button
if ((digitalRead(selpin2) == 0) && (!b1flag)) // if the button 2 is pressed
{
b1flag = 1;
Mouse.click(MOUSE_LEFT);
}
else if ((digitalRead(selpin2)) && (b1flag)) // if the button 2 is not pressed
{
b1flag = 0;
Mouse.release(MOUSE_LEFT); // release the left button
}
}
It is working fine now.
I am now think to add rotary encoder to add zoom in and out feature(scroll down and up) do you have a docs for this, if so i will be really appreciated