Picture of an experimental setting by Diego Cuartielles with the maxon motor and the 500-steps encoder





 
Examples  
/* Read Encoder Direction
* by DojoCorp <http://www.0j0.org>
*
* Demonstrates how to detect the direction
* towards which a motor spins through using
* a two lines encoder. The testing procedure
* is to:
* - press pushbutton 1 on the board to start the test
* - make the encoder spin by hand
* - press pushbutton 1 on the board to stop the test
* - read the result on the serial port configured at 9600
*
* - by the end of the test, button 2 will restart
*
* Description of the pinout used by the motor control board:
* - digital pin 39 reads pushbutton 1
* - digital pin 38 reads pushbutton 2
* - digital pin 0 reads encoder's input A (we just need one encoder line)
* - digital pin 1 reads encoder's cross by zero (not used)
* - digital pin 4 reads encoder's input B (not used)
*
* Created 25 March 2005
*/

int encoderA = 0; // pin 0 is the encoder's line A
int encoderCross = 1; // pin 1 is the encoder's cross by zero
int encoderB = 4; // pin 4 is the encoder's line B
int inB = 38; // reads the pin 38 as a pushbutton (pushbutt 2)
int inA = 39; // reads the pin 39 as a pushbutton (pushbutt 1)
boolean testing = false; // memorize if we are in the testing process
int ledpin = 48; // show the status on while testing, off otherwise
int lastState = LOW; // monitor last state of the measuring pin
int state = LOW; // monitor current state of the measuring pin
void setup() {
   pinMode(inA, INPUT); 
   pinMode(inB, INPUT); 
   pinMode(encoderA, INPUT); 
   pinMode(encoderCross, INPUT); 
   pinMode(encoderB, INPUT); 
   pinMode(ledpin, OUTPUT); 
   beginSerial(9600);
   printMode(SERIAL);
   print("Start Program \n\r");
   print("Verbose Active \n\r");
}
void loop() {
   // Waits until button 1 pressed to start the test
   while (!testing) {
      if (digitalRead(inA) == HIGH) {
         testing = true;
      }
      delay(100);
   }
   
   // Test starts, counting steps in the encoder
   print("Start Test \n\r");
   digitalWrite(ledpin, HIGH);
   
   while (testing) {
      state = digitalRead(encoderA);
      if ((state == HIGH) && (lastState == LOW)) {
         // only if there is a transition from low to high
         if (digitalRead(encoderB) == HIGH) {
            print("Rotating in direction 1");
         } else {
            print("Rotating in direction 2");
         } 
      }
      lastState = state;
      if (digitalRead(inA) == HIGH) {
         testing = false;
      }
      delay(50); // this time value is our temporal resolution
   }
   
   // Final info
   print("Test if finished \n\r");
   digitalWrite(ledpin, LOW);
   print("Press button 2 to repeat \n\r");
   while (digitalRead(inB) == LOW) {
      delay(100);
   }
}

Description  

This program is a test designed to operate the direction an encoder is spinning towards. It requires to connect an encoder to the motor control board, to the ENCODER 1 LINE A and LINE B pins. We will use those pins to count the pulses coming from the encoder. The way of measuring the arrival of a pulse is through comparing the state of the input to how it was short ago. If the state changed from LOW to HIGH that means that a pulse arrived and that we will have to count one more step in the encoder. Once we detect a pulse on LINE A, we check the value at LINE B. As explained in the theory, depending on if it is LOW or HIGH the encoder will be spinning in one direction or the other

The example uses the serial port to send the information back to a computer constantly, there is no verbose mode in this program. When the test is running, the wiring on-board's LED lights up. The way of entering the test mode is through pressing pushbutton nr. 1

Once we have finished the test, we have to press button nr. 1 again to indicate the end of it. The program will then send an indication of pressing button nr. 2 for repeating the whole operation

   
Syntax   Please refer to: beginSerial(int) and print(string) at the reference
   
Parameters  
encoderA   input from encoder's line A, we will be counting the pulses through this pin. On the i/o board is pin nr. 0

   
encoderB   input from encoder's line B, we will be counting the pulses through this pin. On the i/o board is pin nr. 4

   
inA   HIGH or LOW, it is the input coming from pushbutton nr. 1. We use it for starting and stopping the test

   
inB   HIGH or LOW, it is the input coming from pushbutton nr. 2. We use it to re-initiate the test process