Hello All,
Background and equipment:
I need some help to determine what is causing my HiTech hobby servo to become jerky when I turn on a small 9v DC motor. I am using a arduino Fio v3 to send Serial data about the position of two pots and a switch. One pot controls the position of the servo and the other pot controls the speed of the DC motor. The switch controls whether or not the motor is on. On the receiving end I am using adafruits v1 motor shield with an Arduino uno. I am using two xbee s1 radios to transmit the data. Power source for the receiving arduino uno is a tekkeon power pack. The battery for the motor shield is a wimpy 9v batt. The power sources will be swaped, but haven't yet, due to lack of easy connectors.
Observation:
When the dc motor switch is low (DC motor OFF) the servo responds accurately without twitching. As soon as I turn on the DC motor the servo begins to twitch back and forth. I can still roughly control the servo but it jumps back and forth around the position it should be at.
Below is my code:
Transmitter:
//Transmitter: Fio V3 with Xbee S1
int potpin = 0; // analog pin used to connect the potentiometer
int intValue = 0; // an integer valuse (16 bits)
int speedpot = 1; //Potentiometer pin for speed
int speedval = 0; //value to store speed
int direcpin = 15;
int direcstate = 0;
void setup()
{
//myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(direcpin, INPUT);
Serial.begin(38400); // Why so slow? Cause I like it.
Serial1.begin(38400); //Open serial port, sets data rate to 9600 bps
}
void loop()
{
Serial1.print('H'); //send a header character
//Servo Control code
intValue = map(analogRead(potpin), 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
Serial1.write(intValue / 256); //Send the high byte
Serial1.write(intValue % 256); //send the low byte
Serial.println(intValue); //show me the value on my computer
//DC motor control
speedval = analogRead(speedpot) / 4; //scalled to 0-255
Serial1.write(speedval / 256); //Send the high byte
Serial1.write(speedval % 256); //Send the low byte
Serial.println(speedval);
//DC motor direction control
direcstate = digitalRead(direcpin);
Serial.println(direcstate);
if (direcstate == HIGH)
{
Serial1.write(2);
Serial.println("High");
}
else
{
Serial1.write(1);
Serial.println("Low");
Serial.write(1);
}
delay(250); // waits for the servo to get there
}
Receiver:
//Reciever: Arduino Uno with Xbee S1
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
boolean flag = false;
//Servo variables
int value1 = 0; //value to hold the servo postion once assembled
int value2 = 0;
int tvalue = 0;
//Motor Variables
int speed1 = 0;
int speed2 = 0;
int dc_speed = 0;
int dc_direc = 0;
//Long without DELAY()
long previousmillis = 0;
int interval = 250;
void setup()
{
myservo.attach(10); // attaches the servo on pin 9 to the servo object
Serial.begin(38400); //Open serial port, sets data rate to 9600 bps
pinMode(7, OUTPUT); // LED showing me something is going on when the servo stops moving
}
void loop()
{
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval)
{
previousmillis = currentmillis;
if(Serial.read() == 'H') // is this the header
{
//Servo control
value1 = Serial.read(); //read the most sig fig
value2 = Serial.read(); //read the least sig fig
tvalue = value1 * 256 + value2; //assemble the number
tvalue = constrain(tvalue, 17, 114); //min and max travel positions learned by trial and err..destruction
myservo.write(tvalue); // sets the servo position according to the scaled pos value
//Motor Control
speed1 = Serial.read(); //read the most sig fig
speed2 = Serial.read(); //read the least sig fig
dc_speed = speed1 * 256 + speed2; //assemble the number
dc_speed = constrain(dc_speed, 40, 255);
dc_direc = Serial.read();
if (dc_direc == 2)
{
motor.run(FORWARD);
}
else
{
motor.run(RELEASE);
}
motor.setSpeed(dc_speed);
}
//delay(250); // chill out for a bit (REMOVED B/C OF ILL AFFECTS OF MOTOR Pausing)
Serial.flush();
}
//Show me that I am not stuck in the while loop
if (flag == false)
{
digitalWrite(7, HIGH);
flag = true;
}
else
{
digitalWrite(7, LOW);
flag = false;
}
}
At the moment I can't think of anything that would be causing the jerky motions of the servo only when the dc motor is on.
P.S. Thanks to PaulS for his info on Can't send integers correctly over Serial - Interfacing - Arduino Forum I wouldn't have made it this far without this forum and posts like his, and AWOL.