Hello friends, in the following code, I have connected a motor driver and an encoder, and a motor to Arduino, which starts moving the motor by rotating the encoder and stops when it reaches 600.
In this code, if the encoder rotates to the right, which means it has a positive pulse, the motor rotates to the right, and if it has a negative pulse, it rotates to the left.
Now, I want that when I rotate the encoder to the right and after reaching 600 and stopping the motor, as soon as I rotate the encoder in the opposite direction (to the left), the motor also rotates in the opposite direction (to the left), without having a negative pulse to the left.
Best Regards
Neda
If by REN and LEN you mean Right Enable and Left Enable, this suggests that you may be confusing the pins of your driver. On a L293N, for example, you need 3 pins to control 1 motor. 2 inputs (common digital pins) and 1 enable (PWM).
I tried this code and it worked just like my own code.
It means that when I rotate the encoder, the motor stops at 600 and when I rotate the encoder in the opposite direction, the motor rotates in the same direction as before until it reaches zero and its value becomes negative, and then it moves in the opposite direction.
if (position >= 600) {
analogWrite(LPWM, 0);
analogWrite(RPWM, 0);
position = 0; // <-- **I added this line to your code**
return;
}
When "position" comes to 600, it should be set to zero again. So, the motor should turn off and if you move the encoder CCW "position" should become <0. What are the results of: