Coordinating Rotary Encoder With DC Motor

Hi. I am looking to coordinate a rotary encoder with a dc motor so that the dc motor brakes after 57 encoder rotations, then goes at a slower speed, and finally stops at a new variable “TotalRotations” for when the rotary encoder records that many rotations.

So far, I have this code, and the rotary encoder records the number of rotations in the serial monitor, but besides that, nothing else I want to happen, happens. Would you know how to fix this? Thank you.

int motorPinA = 5;
int motorPinB = 10;
int ena = 1;
int enb = 2;
int totalRotations;
boolean BRAKE = true;

int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {

 pinMode (motorPinA, OUTPUT);
 pinMode (motorPinB, OUTPUT);
 pinMode (ena, OUTPUT);
 pinMode (enb, OUTPUT);
 pinMode (encoder0PinA, INPUT);
 pinMode (encoder0PinB, INPUT);
 Serial.begin (9600);

void loop() {
 n = digitalRead(encoder0PinA);
 if ((encoder0PinALast == LOW) && (n == HIGH)) {
   if (digitalRead(encoder0PinB) == LOW) {
   } else {
   Serial.print (encoder0Pos);
   Serial.print ("/");
 encoder0PinALast = n;
 if (encoder0Pos < 360 * 57) {
   digitalWrite (ena, HIGH);
   digitalWrite (enb, HIGH);
   digitalWrite (motorPinB, HIGH);
   analogWrite (motorPinA, 255);
 } else {
   if (BRAKE) {
     analogWrite(ena, BRAKE);
     analogWrite(enb, BRAKE);
     BRAKE = false;
   } else {

     if (encoder0Pos < totalRotations) {
       digitalWrite (ena, HIGH);
       digitalWrite (enb, HIGH);
       digitalWrite (motorPinB, HIGH);
       analogWrite (motorPinA, 50);

     } else {
       analogWrite(ena, BRAKE);
       analogWrite(enb, BRAKE);

Please edit your post to add code tags. If questions, see the "How to use this forum" post.

What do you want the program to do, and what does it do instead?

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,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.

Thanks.. Tom... :slight_smile:

Done! Thanks!

Hi @jremington. So I have a rotary encoder and dc motor connected to an arduino, and they will control when a car will stop and go/slow down. I would like the motor to run full speed until the rotary encoder records 57 full rotations, and then the motor will brake for a small amount of time, then slow to a speed of 50 of 255 instead of full. The whole time, the rotary encoder should still be recording number of rotations, and when it gets to the variable "totalRotations," the motor should brake and stay braked. Thank you!

How fast is the encoder producing pulses - how many per second? You may need to use an interrupt to make sure you don’t miss any.

Assuming you are keeping count of your position I think your code needs to be something like this pseudo code

if (count < brakePosition) 
else if (count < stopPosition) 

And if that is not quite correct you will probably find it useful to describe the problem like that before trying to write the program.

Planning and Implementing a Program