How do you control a Dual VNH5019 Arduino shield with a pot?

Hello everyone,
I am working on a ROV(an RC submarine) for a competition coming up and I am hoping i could get some help writing a code where I can adjust the speed and direction of the Dual VNH5019 motor with 2 analog potentiometers. I have been trying to use the demo example as a template then trying to map the speed and direction by the values given by a pot, but i have had no luck yet. Can someone please help me with this dilemma as soon as possible?

Hello.

It should be as easy as calling analogRead() to get the value of the pot, scaling it in some way, and then passing it as an argument to the function that sets the motor speed. Can you post a simple example that you think should work (it should only be a few lines) and tell us what happens?

- Ben

Thank you Ben for your reply. I really thought it would be simple like that but yet i have been struggling for quite a few hours on this, I have tried to troubleshoot this the best that I could, but the only success out of the code i have written is that i was able to get a analog read out on the serial monitor for a motor value and a potentiometer read out, but haven’t been able to write a successful code to create both a positive and negative value for the speed to determine the direction and speed. I am going to be running 2 directional motors out of a joystick hopefully, and so center will be my “Zero” for my motors i hope. the code is below:

#include "DualVNH5019MotorShield.h"
 
DualVNH5019MotorShield md;

void setup()
{
  Serial.begin (9600);                   //initialize serial communications
  md.init();                                  //initiates default pololu shield pins
}

void stopIfFault()
{
  if (md.getM1Fault())
  {
    while(1);
  }
  if (md.getM2Fault())
  {
    while(1);
  }
}

void loop()
{
  { 
   
    int speed1 = analogRead(A0);             //potiometer value
    {
      Serial.println(speed1);             // speed value to be displayed on serial monitor
      delay(20);
    }
    {
    int A = map(speed1, 0, 1023, -400, 400);   //maps motor speed and directions values
    Serial.println(A);
     md.setM1Speed(A);       //Set speed and direction for motor 1. Speed should be between -400 and 400. 400 corresponds to motor current flowing from M1A to M1B. -400 corresponds to motor current flowing from M1B to M1A.
      
     md.setM2Speed(A);      //Set speed and direction for motor 2. Speed should be between -400 and 400. 400 corresponds to motor current flowing from M2A to M2B. -400 corresponds to motor current flowing from M2B to M2A.
  
   stopIfFault();
    if (speed1%200 == 100);
    }
 
      {
    int m1Brake = 0;
    int m2Brake = 0;
    md.setBrakes(m1Brake,   m2Brake);//Set brake for motor 1 and 2.
      }
    
  }
}

Your code could be much simpler, and I suspect that simplifying it will help you figure out what your problem is.

For example, if you just call:

md.setM1Speed(400);

does the motor move? If not, that would be good to know. If so, make it slightly more complicated by adding in the pot:

md.setM1Speed(analogRead(A0));

does the motor speed change as you change the pot?

You should be able to gradually increase complexity in this way until things stop working, which will make it much easier to narrow down where the problem is. So as I said before, please post the simplest code you can that doesn’t do what you expect, and explain exactly what does happen when you run it. Additionally, it would be very helpful if you could post the next simplest code that actually does do what you expect.

- Ben

Thank you very much for your help. I was able to see the flaws in my code and this code is as simple as it needed to be:

#include "DualVNH5019MotorShield.h"
 
DualVNH5019MotorShield md;

void setup()
{
  Serial.begin (9600); //initialize serial communications
  md.init();            //initiates default pololu shield pins
}

void loop()
{
  int A = analogRead(A0);
  int B = analogRead(A1);

  int Speed1 = map(A, 0, 1023, -400,400);
  int Speed2 = map(B, 0, 1023, -400,400);
  
  md.setM1Speed(Speed1);
  md.setM2Speed(Speed2);
}

So it sounds like you have it working now, is that correct? If so, that’s great! Please let me know if you run into any other problems.

- Ben

Yes, its runs very smoothly and gracefully. I’m glad you helped me quit over thinking the code for it. The next main step kind of just for fun was whether i could stack 2 VNH5019 arduino shields easily, or if it takes a lot more effort than what its worth on a tight schedule. Thank you so much for all your troubleshooting help.