Arduino robot control function?

Hello guys,
Just to let you know I am trying to build a self balancing two wheeled robot. I am using two pololu 37mm 100:1 12 volt geared motors with encoders. I am also using arduino mega with two cytron 10 amp motor controllers for arduino mega and uno. I got my motors working, my encoders working, but not the motors and encoders to work together. I am using the PJRC library to control my encoders as well. For some reason the motors will keep spinning non stop every time I set my speed and distance variables and will keep on spinning even though the encoder has well beyond passed the distance variables.

Here is how my code works. Function move should control the motors and encoders with it’s parameters. The parameters are LeftMotorSpeed, RightMotorSpeed, LeftMotorTicks, and RightMotorTicks. Left and Right motor speed are how fast the motors are moving on a scale of 0 - 255. Right and left motor ticks are the ticks the encoders should make before stopping and returning the function. Any ideas for where I could be going wrong with this? Thank you.
From: Noah

Noah_s_Fire_Fighting_Robot.ino (2.34 KB)

I am using the PJRC library to control my encoders as well.

How are you controlling an output device?

  bool LeftMotorSet;
  bool RightMotorSet;

  long LeftEncRead, RightEncRead;
  LeftEncRead = MtrEncLeft.read();
  RightEncRead = MtrEncRight.read();
  Serial.print("Encoder Left Set ");
  Serial.print(LeftMotorSet);
  Serial.print(" Encoder Right Set: ");
  Serial.print(RightMotorSet);

Local variables are not initialized. Why are you printing garbage, and then setting LeftMotorSet and RightMotorSet, and expecting the printed garbage to provide any clues?

  if (LeftMotorTicks > 0 && LeftMotorTicks > LeftEncRead || LeftMotorTicks < 0 && LeftMotorTicks < LeftEncRead || LeftMotorTicks != 0 && LeftMotorSpeed != 0)

(You) (might) (need) (some) (parentheses). (Certainly) (couldn’t) (hurt).

Why do you bother assigning new values to LeftMotorSpeed and RightMotorSpeed, after you have used the values input? Doing so does not get any information back to the caller.

Well the variables I am printing are the variables that are crucial to making the robot work. Left / Right MotorSet if those variables are true it should stop the motors from moving if the motor's encoder is reading a value greater or less than depending if MotorTicks is positive or negative than left / right MotorTicks and make the robot closer to returning the function. Left / RightMotorTicks are the ticks each encoder should make before returning the function. The reason I am printing those variables is something is not working with LeftMotorSet and RightMotorSet. They aren't becoming true and are remaining false. Thank you.

Also I am having trouble understanding what you mean by you questioning me assigning new values to left and rightMotorSpeed? What values were you referring to there are a lot of values for left and rightMotorSpeed and they are very crucial? What do you need a better explanation of I am confused and am very sorry? Thank you.

nschreiber0813: For some reason the motors will keep spinning non stop every time I set my speed and distance variables and will keep on spinning even though the encoder has well beyond passed the distance variables.

Hi Noah,

I haven't looked at your code yet but I will soon.

It would help if you linked to the motors you're using. A link to where you found your code would also be useful. If you use the "Insert a link" button in the editor, the links will be clickable and I won't have to copy and paste the URL in order to visit the linked site.

Have you tried switching the encoder pins? If the program is expecting A and B pins from the encoders to be on opposite pins, the motors will just keep spinning as you're seeing.

This is my motor shield and where I found the code for it. These are my pololu motors. Here is my code for encoders. Thank you.

Well the variables I am printing are the variables that are crucial to making the robot work.

But you print the values BEFORE you compute the values. How is THAT meaningful?

They aren’t becoming true and are remaining false.

You do NOT know that.

nschreiber0813: I am using the PJRC library to control my encoders as well.

Noah,

I couldn't compile your code. I don't have "Encoder.h". A link would be helpful.

nschreiber0813: I got my motors working, my encoders working, but not the motors and encoders to work together.

Could you post the code you used to test the encoders and the motors? I think you did. Thanks.

Are you sure the encoder and motors are treating positive directions the same? If you manually turn the encoders the direction the motor spins with positive motion do you get a positive distance from the encoders?

