Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created July 20, 2017 20:41
Show Gist options
  • Select an option

  • Save ShawnHymel/fa10aa75e5572b0f551ebcaf20179633 to your computer and use it in GitHub Desktop.

Select an option

Save ShawnHymel/fa10aa75e5572b0f551ebcaf20179633 to your computer and use it in GitHub Desktop.
Quadrature Encoder Demo
// Parameters
const int threshold = 600;
const float multiplier = 1.4;
// Pins
const int i_pin = A1;
const int q_pin = A0;
const int led_pin = 8;
const int lamp_pin = 9;
// Globals
int i_prev = 0;
int q_prev = 0;
float brightness = 1;
void setup() {
// Set up pins
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, HIGH);
pinMode(lamp_pin, OUTPUT);
// Set up Serial
Serial.begin(9600);
// Measure initial Q
i_prev = (analogRead(i_pin) > threshold) ? 1 : 0;
}
void loop() {
// Measure
int i_state = (analogRead(i_pin) > threshold) ? 1 : 0;
int q_state = (analogRead(q_pin) > threshold) ? 1 : 0;
// Look for falling edge on I
if ( (i_prev != i_state) && (i_state == 0) ) {
// If I and Q are the same, CW, otherwise CCW
if ( i_state == q_state ) {
brightness *= multiplier;
if ( brightness >= 255 ) brightness = 255;
} else {
brightness /= multiplier;
if ( brightness <= 1 ) brightness = 1;
}
// Print counter
Serial.println(brightness);
// Set LED
analogWrite(lamp_pin, (int)brightness);
}
// Save state for next sample
i_prev = i_state;
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment