I'm not sure this is in the right place, I believe I have troubleshot the problem down to a programming issue so here goes...
When my line follower motors turned on my servo kept jerking so I disconnected the inputs on the motors, same problem. I commented out the AnalogWrite() PWM commands and it doesn't have an issue anymore. I have pins 9 and 10 using PWM to control two motors and I have a servo attached to pin 6, all share the same power source.
The thing I don't understand is that even if I disconnect the input lines to the motors it still gives the problem, literally nothing is moving on the line follower yet it jerks. If I comment out the PWM commands it stops jerking so it's not related to the motors, is there something internally in the Arduino that could be causing the PWM pins to affect my servo write line?
Can you post the full code you are using? Do you have any current limting resistors between the Arduino Pins and PWM inputs? Do your motors share the same power source as the Arduino? What is/are the power source(s)?
If you disconnect the motors (but still send the PWM) and the servo jitters, then there is probably some issue with the timer function trying to generate PPM and PWM in the same code.
No current limiting resistors between pins and PWM input, I'm actually not sure what you mean by this.
Everything shares the same power source although it's a bit more complicated than that, I have 24 going to my L293D input, 12 going to the arduino, 12 going to a 5V regulator for the L293D, 12 going to a regulator for 6V on the servo and 12V going through another 5V regulator for the line sensors
The power source is two 12V battery supplies in series
@zoomkat
I'm not sure what PPM is but I will definitely check that out.
@AWOL
See Below
No, only PWM on the DC motors.
#include <PololuQTRSensors.h>
#include <Servo.h>
#define NUM_SENSORS 4 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
#define EMITTER_PIN QTR_NO_EMITTER_PIN
// sensors 0 through 3 are connected to analog inputs 0 through 5, respectively
PololuQTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3}
, NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
const int Kp = 185;
const int Kd = 6;
const int M1 = 255;
const int M2 = 255;
int lastError = 0;
const int m1Pin = 9;
const int m2Pin = 10;
const int putterPin = 6;
const int startBtnPin = 8;
const int objSensePin = A4;
const int objThreshold = 80;
int objCount = 0;
int sensorValue;
Servo putter;
const int putterStartPos = 50;
const int putterEndPos = 0;
boolean puttTriggered;
int delayStart;
void setup()
{
delay(500);
// Attach the servo and initialize to start pos
putter.attach(putterPin);
putter.write(putterStartPos);
int i;
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); // turn on LED to indicate we are in calibration mode
for (i = 0; i < 200; i++) // Continue calibrating (400~10 Seconds)
qtra.calibrate(); // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
digitalWrite(13, LOW); // turn off LED to indicate we are through with calibration
// print the calibration minimum values measured when emitters were on
Serial.begin(9600);
for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();
// print the calibration maximum values measured when emitters were on
for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMaximumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
// Wait for user to push a button
pinMode(startBtnPin, INPUT);
while(!digitalRead(startBtnPin)){
}
}
void loop()
{
// read calibrated sensor values and obtain a measure of the line position
// from 0 to 3000, 1500 is center due to the use of four sensors
unsigned int position = qtra.readLine(sensorValues);
// Calculate error based on center
int error = position - 1500;
// Calculate motor speed to correct the direction
int turn = Kp / 1000 * error + Kd * (error - lastError);
int m1Speed = M1 + turn;
int m2Speed = M2 - turn;
// Ensure the motors do not go below 0 or above their set max
if (m1Speed < 0)
m1Speed = 0;
if (m2Speed < 0)
m2Speed = 0;
if (m1Speed > M1)
m1Speed = M1;
if (m2Speed > M2)
m2Speed = M2;
// Set Motor Speeds
//analogWrite(m1Pin, m1Speed);
//analogWrite(m2Pin, m2Speed);
lastError = error;
// CODE FOR CHECKING OBJECT SENSOR
sensorValue = analogRead(objSensePin);
Serial.println(sensorValue);
// If sensor value exceeds threshold, trigger servo
// TODO: Change this to a bandpass later
if (sensorValue > objThreshold)
objCount++;
else
objCount = 0;
if (objCount > 3 && !puttTriggered) {
putter.write(putterEndPos);
delayStart = millis();
puttTriggered = true;
}
else if (puttTriggered && millis() - delayStart > 200) {
puttTriggered = false;
putter.write(putterStartPos);
}
}
So I looked into PPM and it appears this may be part or the whole of the issue.
This is quoted from the Arduino Servo Library description:
"The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."
Just out of sheer coincidence I happen to be using pins 9 and 10 for my DC motors. I will try changing them to different pins later today when I get a chance and I will post any results.
I changed the pins for the motor drivers to two different PWM pins and it works great. Lesson learned, PWM does not work on pins 9 and 10 if using Servo.h.
Or at least as of this post, please refer to Libraries - Arduino Reference for up to date information as this may change.