Making Things Interactive

February 12, 2008

Don Quixote

Filed under: 5: Making Motion,Assignments,Nadeem Haidary — nhaidary @ 4:01 am

For my MakingMotion project I decided to recreate the scene of Don Quixote charging into the windmill. The motor runs a windmill and our hero, Don Quixote, charges on a servo controlled by a potentiometer. I tried to modify the rotational speed of the windmill with a sin wave to give a more natural wind, but the numbers don’t seem to be accurately controlling the motor.


/*
 DON QUIXOTE
 by Nadeem Haidary

 with code from
 "Servo control from an analog input"
 by Tom Igoe
 additions by Carlyn Maw
 */

int transistorPin = 9;              // connected to the base of the transistor
int transistorValue = 180;          // initial value for motor speed (through transistor)
float angle = 0.0;                    // angle for sin-value that modifies windmill speed

int servoPin = 2;                   // Control pin for servo motor
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 = 25;               // the time needed in between pulses

int potentiometerValue = 0;         // the value returned from the potentiometer
int potentiometerPin = 0;           // the analog pin reading the potentiometer

//-------------------------------------------------------------------

void setup() {
  pinMode(transistorPin, OUTPUT);   // Set transistor pin as an output pin
  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() {

  //-----DC MOTOR-----
  angle += 0.001;                                        // Modify how quickly the windspeed changes
  float sinval = sin(angle);                             // Use sin to vary windspeed
  int windSpeed = (int)(255 + (sinval * 255))/2;         // Convert sin to motor speed (0-255)
  transistorValue = windSpeed;                           // Transfer windspeed to motor
  Serial.println(transistorValue);
  analogWrite(9, transistorValue);                       // Turn the DC motor on

  //-----SERVO-----
  potentiometerValue = analogRead(potentiometerPin);     // read the analog input
  pulse = (potentiometerValue * 19) / 10 + minPulse;     // convert the potentiometer value (0-1023)
                                                         // to a range between minPulse and maxPulse. 

  // pulse the servo again if rhe refresh time has passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);                        // Turn the Servo on
    delayMicroseconds(pulse);                            // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);                         // Turn the Servo off
    lastPulse = millis();                                // save the time of the last pulse
  }
}
Advertisements

Leave a Comment »

No comments yet.

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: