Last active
November 13, 2016 16:30
-
-
Save Synergyst/4e6a84a0fae555f7e14a6c01bd017a7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HID-Project.h" | |
#define maxMoveAmt 1 | |
#define encoder0PinA 2 | |
#define encoder0PinB 3 | |
#define encoder0Button 4 | |
volatile unsigned int encoder0Pos = 0; | |
unsigned int tmp_Pos = 1; | |
unsigned int valx; | |
unsigned int valy; | |
unsigned int valz; | |
boolean A_set; | |
boolean B_set; | |
boolean encPosi; | |
// the current state of the output pin | |
int encButtonState = HIGH; | |
// the current reading from the input pin | |
int buttonState; | |
// the previous reading from the input pin | |
int lastButtonState = LOW; | |
/* | |
the following variables are long's because the time, measured in miliseconds, | |
will quickly become a bigger number that can't be stored as an int. | |
*/ | |
// the last time the output pin was toggled | |
long lastDebounceTime = 0; | |
// the debounce time; increase if the output flickers | |
long debounceDelay = 60; | |
void setup() { | |
pinMode(encoder0Button, INPUT); | |
digitalWrite(encoder0Button, HIGH); | |
pinMode(encoder0PinA, INPUT); | |
pinMode(encoder0PinB, INPUT); | |
// encoder pin on interrupt 0 (pin 2) | |
attachInterrupt(0, doEncoderA, CHANGE); | |
// encoder pin on interrupt 1 (pin 3) | |
attachInterrupt(1, doEncoderB, CHANGE); | |
Serial.begin(115200); | |
} | |
void loop() { | |
// read the state of the switch into a local variable: | |
int reading = digitalRead(encoder0Button); | |
// check to see if you just pressed the button | |
// (i.e. the input went from LOW to HIGH), and you've waited | |
// long enough since the last press to ignore any noise: | |
// If the button changed, due to noise: | |
if (reading != lastButtonState) { | |
// reset the debouncing timer | |
lastDebounceTime = millis(); | |
if (buttonState == HIGH) { | |
encButtonState = !encButtonState; | |
} | |
} | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
// whatever the reading is at, it's been there for longer | |
// than the debounce delay, so take it as the actual current state: | |
buttonState = reading; | |
} | |
// set the LED using the state of the button: | |
/*if (encButtonState == 0) { | |
Serial.println("HIGH"); | |
} else { | |
Serial.println("LOW"); | |
}*/ | |
// save the reading. Next time through the loop, | |
// it'll be the lastButtonState: | |
lastButtonState = reading; | |
//Check each second for change in position | |
if (tmp_Pos != encoder0Pos) { | |
if (encPosi == true) { | |
if (digitalRead(encoder0Button) == 0) { | |
Mouse.click(2); | |
Serial.println("THROW-R"); | |
} else { | |
if (encButtonState == true) { | |
Mouse.move(0, maxMoveAmt); | |
Serial.println("Down"); | |
} else { | |
Mouse.move(maxMoveAmt, 0); | |
Serial.println("Right"); | |
} | |
} | |
} else { | |
if (digitalRead(encoder0Button) == 0) { | |
Mouse.click(1); | |
Serial.println("THROW-L"); | |
} else { | |
if (encButtonState == false) { | |
Mouse.move(-maxMoveAmt, 0); | |
Serial.println("Left"); | |
} else { | |
Mouse.move(0, -maxMoveAmt); | |
Serial.println("Up"); | |
} | |
} | |
} | |
//Serial.println(encoder0Pos, DEC); | |
tmp_Pos = encoder0Pos; | |
} | |
} | |
// Interrupt on A changing state | |
void doEncoderA() { | |
// Low to High transition? | |
if (digitalRead(encoder0PinA) == HIGH) { | |
A_set = true; | |
if (!B_set) { | |
encoder0Pos = encoder0Pos + 1; | |
valx = analogRead(0); | |
valy = analogRead(1); | |
valz = analogRead(2); | |
} | |
} | |
// High-to-low transition? | |
if (digitalRead(encoder0PinA) == LOW) { | |
A_set = false; | |
} | |
encPosi = true; | |
} | |
// Interrupt on B changing state | |
void doEncoderB() { | |
// Low-to-high transition? | |
if (digitalRead(encoder0PinB) == HIGH) { | |
B_set = true; | |
if (!A_set) { | |
encoder0Pos = encoder0Pos - 1; | |
} | |
} | |
// High-to-low transition? | |
if (digitalRead(encoder0PinB) == LOW) { | |
B_set = false; | |
} | |
encPosi = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Final update(probably)! - Adds full mouse control(minus scrolling)
Turning the encoder left or right will move the current axis.
Clicking the rotary encoder's internal button will change the axis.
Clicking and holding the internal button and rotating left or right will perform a left or right click!
TODO: Hopefully add EEPROM memory storage so that the last axis(before a reflash/reset/power cycle) will remain in memory.