Go Down

Topic: 3 brushed motors arduino code (Read 108 times) previous topic - next topic

soyboy97

Hi,

I'm coding the motors for an ROV. We're using 3 motors, each controlled by a 2 input H-bridge. I cant test the motors as I wont be in the workshop for while. I was just wandering if my code looks okay… It compiles but not sure if it will actually work.


 
// Left Motor

int enA = 9;    //PWM
int in1 = 8;
int in2 = 7;

// Right Motor

int enB = 10;   //PWM
int in3 = 5;
int in4 = 4;

// Vertical Motor

int enC = 11;   //PWM
int in5 = 13;
int in6 = 12;

// Joystick Input

int joy1Vert = A0; // Joystick 1 Vertical 
int joy1Horz = A1; // Joystick 1 Horizontal
int joy2Horz = A2; // Joystick 2 Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
int MotorSpeed3 = 0;

//Middle value for Joysticks

static const int Joy1Vert = 511;
static const int Joy1Horz = 511;
static const int Joy2Horz = 511;

void setup()

{

  // Set all the motor control pins to outputs

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(enC, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
   
  // Start with motors disabled and direction forward
 
  // Left Motor
 
  digitalWrite(enA, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
 
  // Right Motor
 
  digitalWrite(enB, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // Vertical Motor
  digitalWrite(enC, LOW);
  digitalWrite(in5, LOW);
  digitalWrite(in6, LOW);
 
}

void loop() {

  // Read the Joystick X and Y positions

  joy1Vert = analogRead(joy1Vert);
  joy1Horz = analogRead(joy1Horz);
  joy2Horz = analogRead(joy2Horz);


  if (joy1Vert < 460)
  {
    // This is Backward

    // Set Left Motor backward

    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);

    // Set Right backward

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW );

    //Determine Motor Speeds

    joy1Vert = joy1Vert - 460; // This produces a negative number
    joy1Vert = joy1Vert * -1;  // Make the number positive

    MotorSpeed1 = map(joy1Vert, 0, 460, 0, 255);
    MotorSpeed2 = map(joy1Vert, 0, 460, 0, 255);

  }
  else if (joy1Vert > 564)
  {
    // This is Forward

    // Set Left Motor Forward

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);

    // Set Right Motor forward

    digitalWrite(in3,LOW);
    digitalWrite(in4,LOW );

    //Determine Motor Speeds

    MotorSpeed1 = map(joy1Vert, 564, 1023, 0, 255);
    MotorSpeed2 = map(joy1Vert, 564, 1023, 0, 255);

  }
  else
  {
    // Left & Right motors Stopped

    MotorSpeed1 = 0;
    MotorSpeed2 = 0;

  }
 
  // STEERING //

  if (joy2Horz < 460)
  {
    // Move Left

    // reverse readings

    joy2Horz = joy2Horz - 460; // This produces a negative number
    joy2Horz = joy2Horz * -1;  // Make the number positive

    // Map the number to a value of 255 maximum

    joy2Horz = map(joy2Horz, 0, 460, 0, 255);
       

    MotorSpeed1 = MotorSpeed1 - joy2Horz;
    MotorSpeed2 = MotorSpeed2 + joy2Horz;

    // Don't exceed range of 0-255

    if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 300;

  }
  else if (joy2Horz > 564)
  {
    // Move Right

    // Map the number to a value of 255 maximum

    joy2Horz = map(joy2Horz, 564, 1023, 0, 255);
       

    MotorSpeed1 = MotorSpeed1 + joy2Horz;
    MotorSpeed2 = MotorSpeed2 - joy2Horz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 > 255)MotorSpeed1 = 300;
    if (MotorSpeed2 < 0)MotorSpeed2 = 0;     

  }

  if (joy1Horz < 460)
  {
    // This is Downwards

    // Set vertical motor downwards

    digitalWrite(in5, LOW);
    digitalWrite(in6, LOW);

    //Determine Motor Speeds

    // As we are going backwards we need to reverse readings

    joy1Horz = joy1Horz - 460; // This produces a negative number
    joy1Horz = joy1Horz * -1;  // Make the number positive

    MotorSpeed3 = map(joy1Horz, 0, 460, 0, 255);


  }
  else if (joy1Horz > 564)
  {
    // This is Upwards

    digitalWrite(in5, HIGH);
    digitalWrite(in6, HIGH);

    //Determine Motor Speeds

    MotorSpeed3 = map(joy1Horz, 564, 1023, 0, 255);

  }
  else
  {
    // This is Stopped

    MotorSpeed3 = 0;

  // prevent buzzing at very low speeds

  if (MotorSpeed1 < 8)MotorSpeed1 = 0;
  if (MotorSpeed2 < 8)MotorSpeed2 = 0;
  if (MotorSpeed3 < 8)MotorSpeed3 = 0;
  }
  // Set the motor speeds

  analogWrite(enA, MotorSpeed1);
  analogWrite(enB, MotorSpeed2);
  analogWrite(enC, MotorSpeed3);

}


Robin2

Testing is a key part of programming - especially when the program interfaces with hardware. Test early and often.

I am too lazy to study code without advice about what happens when it is tested. If it does what you want I won't need to read the code at all :)



Also, to make it easy for people to help you please modify your post and use the code button </>


Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

slipstick

Code: [Select]
  // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 > 255)MotorSpeed1 = 300;

Are you sure 300 doesn't exceed 255? (in several places).
Code: [Select]

  joy1Vert = analogRead(joy1Vert);
  joy1Horz = analogRead(joy1Horz);
  joy2Horz = analogRead(joy2Horz);

Reading the analog value into the variable that holds the pin number?

Have you tested the joysticks? They don't often have a full range of 0-1023 and often don't centre all that close to 511.

Steve

vinceherman

#3
Apr 23, 2019, 10:25 pm Last Edit: Apr 23, 2019, 10:25 pm by vinceherman
Don't try to eat the whole cow at once.
Take smaller bites.

Write a sketch that simply runs a motor, forward for a while.  Stop for a while.  Backwards for a while.
This will confirm that you have the wiring for that motor and driver done correctly and that your understanding of the code to perform these actions is correct.

Write a sketch that simple reads the joystick values and prints them to the serial monitor.  This confirms that you have the wires for the joysticks done correctly.  And that your understanding of the code to read joysticks is correct.  And it also gives you the opportunity to note the min, max and centered values for each joystick.

Then combine them into a single sketch.

Go Up