Let's Make Robots!

Rover 5: how to measure distance with one encoder output

I recently bought a Rover 5, Arduino Uno and 2A Motor Shield and made my first sketches.
I would now like to use the encoders to control the motors and I have a question about that.

I would like to measure distance and am not interested in direction.
That must be possible with one encoder output but how?

I experimented with Encoder.h. .
That provides the desired distance information, but requires two encoder outputs (myEnc.Read) – and I only want to use one encoder output.

I also experimented with digitalRead () but that only delivers “1” and “0” and no distance information.

I'm stuck and am looking for help and/or suggestions.

Thanks in advance. Regards, Ko

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks for your replies. I solved the distance measuring with an interrupt (sub routine), as follows:

 

// Sketch @ distance determination with 1 encoder output using an interrupt (INT0)

  

volatile int distance = 0;

 

void setup() {

   // EncoderPin attached to pin 2 (Uno INT 0) | for distance measurement with one encoder output!

    attachInterrupt (0,ISR_Encoder,CHANGE);

  }

 

void loop() {

  }

 

void ISR_Encoder()

{

  distance = distance + 1;

}

 

Kind regards, Ko

The Rover 5 uses quadrature encoders that have 2 outputs. Both outputs are roughly 50% duty cycle but are out of phase by 90°.

For your needs you can select either output or use an XOR gate to mix them into a single output with twice the resolution. Your code should know which direction you are traveling in so then you can just count the pulses to measure distance. Use your interrupt inputs for counting the pulses to ensure you get a reliable count.

Use digitalRead().  Keep track of the last reading, 1 or 0.  When the reading changes from 1 to 0 or from 0 to 1, add one to the distance measurement.  You have to make sure you read it fast enough to not miss any transitions.  Interrupts are the best method so the processor doesn't waste time reading it when not necessary. You can mostly keep track of direction, since your program knows which way the wheel is supposed to be turning.  Then you can add 1 if moving forward, subtract 1 if moving backward.

One encoder/wheel has 2 outputs, A and B. You need both to do something useful with the encoder, that's why you also need 2 for the encoder library.