reducing 2400p/r optical encoder to 360 degress

Hello,

I’m having trouble converting my 2400p/r optical encoder to 360. Ive managed to get 360 degrees but its not accurate to a actual 1 revolution and dividing myEnc.read() by 6.666666666666667 is also not working as well. I’m having trouble programming the adjustment of the myEnc.read() to skip certain pulses. >:(

/* Encoder Library - Basic Example

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(3, 4);
// avoid using pins with LEDs attached

void setup() {
Serial.begin(9600);
Serial.println(“Basic Encoder Test:”);
}

long oldPosition = -999;

void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
if (myEnc.read() > 359) {myEnc.write(0);}
if (myEnc.read() < 0) {myEnc.write(359);}
Serial.println(newPosition);
}
}

please help thanks in advance

(deleted)

if you are trying to get the angle travelled with each encoder movement, would something like this not work:

compiles, not tested!

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(3, 4);

//encoder resolution = 2400p/r. therefore 1 pulse = 0.15degrees

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = myEnc.read();
long newPosition = oldPosition;

void loop() {
  newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    float new_angle = (abs(newPosition - oldPosition)) * 0.15;
    oldPosition = newPosition;
    Serial.println(newPosition);
    Serial.println(new_angle);
  }

hope that helps

I need it for a motor’s rotor position. I’ve also tried this code without the Encoder.h, but my code does work. I don’t need to utilized both pins. I just need the 360 degrees.

const int pin_A = 3;
const int pin_B = 4;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
int count = 0;

void setup() {
// put your setup code here, to run once:
pinMode(pin_A, INPUT);
pinMode(pin_B, INPUT);
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);

}

void loop() {
// put your main code here, to run repeatedly:
// initialized change in rotor knob
if ((encoder_A) != (encoder_A_prev)) {
encoder_A_prev = encoder_A;
// initialize positive turn
//assuming pins & encoder are high and low and not actual pulse ticks
if (((encoder_A)==HIGH) && ((encoder_B)== LOW)){
encoder_A= encoder_A + 1;
// initialize negative turn
if (((encoder_A)==LOW) && ((encoder_B)== HIGH)){
encoder_A = encoder_A - 1;
if ((encoder_A) < 359) {encoder_A = 0;}
if ((encoder_A) > 0) {encoder_A = 359;}
Serial.print(encoder_A);
}
}
}
}

(deleted)

Hi, Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum". OR http://forum.arduino.cc/index.php/topic,148850.0.html. Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

If you are looking at 2400 counts, is there a reason you need to work in degrees. Why can't you calibrate your motor position to 0 to 2400? Forget about degrees of rotation.

How much precision do you need with your positioning? 2400 counts will give you 360/2400 = 0.15 degrees per count. 6 counts = 6 x 0.15 = 0.9 Degrees 7 counts = 7 x 0.15 = 1.05 Degrees How close to 1 Degree do you want to be.

600 counts = 600 x 0.15 = 90 Degrees 2400 counts = 360 Degrees.

Tom... :)