#include #define ENCA 2 #define ENCB 3 const uint8_t encodeA[] = {0b10, 0b00, 0b11, 0b01}; //const uint8_t encodeB[] = {0b01, 0b11, 0b00, 0b10}; volatile int counter; volatile uint8_t preab; int preCount; void setup() { pinMode(ENCA, INPUT_PULLUP); pinMode(ENCB, INPUT_PULLUP); //マウス初期化 Mouse.begin(); counter = 0; preCount = 0; preab = 0; attachInterrupt(digitalPinToInterrupt(ENCA), pulse, CHANGE); attachInterrupt(digitalPinToInterrupt(ENCB), pulse, CHANGE); } void pulse() { uint8_t a, b, ab; a = digitalRead(ENCA); b = digitalRead(ENCB); ab = (a << 1) | b; if(preab != ab) { if(encodeA[preab] == ab) --counter; else //if(encodeB[preab] == ab) ++counter; preab = ab; } preab = ab; } void loop() { int diff; diff = counter - preCount; if(diff != 0) { Mouse.move(diff << 3, 0, 0); preCount = counter; } }