#include #include #include #include #include #define encoderStepsPerRev 24 // Number of pulse per revolution of the encoder. Change this value to match your encoder. #define outputStepsPerRev 24 // Nuber of pulse per revolution for output. Must be equal to or lower than encoderStepsPerRev. Change this if you want to adjust the tuning rate. #define reverse true // Set to true to reverse the tuning direction of the encoder. #define RPHA 2 // Rotary encoder A phase pin #define RPHB 3 // Rotary encoder B phase pin #define LEFTBUTTON 4 #define RIGHTBUTTON 5 #define MIDDLEBUTTON 6 volatile long counts; Encoder Enc(RPHA, RPHB); #define stepsPerClick 4 void setup() { // Reboot parameters uint16_t bootKey = 0x7777; uint16_t *const bootKeyPtr = (uint16_t *)0x0800; delay(8000); // Wait for Arduino bootup // Reboot for USB connection bug correction if(EEPROM.read(0) != 1){ EEPROM.update(0, 1); *bootKeyPtr = bootKey; wdt_enable(WDTO_1S); while(1){} } EEPROM.update(0, 0); Mouse.begin(); Enc.write(0); pinMode(LEFTBUTTON, INPUT_PULLUP); pinMode(RIGHTBUTTON, INPUT_PULLUP); pinMode(MIDDLEBUTTON, INPUT_PULLUP); MsTimer2::set(1, readEnc); // Interval time 1ms MsTimer2::start(); } void loop() { if(counts != 0){ if(reverse == true){ counts = 0 - counts; } Mouse.move(0, 0, counts); Enc.write(0); } readButton(LEFTBUTTON, MOUSE_LEFT); readButton(RIGHTBUTTON, MOUSE_RIGHT); readButton(MIDDLEBUTTON, MOUSE_MIDDLE); } void readEnc() { counts = Enc.read() / ((encoderStepsPerRev * stepsPerClick) / outputStepsPerRev); } void readButton(int pinNo, uint8_t Button) { while(digitalRead(pinNo) == LOW){ Mouse.press(Button); Enc.write(0); } Mouse.release(Button); delay(10); // Chattering prevention }