I have little confused on sending PWM to motor controller, I am using a Pololu high powered motor driver:
I am slightly confused on setting PWMH to high or a value. I don't want to full forward and would like to test with it to lower the speed. I have a value called val and map it. I am unsure on how to set the forward HIGH and LOW and also be able to set a % of the actual speed to lower it. I wrote but unsure what to do with PWM1, connection wise(I am even unsure why I put it), and would I need to change PWMH from HIGH to an actual value, like PWMval/2 ?
#define PWML 10
#define PWMH 11
#define PWM1 9 //pwm controlled pin
#define DIR1 12 //non PWM pin for direction control
#define SWTCH 8; //switch enable
//second option for motor
void setup()
{
Serial.begin(9600);
pinMode(SWTCH, INPUT);
pinMode(PWML,OUTPUT);
pinMode(PWMH,OUTPUT);
pinMode(PWM1, OUTPUT);
}
void loop()
{
int PWMval = map(val, 0,1023, 0,255);
Serial.print("Speed Value: ");
Serial.println(PWMval);
motorForward(PWMval/2); //half speed to test
delay(100);
}
//stop motor before going in different direction
void stopFunction()
{
analogWrite(PWML, 0);
analogWrite(PWMH, 0);
}
//When DIR is HIGH, current flow OUTA to OUTB; LOW = OUTB to OUTA
void motorForward(int PWMforward)
{
analogWrite(PWM1, PWMforward);
digitalWrite(DIR, LOW);
digitalWrite(PWML, HIGH);
digitalWrite(PWMH, HIGH);
}
You have a variable called val that is never declared or initialized. That code will not even compile, let along do anything useful.
I am using a Pololu high powered motor driver
What do you actually have connected on that driver to what on the Arduino? The only things that need to be connected are GND, PWMH, and DIR.
PWMH should be connected to a PWM pin. DIR should be connected to a non-PWM pin. GND should be connected to ground.
If you connect PWML, it should be connected to a PWM pin, and can be used to control breaking. Initially, I would recommend that you not connect this pin, until you get the other stuff working.
Yes, I understand all that. I only posted part of my code that I had a problem with. I have a variable called 'val' that has a value. I know I don't need to connect PWML but this project might be expanded upon in the future so I will connect it and that is why I implemented it into the code. My main question was how to cut the speed of the motor in half. Instead of setting PWMH to 'HIGH' would I set it to PWMval/2 ?
void setup()
{
Serial.begin(9600);
pinMode(SWTCH, INPUT);
pinMode(PWML,OUTPUT);
pinMode(PWMH,OUTPUT);
pinMode(PWM1, OUTPUT);
}
void loop()
{
int val; // this contains a number but have not posted code.
int PWMval = map(val, 0,1023, 0,255);
Serial.print("Speed Value: ");
Serial.println(PWMval);
motorForward(PWMval/2); //half speed to test
delay(100);
}
void motorForward(int PWMforward)
{
//analogWrite(PWM1, PWMforward);
digitalWrite(DIR, LOW);
digitalWrite(PWML, HIGH);
analogWrite(PWMH, PWMforward);
}
We all can, but you're the one that wants help. People want to see the full sketch so that they can see the context of the bit you are having problems with.
According to the link you posted, you only need to use PWMH and DIR to drive the motor, so give it a try. Set DIR as appropriate and set PWMH to a value between 0 and 255. Depending on the motor, it may need a particular value to actually start moving.
The values in PWM >> 0 to 255 (total 256 values, so 256/2 = 128) here 128 is the value which will give you the working half power of motor, now as you have mapped the input on Analog pin's the value 0 to 1023 half will provide you 128.
dxw00d:
According to the link you posted, you only need to use PWMH and DIR to drive the motor, so give it a try. Set DIR as appropriate and set PWMH to a value between 0 and 255. Depending on the motor, it may need a particular value to actually start moving.
And according to my post, that you might not have read, I mentioned the PWML possibly being used later but this is a non issue right now.
My main question, with no code even needed to be posted, was to lower the motor speed. Could I use a value instead of 'HIGH' when referring to the digital/analog write(PWMH, value). That was my only question. And seems like NI$HANT answered it in a nice manner.