Motor won't run backwards

Hi everyone,

I'm making a line-follower robot, and my right motor won't run backwards in my code (in a test code I made before assembling the robot, it works fine). I'm using a Pololu TB6612FNG motor driver, a Pololu QTR-8A line-sensor, and a Teensy3.6 micro controller.
The left motor works fine and runs backwards.

Here is the test code (that works):

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, LOW);    //this is one control for the left motor: LOW = backwards, HIGH = forwards
  digitalWrite(3, HIGH);   //this is one control for the left motor: LOW = forwards, HIGH = backwards
  digitalWrite(4, HIGH);   //Set low to set motor driver in standby mode
  digitalWrite(5, LOW);   //this is one control for the right motor: LOW = backwards, HIGH = forwards
  digitalWrite(6, HIGH);  //this is one control for the right motor: LOW = forwards, HIGH = backwards
  analogWrite(35, 65);  // set motor speed for left motor
  analogWrite(36, 65);  // set motor speed for right motor
  delay(1000);
}

And this is the code for my line-follower (This is the part of code where the problem should be. The whole code is in attachement (it's in different tabs)):

#define motorSTBY 4
#define lMotorControl1 2
#define lMotorControl2 3
#define rMotorControl1 5
#define rMotorControl2 6
#define lMotor 35
#define rMotor 36

void setup() {
pinMode(lMotorControl1, OUTPUT);
  pinMode(lMotorControl2, OUTPUT);
  pinMode(motorSTBY, OUTPUT);
  pinMode(rMotorControl1, OUTPUT);
  pinMode(lMotorControl2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Display();
  TCS34725_1();
  TCS34725_2();
  Calibration();
 
  baseSpeed = ((MaxMspeed - MinMspeed) / 2) + MinMspeed;
  for (i=0; i<=7; i++) {
    if (analogRead(lineSensor[i]) >= calibrationValue[i]) {
      sensorValue[i] = 1000;
    } else {
      sensorValue[i] = 0;
    }
  }
// calculating the position of the line (a number between 0 and 70, where 35 is the middle):
  pos = (0 * sensorValue[0] + 10 * sensorValue[1] + 20 * sensorValue[2] + 30 * sensorValue[3] + 40 * sensorValue[4] + 50 * sensorValue[5] + 60 * sensorValue[6] + 70 * sensorValue[7]) / (sensorValue[0] + sensorValue[1] + sensorValue[2] + sensorValue[3] + sensorValue[4] + sensorValue[5] + sensorValue[6] + sensorValue[7]);
  
// keep outputting 70 when the robot lose the line
  if (pos == 0 && error > 0) {
    pos = 70;
  }
  error = pos - 35;
  correction = Kp * error + Kd * (error - lastError);

  MspeedL = baseSpeed - correction;
  MspeedR = baseSpeed + correction;

  if (MspeedL > MaxMspeed) {
    MspeedL = MaxMspeed;
  }
  if (MspeedL < MinMspeed) {
    MspeedL -= 2 * MinMspeed;
    digitalWrite(lMotorControl1, LOW);
    digitalWrite(lMotorControl2, HIGH);
  } else {
    digitalWrite(lMotorControl1, HIGH);
    digitalWrite(lMotorControl2, LOW);
  }
  if (MspeedR > MaxMspeed) {
    MspeedR = MaxMspeed;
  }
  if (MspeedR < MinMspeed) {
    MspeedR -= 2 * MinMspeed;
    Serial.println(MspeedR);
    Serial.println(abs(MspeedR));
    digitalWrite(rMotorControl1, LOW);
    digitalWrite(rMotorControl2, HIGH);
  } else {
    digitalWrite(rMotorControl1, HIGH);
    digitalWrite(rMotorControl2, LOW);
  }

  analogWrite(lMotor, abs(MspeedL));
  analogWrite(rMotor, abs(MspeedR));
  
  if (b_start_stop == 1) {
    digitalWrite(motorSTBY, HIGH);
//    Serial.println("Motors running");
  } else {
    digitalWrite(motorSTBY, LOW);
//    Serial.println("Motors off");
  }
}

Any help would be appreciated.
Thanks in advance.

a_GLOBAL.ino (4.44 KB)

b_SETUP.ino (1.89 KB)

c_LOOP.ino (2.5 KB)

d_NEXTION.ino (7.22 KB)

e_CALIBRATION.ino (2.84 KB)

f_TCS34725.ino (4.92 KB)

Double check the correctness of the wiring, and all connections for continuity. Swap the two motors.

swapped the motors and same result: the (now left) motor won't run backwards

In your original post, you stated

in a test code I made before assembling the robot, it works fine

This might lead an intelligent person to suspect that robot assembly could be a problem.

Good luck with your project!

I think you didn't understood me well.
The test code that I made before assembling my robot, works also now when my robot is assembled.
Maybe that an intelligent person (apparently you are) would read my post 2 times before saying something like that, because I used the present and not the past.

I think you didn't understood me well.

Evidently, English is not your native language. There are sections for other languages.

If you want informed help from this forum, it is important to communicate your problem clearly.

I'm sorry for you that only 5.52% from the world population (2007) a native English speaker is. But I don't think that this is the point of the conversation and this forum.
My excuses if it wasn't clear what I mean't.

Anton_H:
The test code that I made before assembling my robot, works also now when my robot is assembled.

Then post the test code so we can compare it with the problem code.

...R

the test code is in my first post

If the test code really runs correctly now then the obvious conclusion is that your real code is never getting to a point where it is trying to run the Right motor in reverse. Can you put some serial prints in to see if it ever gets to that point?

Steve

that's already in my code (see first post), and it gets there

Having looked at the test code, I am now confused about all the other code. Is the code you included in your Original Post a complete program that illustrates the problem, or is it one piece that is intended to work with all the supplementary .ino files that you have attached?

If it is the latter then I'm afraid I am too lazy to try making sense of how all that stuff interacts to find a problem.

...R

It's the part of the code where the problem should be, in other words everything to do with the motors

Anton_H:
It's the part of the code where the problem should be, in other words everything to do with the motors

That does not answer my question in Reply #11. And without an answer I don't know how to help you.

...R

But if your test code still works but your "real" code doesn't then it seems likely that it's never getting into the part that should run the right motor in reverse. Why not add a few serial prints to check if the code ever actually goes to where you think it should be going?

Steve

if you looked at my code, you can see that there are serial prints where the motor should run backwards and I said that I see the serial prints in my serial monitor.

I divided my code in different tabs, and the Arduino IDE saves the different tabs in different files. It executes the different tabs in alphabetical order, so it firs runs a_Global (where I define all my variables), then b_Setup (which is the setup), then c_loop (which is the loop) and then 3 tabs with functions that I made to make my code a little clearer.
Because the code is very long I posted in my first post the parts of code that matters for my problem appart.

Anton_H:
I divided my code in different tabs, and the Arduino IDE saves the different tabs in different files. It executes the different tabs in alphabetical order, so it firs runs a_Global (where I define all my variables), then b_Setup (which is the setup), then c_loop (which is the loop) and then 3 tabs with functions that I made to make my code a little clearer.
Because the code is very long I posted in my first post the parts of code that matters for my problem appart.

I believe that means that the code in your Original Post is not a complete program and it relies on the code in the attached files.

It is a fairly good bet that if the problem is in the place where you think it is then you would have found it by now.

And, as I said in Reply #11, I am not prepared to hunt for a needle in such a large haystack. Dividing up your code is very sensible but when you look at all the code together there is awful lot of it.

I don't understand how you could have gone straight from the simple working demo to all the code in the non-working version in a single jump without testing at lots of different intermediate stages. Can't you go back to the most recent working version?

...R

I wrote my code step by step, beginning with the function to make my screen working e.g.
When that was all done and worked, I began with the real thing: line-following. Everything about that is in c_Loop (with the variables in a_Global). So the most recent working version is without line following.

Have you measured the voltages that reach the motors? Is it completely zero at the moment you expect the motor to run and it doesn't?

Anton_H:
So the most recent working version is without line following.

In that case, if it was my problem I would start over with a working line-following program and add the other parts step by step until I found the part that prevents the line-following from working.

I can't think of any quicker or more reliable way to get to the root of the problem.

...R