Created
April 19, 2019 23:33
-
-
Save jazzyjackson/c6d2d9c81c7046f17c38f1e7c2149b0e 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
#define XPIN 2 | |
#define YPIN 3 | |
#define X 0 | |
#define Y 1 | |
volatile short timeHigh[2] = {0, 0}; | |
volatile short timeLow[2] = {0, 0}; | |
volatile long lastFalling[2] = {0, 0}; | |
volatile long lastRising[2] = {0, 0}; | |
void setup() { | |
Serial.begin(9600); | |
attachInterrupt(digitalPinToInterrupt(XPIN), Xdutycycleupdate, CHANGE); | |
attachInterrupt(digitalPinToInterrupt(YPIN), Ydutycycleupdate, CHANGE); | |
} | |
void loop() { | |
if ((millis() / 10000) % 2) { | |
Serial.println(checkDutyCycle(X)); | |
} else { | |
Serial.println(checkDutyCycle(Y)); | |
} | |
delay(10); | |
} | |
void Xdutycycleupdate() { | |
digitalRead(XPIN) ? risingUpdate(X) : fallingUpdate(X); | |
} | |
void Ydutycycleupdate() { | |
digitalRead(YPIN) ? risingUpdate(Y) : fallingUpdate(Y); | |
} | |
// called after the signal has been high, on a falling signal. | |
void fallingUpdate(byte axis) { | |
lastFalling[axis] = micros(); | |
timeHigh[axis] = lastFalling[axis] - lastRising[axis]; | |
} | |
void risingUpdate(byte axis) { | |
lastRising[axis] = micros(); | |
timeLow[axis] = lastRising[axis] - lastFalling[axis]; | |
} | |
byte checkDutyCycle(byte axis) { | |
return map(timeHigh[axis], 0, timeLow[axis] + timeHigh[axis], 0, 255); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment