0% found this document useful (0 votes)
3 views2 pages

Arduino Mouse Control with Encoder

This document contains Arduino code for a mouse control application using an encoder and a button to switch modes between vertical and horizontal movement. The code initializes the mouse and sets up input pins for the encoder and button, handling rotation and mode changes in the loop function. It also implements a debounce mechanism for the button and stores the current mode in EEPROM for persistence across resets.

Uploaded by

nburak07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Arduino Mouse Control with Encoder

This document contains Arduino code for a mouse control application using an encoder and a button to switch modes between vertical and horizontal movement. The code initializes the mouse and sets up input pins for the encoder and button, handling rotation and mode changes in the loop function. It also implements a debounce mechanism for the button and stores the current mode in EEPROM for persistence across resets.

Uploaded by

nburak07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Mouse.

h>
#include <EEPROM.h>

#define OUTPUT_A 2
#define OUTPUT_B 4
#define BUTTON A1
#define PIN_GND_ENCODER 3
#define PIN_GND_BUTTON A3

#define RESOLUTION 2 // hassasiyet azaltıcı

#define VERTICAL_MODE 0
#define HORIZONTAL_MODE 1
const int numModes = 2;
int mode = VERTICAL_MODE;

bool aLastState;
bool lastButtonState = HIGH;
long tempCount = 0;

unsigned long lastMovementTime = 0;


unsigned long currentTime = 0;

void setup() {
[Link]();
mode = [Link](0);

pinMode(OUTPUT_A, INPUT_PULLUP);
pinMode(OUTPUT_B, INPUT_PULLUP);
pinMode(BUTTON, INPUT_PULLUP);

pinMode(PIN_GND_ENCODER, OUTPUT);
digitalWrite(PIN_GND_ENCODER, LOW);
pinMode(PIN_GND_BUTTON, OUTPUT);
digitalWrite(PIN_GND_BUTTON, LOW);

aLastState = digitalRead(OUTPUT_A);
}

void loop() {
bool aState = digitalRead(OUTPUT_A);

if (aState != aLastState) {
int direction = (digitalRead(OUTPUT_B) != aState) ? -1 : 1;
handleRotation(-direction); // Yön tersine çevrildi
aLastState = aState;
}

// Buton ile mod değiştirme


if (digitalRead(BUTTON) == LOW) {
if (lastButtonState == HIGH) {
delay(250); // debounce
changeMode();
}
lastButtonState = LOW;
} else {
lastButtonState = HIGH;
}
}
void handleRotation(int direction) {
if (tempCount++ % RESOLUTION != 0) return;

currentTime = millis();
unsigned long interval = currentTime - lastMovementTime;
lastMovementTime = currentTime;

int steps = 1;
if (interval < 10) {
steps = 5;
} else if (interval < 20) {
steps = 4;
} else if (interval < 40) {
steps = 3;
} else if (interval < 70) {
steps = 2;
}

for (int i = 0; i < steps; i++) {


if (mode == VERTICAL_MODE) {
[Link](0, 0, direction); // scroll
} else {
[Link](direction * 5, 0, 0); // horizontal move
}
}
}

void changeMode() {
mode = (mode + 1) % numModes;
[Link](0, mode);
}

You might also like