Go Down

Topic: rotary encoder (Read 2 times) previous topic - next topic

mipumat

hi there,

i got a rotary encoder from sparkfun which i have connected to a arduino nano. i am using the following code:

Code: [Select]

int pulses, A_SIG=0, B_SIG=1;

void setup(){
  attachInterrupt(0, A_RISE, RISING);
  attachInterrupt(1, B_RISE, RISING);
  Serial.begin(115200);
}//setup


void loop(){
   
}

void A_RISE(){
detachInterrupt(0);
A_SIG=1;

if(B_SIG==0)
pulses++;//moving forward
if(B_SIG==1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_FALL, FALLING);
}

void A_FALL(){
  detachInterrupt(0);
A_SIG=0;

if(B_SIG==1)
pulses++;//moving forward
if(B_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_RISE, RISING); 
}

void B_RISE(){
detachInterrupt(1);
B_SIG=1;

if(A_SIG==1)
pulses++;//moving forward
if(A_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(1, B_FALL, FALLING);
}

void B_FALL(){
detachInterrupt(1);
B_SIG=0;

if(A_SIG==0)
pulses++;//moving forward
if(A_SIG==1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(1, B_RISE, RISING);
}


and, as i am a newbee, i am in trouble :)   when i start serial monitor i get some numbers coming in and when i turn the encoder also something is happening but not what i want. also numbers are changing when i touch the cables sometimes. so my guess is that i need some kind of pullup oder pulldown resistors somewhere. would someone be so nice and help me out here?

i connected the middle pin of the encoder to gnd on the arduino and the left and right pins to digital 2 and 3. and i do not know much about electronics.

have a nice day!

florinc

Sparkfun usually has some documentation and/or sample code for their products (links after the product description).
Did you check there?

JoeN

I figured out rotary encoders by experimenting with code on this page:

http://www.arduino.cc/playground/Main/RotaryEncoders

I tried several before I got it to work to my satisfaction.  I think the one that worked best for me is under "Another Interrupt Library THAT REALLY WORKS (the Encoder interrupts the processor and debounces like there is no tomorrow)."

I will say, you never do a PinMode(0, INPUT) and PinMode(1, INPUT) which may be necessary.  
I have only come here seeking knowledge. Things they would not teach me of in college.

mipumat

i tried several sketches but the one that works for me is :

Code: [Select]

/*
(Copy and paste)

Rotary encoder decoding using two interrupt lines.
Most Arduino boards have two external interrupts,
numbers 0 (on digital pin 2) and 1 (on digital pin 3).

Program sketch is for SparkFun Rotary Encoder sku: COM-09117
Connect the middle pin of the three to ground.
The outside two pins of the three are connected to
digital pins 2 and 3
*/


volatile int number = 0;                // Testnumber, print it when it changes value,
                                        // used in loop and both interrupt routines
int oldnumber = number;

volatile boolean halfleft = false;      // Used in both interrupt routines
volatile boolean halfright = false;


void setup(){
  Serial.begin(9600);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);                // Turn on internal pullup resistor
  pinMode(3, INPUT);
  digitalWrite(3, HIGH);                // Turn on internal pullup resistor
  attachInterrupt(0, isr_2, FALLING);   // Call isr_2 when digital pin 2 goes LOW
  attachInterrupt(1, isr_3, FALLING);   // Call isr_3 when digital pin 3 goes LOW
}

void loop(){
  if(number != oldnumber){              // Change in value ?
    Serial.println(number);             // Yes, print it (or whatever)
    oldnumber = number;
  }
}

void isr_2(){                                              // Pin2 went LOW
  delay(1);                                                // Debounce time
  if(digitalRead(2) == LOW){                               // Pin2 still LOW ?
    if(digitalRead(3) == HIGH && halfright == false){      // -->
      halfright = true;                                    // One half click clockwise
    } 
    if(digitalRead(3) == LOW && halfleft == true){         // <--
      halfleft = false;                                    // One whole click counter-
      number--;                                            // clockwise
    }
  }
}
void isr_3(){                                             // Pin3 went LOW
  delay(1);                                               // Debounce time
  if(digitalRead(3) == LOW){                              // Pin3 still LOW ?
    if(digitalRead(2) == HIGH && halfleft == false){      // <--
      halfleft = true;                                    // One half  click counter-
    }                                                     // clockwise
    if(digitalRead(2) == LOW && halfright == true){       // -->
      halfright = false;                                  // One whole click clockwise
      number++;
    }
  }
}


i found it here : http://home.online.no/~togalaas/rotary_encoder/

just for other ones who have troubles with this. i am solved :)

JoeN

Maybe because that example sets the PinMode to input...   :smiley-yell:
I have only come here seeking knowledge. Things they would not teach me of in college.

Go Up