Difference between revisions of "Time is a technology"
From Lahaag - Project wiki
Jump to navigationJump to searchLine 30: | Line 30: | ||
void read_quadrature() | void read_quadrature() | ||
{ | { | ||
− | // found a low-to-high on channel A | + | // found a low-to-high on channel A |
− | if (digitalRead(ENCODER_A_PIN) == HIGH) | + | if (digitalRead(ENCODER_A_PIN) == HIGH) |
− | { // check channel B to see which way | + | { // check channel B to see which way |
if (digitalRead(ENCODER_B_PIN) == LOW) | if (digitalRead(ENCODER_B_PIN) == LOW) |
Revision as of 19:41, 29 January 2014
code for arduino
using the makerbot magnetic rotary encoder v2.1 here is the code
#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3
long position;
void setup()
{
Serial.begin(19200);
Serial.println("Started");
pinMode(ENCODER_A_PIN, INPUT);
pinMode(ENCODER_B_PIN, INPUT);
attachInterrupt(0, read_quadrature, CHANGE);
}
void loop()
{
Serial.print("Position: ");
Serial.println(position, DEC);
delay(1000);
}
void read_quadrature()
{
// found a low-to-high on channel A
if (digitalRead(ENCODER_A_PIN) == HIGH)
{ // check channel B to see which way
if (digitalRead(ENCODER_B_PIN) == LOW)
position++;
else
position--;
}
// found a high-to-low on channel A
else
{ // check channel B to see which way
if (digitalRead(ENCODER_B_PIN) == LOW)
position--;
else
position++;
}
}