Created
October 16, 2013 21:47
-
-
Save dloman/7015483 to your computer and use it in GitHub Desktop.
quadrature encoder -> servo
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 <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