Skip to main content
added 819 characters in body
Source Link
crucl
  • 11
  • 1
  • 3

I have a HP Optical Incremental Encoder (256 CPR) in which

  • Pin 1 = A,
  • Pin 2 = VCC,
  • Pin 3 = GND,
  • Pin 8 = B.

I already read and see tutorials for rotary encoder such this one:
https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder, however I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

I was able to get it to work. I wasn't connecting my pin properly to my Arduino.

    #include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


#define encoderPinA 2
#define encoderPinB 3
#define CPR 256
volatile int counter =0;
volatile boolean flag;

volatile int var_degrees =0; 

void setup() { 

  pinMode(encoderPinA, OUTPUTINPUT);
  pinMode(encoderPinB, OUTPUTINPUT);
  Serial.begin (9600);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), isr,CHANGE RISING);
  lcd.clear();
  
}

void loop() {
  
   if(flag == true){     
        var_degrees = ((360/256.0)*counter);
        Serial.println(var_degrees);
        lcd.setCursor(0, 1);
        lcd.print(counter"Degrees: ");
        lcd.setCursor(9, 1);
        lcd.print(var_degrees);
        flag = false;
  }
   
}
//Interrupts  

void isrisr_2(){
flag = true;
  if(digitalRead(encoderPinA) == intHIGH){
 channel_A =  if(digitalRead(encoderPinAencoderPinB); == LOW){
    int channel_B counter = digitalRead(encoderPinB);counter -1; //COUNTER CLOCK WISE
    }
    else{
      counter = counter +1; //CW
    }
  }
  else{
    if(channel_AdigitalRead(encoderPinB) == channel_BLOW){
    counter++;  counter = counter + 1; //CW
    }
    else{
      counter--;
 = counter }
- 1 ; //CCW
    }
  }
}

I have a HP Optical Incremental Encoder (256 CPR) in which

  • Pin 1 = A,
  • Pin 2 = VCC,
  • Pin 3 = GND,
  • Pin 8 = B.

I already read and see tutorials for rotary encoder such this one:
https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder, however I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

#define encoderPinA 2
#define encoderPinB 3
int counter =0;
void setup() {
  pinMode(encoderPinA, OUTPUT);
  pinMode(encoderPinB, OUTPUT);
  Serial.begin (9600);
  attachInterrupt(encoderPinA, isr,CHANGE);
}

void loop() {

  Serial.print(counter);

}
//Interrupts 
void isr(){

    int channel_A = digitalRead(encoderPinA);
    int channel_B = digitalRead(encoderPinB);
  
   if(channel_A == channel_B){
    counter++;
   }
   else{
    counter--;
   }
         
}

I have a HP Optical Incremental Encoder (256 CPR) in which

  • Pin 1 = A,
  • Pin 2 = VCC,
  • Pin 3 = GND,
  • Pin 8 = B.

I already read and see tutorials for rotary encoder such this one:
https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder, however I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

I was able to get it to work. I wasn't connecting my pin properly to my Arduino.

    #include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


#define encoderPinA 2
#define encoderPinB 3
#define CPR 256
volatile int counter =0;
volatile boolean flag;

volatile int var_degrees =0; 

void setup() { 

  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  Serial.begin (9600);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), isr, RISING);
  lcd.clear();
  
}

void loop() {
  
   if(flag == true){     
        var_degrees = ((360/256.0)*counter);
        Serial.println(var_degrees);
        lcd.setCursor(0, 1);
        lcd.print("Degrees: ");
        lcd.setCursor(9, 1);
        lcd.print(var_degrees);
        flag = false;
  }
   
}
//Interrupts  

