Skip to content

Instantly share code, notes, and snippets.

@dloman
Created October 16, 2013 21:47
Show Gist options
  • Save dloman/7015483 to your computer and use it in GitHub Desktop.
Save dloman/7015483 to your computer and use it in GitHub Desktop.
quadrature encoder -> servo
#include <Servo.h>
Servo MyServo;
void setup()
{
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, encoderPinChangeA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, encoderPinChangeB, CHANGE);
encoder = 0;
//attach servo to pin 9
MyServo.attach(9)
}
void encoderPinChangeA() {
if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
encoder--;
}
else{
encoder++;
}
}
void encoderPinChangeB() {
if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
encoder--;
}
else {
encoder++;
}
}
void loop()
{
MyServo.write(encoder)
delay(10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment