Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created October 28, 2012 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmpvar/3968145 to your computer and use it in GitHub Desktop.
Save tmpvar/3968145 to your computer and use it in GitHub Desktop.
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
int lasta=0, lastb=0;
int dira=0, dirb=0;
int peaka=346, peakb=349;
int position = 0;
int cycle = 0;
void loop() {
digitalWrite(13, HIGH);
int currenta = analogRead(0);
int currentb = analogRead(1);
if (currenta == lasta || currentb == lastb) {
return;
}
//Serial.print(currenta);
//Serial.print(" - ");
//Serial.println(currentb);
if (currenta > currentb) {
if (currenta == peaka) {
// CW
if (currentb > lastb) {
position++;
// CCW
} else if (currentb < lastb) {
position--;
}
}
} else if (currentb > currenta) {
if (currentb == peakb) {
// CW
if (currenta < lasta) {
position++;
// CCW
} else if (currenta > lasta) {
position--;
}
}
}
Serial.println(position);
lasta = currenta;
lastb = currentb;
delay(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment