Hey all,
I am using a pololu dual MC33926 motor driver for the arduino and running a servo at the same time. I also plan on adding a ping distance sensor to another port. At the moment I can get the code to play nice in it's own components but when I put them together the motor just good crazy and the servo does nothing. The motor driver pin out and specs- Pololu - 3.c. Shield Connections: Signals, Power, and Motors. This sais it uses up pins d4, d7, d8, d9, d10, d12, a0, and a1, leaving pin d3, d5, and d11 with as my open pwm pins. I have my servo set to pin d3. Why would the two interfere with each other?
here is the code, maybe the problem is in there.
// Drive and stear functions:
// forward = setM1Speed(+value 0->400)
// Backward = setM1Speed(-value 0->400)
// Left turn = setM2Speed(+value 0->400)
// Right turn = setM2Speed(-value 0->400)
// |value|= magnitude of velocity
// pins d4, d7, d8, d9, d10, d12, a0, and a1 are used by motor driver
// Head Functions
// pins d3, and d5 = servo, ping
//declare variables
//timers
long previousMillis = 0; // will store last time headpause was updated
long headpause = 350; //pause delay for servo head
long previousdrivetimer = 0;
long drivepause = 1500;
int drivestop = 1000;
//libraries
#include <Servo.h> // servo library
#include "DualMC33926MotorShield.h"
//declare variables
DualMC33926MotorShield md; // drive and steer controll
Servo servo; // servo/head
void setup()
{
servo.attach(3); // attach servo to pin 3
md.init(); // motor driver
}
void loop()
{
unsigned long currentMillis = millis(); //start timer
//drive function
{
if(currentMillis - previousdrivetimer == drivepause)
{
md.setM1Speed(100);
}
if(currentMillis - previousdrivetimer == drivestop + drivepause)
{
md.setM1Speed(0);
}
if(currentMillis - previousdrivetimer == drivepause*2 + drivestop)
{
md.setM1Speed(-100);
}
if(currentMillis - previousdrivetimer == drivepause*2 + drivestop*2)
{
previousdrivetimer = currentMillis;
md.setM1Speed(0);
}
}
//head rotating function
{
if(currentMillis - previousMillis == headpause)
{
servo.write(45);
}
if(currentMillis - previousMillis == headpause*2)
{
servo.write(90);
}
if(currentMillis - previousMillis == headpause*3)
{
servo.write(135);
}
if(currentMillis - previousMillis == headpause*4)
{
previousMillis = currentMillis;
servo.write(90);
}
}
}
but when I put them together the motor just good crazy
Technical jargon does not include the phrase "just goes crazy". The motor does something. Try to describe what it actually does. Unless you really do have to have the motor committed to a mental health facility. Which, of course, seems highly unlikely.
Points taken.
The motor goes full front for a good 5 seconds or so and then abruptly reverses for about 2 seconds or so.
how would I go about making the timer without adding the values? It seems to work fine this way.
does the ping sensor not need a pwm pin?
how would I go about making the timer without adding the values? It seems to work fine this way.
Making a timer? Who's making a timer? Since what is actually being added, upon closer inspection, is intervals, the code is fine. It would have been easier to see that with meaningful names for the variables. I prefer to use the term interval in the name of any variable that contains a value that represents an interval. And, I use time (NOT timer) in the name of any variable that hold a time. Time and timers are closely related but they are nowhere near the same thing.
so I've gone into the code I made for driving the motors and just added servo.attach(0); in void setup. That is all it took to make the engine screw up and start going full speed for 5 sec or so then full reverse for 2 sec or so. why would this happen?
another question, when I am trying to devide many variables by another variable,
exe (a+b+c)/d
how do I write that in code? just like that?
here is where I need it
if(currentMillis - previousdrivetimer - drivestop == drivepause*2) can I just do if ((currentMillis - previousdrivetimer - drivestop) / drivepause == drivepause)?
so I've gone into the code I made for driving the motors and just added servo.attach(0); in void setup. That is all it took to make the engine screw up and start going full speed for 5 sec or so then full reverse for 2 sec or so. why would this happen?
PWM uses timers. Servos use timers. Which timers each uses depends on the Arduino, the number of servos involved, and which PWM pin (and PWM mode) is involved.
For the 328-based Arduinos, the first servo instance disables PWM on pins 9 and 10. It's unfortunate that almost every motor driver shield uses those two pins to control the speed of the motors. When will they learn? Using jumpers or switches to select the speed pins would be so much better.
The questions in this thread are spreading across the forum like a weed. I have seen at least 3 threads on the subject and have now replied in all 3 of them. Please can the query be kept in one thread ?