For this assignment, I used a potentiometer to control a DC motor and a servo simultaneously. The speed of the DC changes as you turn the potentiometer, as does the position of the servomotor. At this point my only goal was to produce some sort of variation in the movement of both motors, but it would be useful to further investigate the exact relationship between the turn of the potentiometer and the change in the motors. As it is now, with both motors powered at the same time, the servo is very jumpy and somewhat unpredictable, where as if I turn off the power to the DC motor, the servo runs more smoothly. I’m not sure what the cause of this is… possibly I have the diode in the wrong place?
int potPin = 0; // Analog in 0 connected to the potentiometer
int potValueT = 0; // value returned from the potentiometer used to control transistor
int potValueS = 0; // value returned from the potentiometer used to control Servomotor
int transistorPin = 9; // connected to the base of the transistor
int servoPin = 2; // Control pin for the servomotor
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 0; // Amount to pulse the servo
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
void setup() {
pinMode(transistorPin, OUTPUT); // set the transistor pin as output:
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulse = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}
void loop() {
potValueT = analogRead(potPin) / 4; // read the potentiometer, convert it to 0 - 255:
analogWrite(9, potValueT); // use that to control the transistor:
potValueS = analogRead(potPin); // read the potentiometer input
pulse = (potValueS * 19) / 10 + minPulse; // convert the analog value
// to a range between minPulse
// and maxPulse.
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
We’d like to see, in addition to the code, the circuit schematic, a photo of the project, and perhaps a video of it working. Just the code is not enough.
Comment by mdgross — February 13, 2008 @ 7:09 am |