# help with code with encoders

I have 2 dc linear actuators with encoders and what I'm trying to do is this;

The 2 actuators are suppose to run at the same speed. The problem is they might have differents loads which is going to slow down one of the actuator. So what I'm trying to do is to read the encoders and compare the values. If the value of one of the encoders is higher than the another one, slow down the speed until they are even.

How can I code something like this?

Thanks

Could be as simple as an if statement to compare the two readings or you might need a PID loop. You've been quite vague about the details here and you've given us absolutely nothing to work with in terms of your code or any information on how you are reading the encoders or controlling the actuators so there's not much that anyone can do for you at the moment.

This is what I have so far and not properly working yet.

You can think of this like if you have elevator 1 and elevator 2. The goal is to make both elevator reach the 3rd floor at the same time. If both elevators are empty the speed is going to be 255 for both, no problem. But if elavator 1 has a 500 pounds load is going to run slower. So I need to slow down elevator 2 to match the speed of elevator 1 and viceversa.

Thanks

``````// defines pins numbers for 1 encoder
#define EncoderOutputA 42
#define EncoderOutputB 43
// defines pins numbers for 2 encoder
#define EncoderOutputA2 44
#define EncoderOutputB2 45

//variables for speed
int motor1speed=255;
int motor2speed=255;

// variables for encoder 1
int EncoderCounter = 0;
int EncoderaState;
int EncoderaLastState;

// variables for encoder 2
int EncoderCounter2 = 0;
int EncoderaState2;
int EncoderaLastState2;

void setup() {

Serial.begin(9600);
// Sets the two pins as input for encoder 1
pinMode (EncoderOutputA,INPUT);
pinMode (EncoderOutputB,INPUT);
// Sets the two pins as input for encoder 2
pinMode (EncoderOutputA2,INPUT);
pinMode (EncoderOutputB2,INPUT);

}
void loop() {

// if for encoder 1
if (EncoderaState != EncoderaLastState){
if (digitalRead(EncoderOutputB) != EncoderaState) {
EncoderCounter ++;

}
else {
EncoderCounter--;

}

Serial.print("Encoder 1: ");
Serial.print(int(EncoderCounter));
Serial.print(" ");
Serial.print("Encoder 2: ");
Serial.print(int(EncoderCounter2));

Serial.print(" ");
Serial.print("Balance: ");
Serial.print(int(EncoderCounter2) -(EncoderCounter));

//--------------------------PROBLEM--------------------------------------

// AND NEED TO KEEP motor1speed in the 0-255 range

if(EncoderCounter<EncoderCounter2)  {
motor1speed==motor1speed++;
}
if(EncoderCounter>EncoderCounter2)  {
motor1speed==motor1speed--;
}

//--------------------------PROBLEM--------------------------------------

Serial.print(" ");
Serial.print("motor 1 spd: ");
Serial.print(motor1speed);
Serial.print(" ");
Serial.print("motor 2 spd: ");
Serial.print(motor2speed);

Serial.println(" ");
}
EncoderaLastState = EncoderaState;

// if for encoder 2
if (EncoderaState2 != EncoderaLastState2){
if (digitalRead(EncoderOutputB2) != EncoderaState2) {
EncoderCounter2 ++;

}
else {
EncoderCounter2--;

}

Serial.print("Encoder 1: ");
Serial.print(int(EncoderCounter));
Serial.print(" ");
Serial.print("Encoder 2: ");
Serial.print(int(EncoderCounter2));

Serial.print(" ");
Serial.print("Balance: ");
Serial.print(int(EncoderCounter2) - (EncoderCounter));

//--------------------------PROBLEM--------------------------------------

// AND NEED TO KEEP motor1speed in the 0-255 range

if(EncoderCounter<EncoderCounter2)  {
motor1speed==motor1speed++;
}

if(EncoderCounter>EncoderCounter2)  {
motor1speed==motor1speed--;
}

//--------------------------PROBLEM--------------------------------------

Serial.print(" ");
Serial.print("motor 1 spd: ");
Serial.print(motor1speed);
Serial.print(" ");
Serial.print("motor 2 spd: ");
Serial.print(motor2speed);

Serial.println(" ");
}
EncoderaLastState2 = EncoderaState2;

}
``````
``````motor1speed==motor1speed++;
``````

This is a bug every time. This gives undefined behavior. You can write: "i++" or you can write "i = i + 1" but you must NEVER write i = i++;

What you need is a PID controller. Google "Arduino PID" and expect to spend a day or two reading. It's a pretty thick subject.

I will, thanks

Hi,
What are your 2 actuators, spec/data?

Tom... They are salvage from a machine and don't have any idea about it. The only thing I know is that they are 12v.

Hi,
Does the code you posted work?

It would be best if you only wrote code for one encoder, got that working, then worked on two.

Do they have anything written on them?
How do you know the Encoder connections?
What type of output are the A and B signals.
How are you powering the Encoder?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

This;

``````// defines pins numbers for 1 encoder
#define EncoderOutputA 42
#define EncoderOutputB 43
// defines pins numbers for 2 encoder
#define EncoderOutputA2 44
#define EncoderOutputB2 45
``````

Reads better program wise;

``````// defines pins numbers for 1 encoder
#define Encoder1PinA 42
#define Encoder1PinB 43
// defines pins numbers for 2 encoder
#define Encoder2PinA 44
#define Encoder2PinB 45
``````

Thanks.. Tom.. 