Created
November 13, 2016 18:21
-
-
Save Synergyst/7ec710bfd54df3c9dbb4c9b8f5afb2a4 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" | |
/* | |
* Benchmark results were determined by using the following projecct-unrelated website: https://cookie.riimu.net/speed/ | |
* | |
* Refresh rates to MS ratio translations(REL / HLD) | |
* 10 / 10 = smooth 48Hz | |
* 9 / 10 = smoothed 48 to 52Hz | |
* 8 / 10 = semi-smooth 52Hz | |
* 7 / 10 = smooth 56Hz | |
* 6 / 10 = semi-smooth 58 to 60Hz | |
* | |
*/ | |
int delayClickRelAmt = 6; | |
int delayClickHldAmt = 10; | |
#define maxMoveAmt 4 | |
#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; | |
Serial.println("CHNG"); | |
/*Mouse.press(2); | |
delay(115);*/ | |
for (int d; d < 1024; d++) { | |
Mouse.press(1); | |
delay(delayClickHldAmt); | |
Mouse.release(1); | |
delay(delayClickRelAmt); | |
} | |
//Mouse.release(2); | |
} | |
} | |
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) { | |
Serial.print("Hold: "); | |
Serial.println(delayClickHldAmt); | |
delayClickHldAmt--; | |
} else { | |
Serial.print("Release: "); | |
Serial.println(delayClickRelAmt); | |
delayClickRelAmt++; | |
} | |
} | |
} else { | |
if (digitalRead(encoder0Button) == 0) { | |
Mouse.click(1); | |
Serial.println("THROW-L"); | |
} else { | |
if (encButtonState == false) { | |
Serial.print("Release: "); | |
Serial.println(delayClickRelAmt); | |
delayClickRelAmt--; | |
} else { | |
Serial.print("Hold: "); | |
Serial.println(delayClickHldAmt); | |
delayClickHldAmt++; | |
} | |
} | |
} | |
//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