void isr_2(){
flag = true;
  if(digitalRead(encoderPinA) == HIGH){
    if(digitalRead(encoderPinB) == LOW){
      counter = counter -1; //COUNTER CLOCK WISE
    }
    else{
      counter = counter +1; //CW
    }
  }
  else{
    if(digitalRead(encoderPinB) == LOW){
      counter = counter + 1; //CW
    }
    else{
      counter = counter - 1 ; //CCW
    }
  }
}
added 9 characters in body
Source Link
gre_gor
  • 1.7k
  • 4
  • 18
  • 30

I have a HPHP Optical Incremental Encoder (256 CPR) in which Pin 1 = A, Pin 2 = VCC, Pin 3 = GND, Pin 8 = B.

  • Pin 1 = A,
  • Pin 2 = VCC,
  • Pin 3 = GND,
  • Pin 8 = B.

I already read and see tutorials for rotary encoder such this one  : 
https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder;Howeverencoder, however I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

#define encoderPinA 2
#define encoderPinB 3
int counter =0;
void setup() {
  pinMode(encoderPinA, OUTPUT);
  pinMode(encoderPinB, OUTPUT);
  Serial.begin (9600);
  attachInterrupt(encoderPinA, isr,CHANGE);
}

void loop() {

  Serial.print(counter);

}
//Interrupts 
void isr(){

    int channel_A = digitalRead(encoderPinA);
    int channel_B = digitalRead(encoderPinB);
  
   if(channel_A == channel_B){
    counter++;
   }
   else{
    counter--;
   }
         
}

I have a HP Optical Incremental Encoder (256 CPR) in which Pin 1 = A, Pin 2 = VCC, Pin 3 = GND, Pin 8 = B.

I already read and see tutorials for rotary encoder such this one  : https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder;However, I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

#define encoderPinA 2
#define encoderPinB 3
int counter =0;
void setup() {
  pinMode(encoderPinA, OUTPUT);
  pinMode(encoderPinB, OUTPUT);
  Serial.begin (9600);
  attachInterrupt(encoderPinA, isr,CHANGE);
}

void loop() {

  Serial.print(counter);

}
//Interrupts 
void isr(){

    int channel_A = digitalRead(encoderPinA);
    int channel_B = digitalRead(encoderPinB);
  
   if(channel_A == channel_B){
    counter++;
   }
   else{
    counter--;
   }
         
}

I have a HP Optical Incremental Encoder (256 CPR) in which

  • Pin 1 = A,
  • Pin 2 = VCC,
  • Pin 3 = GND,
  • Pin 8 = B.

I already read and see tutorials for rotary encoder such this one: 
https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder, however I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

#define encoderPinA 2
#define encoderPinB 3
int counter =0;
void setup() {
  pinMode(encoderPinA, OUTPUT);
  pinMode(encoderPinB, OUTPUT);
  Serial.begin (9600);
  attachInterrupt(encoderPinA, isr,CHANGE);
}

void loop() {

  Serial.print(counter);

}
//Interrupts 
void isr(){

    int channel_A = digitalRead(encoderPinA);
    int channel_B = digitalRead(encoderPinB);
  
   if(channel_A == channel_B){
    counter++;
   }
   else{
    counter--;
   }
         
}
Source Link
crucl
  • 11
  • 1
  • 3

Optical Incremental Encoder Reading using Arduino UNO

I have a HP Optical Incremental Encoder (256 CPR) in which Pin 1 = A, Pin 2 = VCC, Pin 3 = GND, Pin 8 = B.

I already read and see tutorials for rotary encoder such this one : https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder;However, I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

#define encoderPinA 2
#define encoderPinB 3
int counter =0;
void setup() {
  pinMode(encoderPinA, OUTPUT);
  pinMode(encoderPinB, OUTPUT);
  Serial.begin (9600);
  attachInterrupt(encoderPinA, isr,CHANGE);
}

void loop() {

  Serial.print(counter);

}
//Interrupts 
void isr(){

    int channel_A = digitalRead(encoderPinA);
    int channel_B = digitalRead(encoderPinB);
  
   if(channel_A == channel_B){
    counter++;
   }
   else{
    counter--;
   }
         
}