Hello again everyone. My code is working for clockwise and counterclockwise, for forwards and backwards. I'm trying to repeat this to turn left and right. I'm trying to keep the speed to turn left and right moderate, so it won't be too fast. I'm not great at coding C++, but I thought I could use similar techniques for the Arduino. I thought I could use analogWrite, and constant for this command. However, the error code keep saying "not declared", and I can't define them. Could someone please help me out with this?
#define mAl 5 // PWM motor pin IN2 motor A
#define mAr 6 // PWM motor pin IN1 motor A
#define TsA 3 // button for clockwise motor A
#define BsA 2 // button for counter clockwise motor A
#define mBl 11 // PWM motor pin IN2 motor B
#define mBr 10 //PWM motor pin IN1 motor B
#define LsB 4 // button for clockwise motor B
#define RsB 7 // button for counter clockwise motor B
#define const int pwm 10,11
#define analogWrite
void setup() {
Serial.begin(9600);
pinMode(pwm, OUTPUT); //we have to set pwm pin as output
pinMode(mAl, OUTPUT);
pinMode(mAr, OUTPUT);
pinMode(TsA, INPUT);
pinMode(BsA, INPUT);
pinMode(mBl, OUTPUT);
pinMode(mBr, OUTPUT);
pinMode(LsB, INPUT);
pinMode(RsB, INPUT);
}
void loop() {
int Pot = analogRead(A0);
Pot = map(Pot, 0, 1023, 0, 255);
Serial.println(Pot);
int top = digitalRead(TsA);
int bottom = digitalRead(BsA);
int left = digitalRead(LsB);
int right = digitalRead(RsB);
if(top == LOW && bottom == HIGH)
{
analogWrite(mAl, Pot);
digitalWrite(mAr,LOW);
}
else if(top == HIGH && bottom == LOW)
{
digitalWrite(mAl, LOW);
analogWrite(mAr, Pot);
}
else{
digitalWrite(mAl, HIGH);
digitalWrite(mAr,HIGH);
}
//For Clock wise motion for motor B, mBl = High , mBr = Low
if (left == LOW && right == HIGH)
{
digitalWrite(mBl, HIGH);
digitalWrite(mBr, LOW);
analogWrite(pwm, 60);
/* setting pwm of the motor to 60 we can change the speed of rotation
by changing pwm input but we are only using arduino so we are using highest
value to drive the motor*/
}
else if(left == HIGH && right == LOW)
{
//For Anti Clock-wise motion - mBl = LOW , mBr = HIGH
digitalWrite(mBl,LOW) ;
digitalWrite(mBr,HIGH) ;
analogWrite(pwm, 60);
//For brake
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
}
//For brake
digitalWrite(mBl,HIGH) ;
digitalWrite(mBr,HIGH) ;
}
the above certainly don't make sense. they redefine "const" as "int pwm10,11" and define "analogWrite" as nothing. also, "pwm", "in1", and "in2" are not defined, hence code doesn't compile
but to support turns, why not drive the inside wheel slower
maybe have just 2 buttons to control direction and one to toggle forward/reverse
#define mAl 5 // PWM motor pin IN2 motor A
#define mAr 6 // PWM motor pin IN1 motor A
#define TsA 3 // button for clockwise motor A
#define BsA 2 // button for counter clockwise motor A
#define mBl 11 // PWM motor pin IN2 motor B
#define mBr 10 // PWM motor pin IN1 motor B
#define LsB 4 // button for clockwise motor B
#define RsB 7 // button for counter clockwise motor B
void setup()
{
Serial.begin(9600);
pinMode(mAl, OUTPUT);
pinMode(mAr, OUTPUT);
pinMode(TsA, INPUT);
pinMode(BsA, INPUT);
pinMode(mBl, OUTPUT);
pinMode(mBr, OUTPUT);
pinMode(LsB, INPUT);
pinMode(RsB, INPUT);
}
void loop()
{
int Pot = analogRead(A0) / 4;
Serial.println(Pot);
int top = digitalRead(TsA);
int bottom = digitalRead(BsA);
int left = digitalRead(LsB);
int right = digitalRead(RsB);
// Motor A:
if (top == LOW && bottom == HIGH)
{
analogWrite(mAl, Pot);
digitalWrite(mAr, LOW);
}
else if (top == HIGH && bottom == LOW)
{
digitalWrite(mAl, LOW);
analogWrite(mAr, Pot);
}
else
{
// Brake
digitalWrite(mAl, HIGH);
digitalWrite(mAr, HIGH);
}
// Motor B
if (left == LOW && right == HIGH)
{
//For Clockwise motion
analogWrite(mBl, Pot);
digitalWrite(mBr, LOW);
}
else if (left == HIGH && right == LOW)
{
//For Anti-Clockwise motion
digitalWrite(mBl, LOW) ;
analogWrite(mBr, Pot);
}
else
{
//For brake
digitalWrite(mBl, HIGH) ;
digitalWrite(mBr, HIGH) ;
}
}
I know, I was just reading off of an Arduino website, and was trying to tweak it.
Well, I'm using 4 buttons sir. I was trying to save money by not using a joystick. I was also aiming to duplicate a gamepad look, like Nintendo . I've seen videos with it.
Exactly, I was trying to to slow the speed of turning down without a potentiometer.
Thanks a bunch as always sir for your help . I was trying to adjust the speed without a potentiometer though. This way, the speed will be constant. Sorry, I should've clarified that . It seems when I turn up the speed, the turns would be too hard. I figured I could always go into the code, and find the best speed to compliment turns, when the speed goes up?
I know, it's gibberish. I just put it there, because I want the turn speed to be at a constant rate.
If you get error messages, please post those too.
Gotcha, sorry about that. I meant to copy paste that as well. My computer was freezing up as I reached out to you all, and it was late.
Right, but I thought the motor A analog write would mess this up, so I was thinking now, do I have to do an analog write for motor B? Thank you so much for reaching out sir .
Right, that's my problem sir . Believe it or not, I'm using Adafruit's DRV8871. I've been reading articles dealing with these drivers, so I was trying to tweak it with this single terminal driver . I'm eyeing some L298N models, makes sense to have one of these, instead of two single boards. I really, really appreciate you alls help. Any book recommendations for coding on Arduino. I have one from, I think the name of the author is Jeffrey Blum.
i can now see why you're driving one of the inputs digitally and the other as a PWM. but when in reverse, shouldn't the PWM timing be reversed (255-n)?
the code i posted needs modification to use with this part
Are you sure you want to reverse the PWM sense? I haven't come across such an idea, so I ask if you are speaking from experience or knowledge of this.
I'll look at the data sheet, yes.
L8R: OK, did. I think section 7.3.1 Bridge Control makes it clear enough that the PWM signal when an input is used as such does not change sense with direction.
Also if the sense were to reverse, since analogWrite() input ranges from 0..255, you probably meant
analogWrite (pinPwm, 255 - spd); // PWM active LOW
correct.
wrote it this way because i originally thought spd might be negative
so if to go forward, and IN1 is LOW and IN2 HIGH, then driving IN2 with PWM makes sense.
but if in reverse, IN1 needs to be HIGH and IN2 LOW, i had thought the PWM signal IN2 should be inverted. but isn't brake when both IN1 and IN2 are HIGH? if so, how to you use a PWM signal with this chip?
When using PWM, it typically works best to switch between driving and braking. For example, to drive a motor forward with 50% of its max RPM, IN1 = 1 and IN2 = 0 during the driving period, and IN1 = 1 and IN2 = 1 during the other period. Alternatively, the coast mode (IN1 = 0, IN2 = 0) for fast current decay is also available. The input pins can be powered before VM is applied.
So… if you follow the recommendation, an inverted PWM signal is used.
One direction
IN1 HIGH
IN2 inverted PWM (LOW driving, HIGH not driving)
and swap the rolls of IN1 and IN2 for the other. Direction.
Note the PWM signal is always invert.
Gonna have to add a dose of I think here, summer time ya know.
More: I find many places use IN1 for direction, and then the PWM does need to change sense depending.
But this also means that in one case you are using the recommended HIGH/HIGH braking mode during the off period, and in the other direction using the other off method LOW/LOW coasting mode.
Which seems like you wouldn't want to do. One mode (braking) or the other (coasting) should be chosen, on what basis I have no idea, and used for both.
Man, thank y'all so much. Y'all are some code wizards . I'm about to try some of these out now. I just woke up from a nap, from staying up until 4 am on this coding .
I've looked at the schematics, but not sure if I saw the actual data sheet. Yeah, that mapping part is confusing to me. Do you care to explain that to me please ? Is it saying the rpm increases from 0 - 1,023? It only maxes out at 255 though.
This seems to complement my code well. I was just going to keep my potentiometer part, to increase and decrease the speed, for forwards and backwards. I wanted to use the constant character to ease the speed of turning .
OK I am with myself on the idea that most DRV8871 code has been uncritically copied from someone who got it to work w/o reading the data sheet. Or with reason to do something asymmetrical. Or not caring.
This, seen everywhere, runs one way between driving and braking, and the other way between driving and coasting.