 # How to set encoder to 365 degrees?

Hello dear friends. I have Encoder OMRON E6B2-CWZ6C. When I rotate the encoder, the value on the screen goes up to 6400. But I want it to value the angle type, i want it to limit the values he gives to 360, so I want him to show 360 degrees when he turns a full lap. This is my current code. Can you help me with this code ?

``````volatile unsigned int counter = 0;  //This variable will increase or decrease depending on the rotation of encoder

void setup() {
Serial.begin (9600);

pinMode(2, INPUT);           // set pin to input
pinMode(3, INPUT);           // set pin to input

digitalWrite(2, HIGH);       // turn on pullup resistors
digitalWrite(3, HIGH);       // turn on pullup resistors

//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);

//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
}

void loop() {
// Send the value of counter
Serial.println (counter);
}

void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
counter++;
}
else{
counter--;
}
}

void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
counter--;
}else{
counter++;
}
}
``````

What you are printing is the value of the counter variable. What is its value when the encoder has rotated 90, 180, 270 and 360 degrees ?

Can you see how the value of counter is related to the number of degrees turned ? It is simple mathematics to derive the angle from the value

You don't say, but I suspect that the value of counter continues to rise if you rotate the encoder more than 360 degrees. If that is the case you can test its value and when it goes over the 360 degree value set it back to zero and when it goes below zero set it to the 360 degree value

When I rotate it 90 degrees with my hand, it gives about 520-535. But it's not exactly value it is approximate. We need to specific and odd values. How can we determine the specific values of this ?

Look into the data sheet! There you'll find 1200 P/R (pulses per round). If you count both phases this will give 2400 counts for 360°.

My encoder is 1024 P/R. Yeah, I guess it shows 2048 when I do a full tour. I understand. So how do I limit the code to 360 ? How can I transfer it to my code to return to zero when it rotates 360 degrees, that is, as soon as it shows 2048

``````if (counter > 2048)
{
counter = 0;
}
``````

Note that you have to do it in reverse too to allow for the fact that counter may go negative if the encoder is rotated counter clockwise past its zero position

Okay. Thanks