Making Things Interactive

February 12, 2008

Making Motion

Filed under: 5: Making Motion,Assignments,Paul Castellana — paulcastellana @ 2:54 pm

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

  }
}
Advertisements

1 Comment »

  1. 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 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: