Go Down

Topic: IR Phototransistor pair interrupt problem (Read 1 time) previous topic - next topic


I've salvage some ir-photo pair, I want to make rotary encoder as inside the mouse. The purpose is to length/distance measurement using rotary wheel movement.

my circuit is

The problem is when I try the code, the tick count is not reliable when moving rotary wheel faster. Though slow but constant movement gives correct output.

Some other question.

  • Rotary wheel encoder is reliable for correct x/y movement ?

  • When breaking the IR beam (put something between the ir-photo pair) fast or slow, the counts increments more than one.

  • How to achieve high resolution count accurately with these type of encoders

Here is my code, I've try different interrupt mode and also moving count var to main loop but the problem exists.

Code: [Select]

#define encoder0PinA 2
//#define encoder0PinB 4

volatile unsigned int encoder0Pos = 0;
static boolean rotating=false;

void setup() {
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);
  //pinMode(encoder0PinB, INPUT);
  //digitalWrite(encoder0PinB, HIGH);

  attachInterrupt(0, rotEncoder, FALLING); 
  Serial.begin (9600);

void rotEncoder(){
  // If a signal change (noise or otherwise) is detected
  // in the rotary encoder, the flag is set to true

void loop() {
  if (rotating) {
    // When signal changes we wait 2 milliseconds for it to
    // stabilise before reading (increase this value if there
    // still bounce issues)
    rotating=false; // Reset the flag back to false

Above code is from http://www.arduino.cc/playground/Main/RotaryEncoders with little bit of change. This is testing project only, so want to use only one encoder.


One problem you have is that "rotating" should be volatile.


Go Up