Time is a technology

From Lahaag - Project wiki
Revision as of 19:41, 29 January 2014 by Atkn (talk | contribs)
Jump to navigationJump to search

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++;  
}
}