Difference between revisions of "Time is a technology"

From Lahaag - Project wiki
Jump to navigationJump to search
 
Line 5: Line 5:
  
 
<syntaxhighlight lang="C">
 
<syntaxhighlight lang="C">
#define ENCODER_A_PIN 2#define ENCODER_B_PIN 3long 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++;  }}
+
#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++;   
 +
}
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 19:17, 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++;  
}
}