[ad_1]
I’m utilizing :
- an Arduino Micro Professional
- two push buttons as left and proper mouse buttons
- a rotary encoder as scroll wheel
I managed to click on the buttons and it’s working nice, however
I wish to click on the button, maintain the state till I click on a second time to launch:
First click on -> change state and maintain -> second click on -> launch state
Right here is my code:
#embrace <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; // Shops the preliminary worth of every axis, normally round 512
int vertValue, horzValue; // Shops present analog output of every axis
const int sensitivity = 200; // Larger sensitivity worth = slower mouse, ought to be <= about 500
int mouseClickFlag = 0; //click on verify sw 1
int b1flag = 0; //click on verify sw2
//int invertMouse = 1; //Invert joystick primarily based on orientation
int invertMouse = -1; //Noninverted joystick primarily based on orientation
int encoderCLK = 2;
int encoderDT = 15;
// Variables
int counter = 0;
int currentStateCLK;
int lastStateCLK;
void setup() {
pinMode(horzPin, INPUT_PULLUP); // Set each analog pins as inputs
pinMode(vertPin, INPUT_PULLUP);
pinMode(selPin, INPUT_PULLUP); // set button choose pin as enter
pinMode(selpin2, INPUT_PULLUP); // set button choose pin as enter
digitalWrite(selPin, HIGH); // Pull button choose pin excessive
digitalWrite(selpin2, HIGH); // Pull button choose pin excessive
delay(1000); // brief delay to let outputs settle
vertZero = analogRead(vertPin); // get the preliminary values
horzZero = analogRead(horzPin); // Joystick ought to be in impartial place when studying these
Mouse.start(); //Init mouse emulation
//Pin Initialization
pinMode (encoderCLK, INPUT_PULLUP);
pinMode (encoderDT, INPUT_PULLUP);
//initialize mouse management:
Mouse.start();
}
void loop() {
vertValue = analogRead(vertPin) - vertZero; // learn vertical offset
horzValue = analogRead(horzPin) - horzZero; // learn horizontal offset
if (vertValue != 0)
Mouse.transfer(0, (invertMouse * (vertValue / sensitivity)), 0); // transfer mouse on y axis
if (horzValue != 0)
Mouse.transfer((invertMouse * (horzValue / sensitivity)), 0, 0); // transfer mouse on x axis
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) { // if the button 1 is pressed
mouseClickFlag = 1;
Mouse.press(MOUSE_RIGHT); // click on the precise button down
}
else if ((digitalRead(selPin)) && (mouseClickFlag)) { // if the button 1 shouldn't be pressed
mouseClickFlag = 0;
Mouse.launch(MOUSE_RIGHT); // launch the precise button
}
// second button
if ((digitalRead(selpin2) == 0) && (!b1flag)) { // if the button 2 is pressed
b1flag = 1;
Mouse.click on(MOUSE_LEFT);
}
else if ((digitalRead(selpin2)) && (b1flag)) { // if the button 2 shouldn't be pressed
b1flag = 0;
Mouse.launch(MOUSE_LEFT); // launch the left button
}
// Your present code for joystick and buttons
// Learn the present state of CLK
currentStateCLK = digitalRead(encoderCLK);
// If final and present state of CLK are completely different, then pulse occurred
// React to just one state change to keep away from double rely
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
// If the DT state is completely different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(encoderDT) != currentStateCLK) {
counter++;
} else {
// Encoder is rotating CW so increment
counter--;
}
}
// Keep in mind final CLK state
lastStateCLK = currentStateCLK;
// Transfer the Scroll Wheel provided that the counter has modified
if (counter != 0) {
Mouse.transfer(0, 0, counter);
counter = 0; // Reset counter after scrolling
}
}
[ad_2]