Oh wow I thought arduino computed everything at the same time lol. Thank you I will put them lower in the code.

@Duane The encoder library download is at the PJRC hyperlink I sent you at the top left hand corner of the page. Also the motors I just realized aren't the same if I turn them. That would definitely be a problem. Thank you.

Hey guys I am just letting you know I am still having the same problem. Instead of having one motor move forward continuously now the motor seems to have some sort of delay. Any ideas?

#include <Encoder.h>

Encoder LeftEncoder(18,19);
const int MtrDirLeft = 2;
const int MtrPWMLeft = 3;
Encoder RightEncoder(20,21);
const int MtrDirRight = 4;
const int MtrPWMRight = 5;
bool LeftMotorSet = false;
bool RightMotorSet = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(MtrDirLeft, OUTPUT);
pinMode(MtrPWMLeft, OUTPUT);
pinMode(MtrDirRight, OUTPUT);
pinMode(MtrPWMRight, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
Move(255,0,6000, 0);
delay(1000);
Move(255, 0, -6000, 0);
delay(1000);
Move(0,255,0, 6000);
delay(1000);
Move(0,255, 0, -6000);
delay(1000);
}

void Move(int LeftMotorSpeed, int RightMotorSpeed, int LeftMotorWantedTicks, int RightMotorWantedTicks)
{

long LeftEncRead, RightEncRead;
LeftEncRead = LeftEncoder.read();
RightEncRead = RightEncoder.read();
if ((RightMotorSet == true) && (LeftMotorSet == true))
{
RightEncoder.write(0);
LeftEncoder.write(0);
return;
}
else
{
if (LeftMotorSet == true)
{
LeftMotorSpeed = 0;
LeftMotorWantedTicks = 0;
LeftEncoder.write(0);
}
else
{
if (((LeftMotorWantedTicks < 0) && ((LeftMotorWantedTicks >= LeftEncRead))) || ((LeftMotorWantedTicks > 0) && (LeftMotorWantedTicks <= LeftEncRead)) || (LeftMotorWantedTicks == 0) || (LeftMotorSpeed == 0))
{
LeftMotorSet = true;
}
if (LeftMotorWantedTicks > 0)
{
digitalWrite(MtrDirLeft, HIGH);
analogWrite(MtrPWMLeft, LeftMotorSpeed);
}
else
{
digitalWrite(MtrDirLeft, LOW);
analogWrite(MtrPWMLeft, LeftMotorSpeed);
}
}
if (RightMotorSet == true)
{
RightMotorSpeed = 0;
RightMotorWantedTicks = 0;
RightEncoder.write(0);
}
else
{
if (((RightMotorWantedTicks < 0) && (RightMotorWantedTicks >= RightEncRead)) || ((RightMotorWantedTicks > 0) && (RightMotorWantedTicks <= RightEncRead)) || (RightMotorWantedTicks == 0) || (RightMotorSpeed == 0))
{
RightMotorSet == true;
}
if (RightMotorWantedTicks > 0)
{
digitalWrite(MtrDirRight, HIGH);
analogWrite(MtrPWMRight, RightMotorSpeed);
}
else
{
digitalWrite(MtrDirRight, LOW);
analogWrite(MtrPWMRight, RightMotorSpeed);
}
}
}
}

    if (((RightMotorWantedTicks < 0) && (RightMotorWantedTicks >= RightEncRead)) || ((RightMotorWantedTicks > 0) && (RightMotorWantedTicks <= RightEncRead)) || (RightMotorWantedTicks == 0) || (RightMotorSpeed == 0))

Surely that statement should be broken into nested ifs.

    RightMotorSet == true;

==?

Noah,

I'll try to help you more when I get some free time.

In the meantime find the thre "How to use this forum - please read" and learn to use code tags. Item #7 covers code tags.

You can edit your post and add tags after the fact.

If you quote someone post which includes code tags you can see how they're used.

Code inside code tags.

@Paul
Thank you for your help but what are nested ifs? I’ve never heard of those.

@Duane
Thank you I appreciate all your help and I do hope you get more free time. Thanks!!!