Week 7: crawler

By John and Ankita

Description 

We wanted to make a boat, with oars propelling the boat forward. We used 2 servo motors, 2 potentiometers to control the oar-like motion and devised our oars from wood sticks.

Video

http://www.youtube.com/watch?v=4S0uAauomWc

Components

-2 servo

-2 pot

-balsa wood

-pins

 

Arduino Code

/*
* modified code from 
 * Servo with Potentiometer control
 * Theory and Practice of Tangible User Interfaces
 * October 11 2007
 */
 
int servoPin1 = 7;      // Control pin for servo motor
int potPin   = 0; // select the input pin for the potentiometer
int servoPin2 = 8;
int potPin2   = 4; 
 
int pulseWidth1 = 0;    // Amount to pulse the servo1
long lastPulse1 = 0;    // the time in millisecs of the last pulse
int pulseWidth2 = 0;    // Amount to pulse the servo2
long lastPulse2 = 0;    // the time in millisecs of the last pulse
 
int refreshTime = 20;  // the time in millisecs needed in between pulses
int val;               // variable used to store data from potentiometer
int val2;    
int minPulse = 500;   // minimum pulse width
 
void setup() {
  pinMode(servoPin1, OUTPUT);  // Set servo pin as an output pin
   pinMode(servoPin2, OUTPUT); 
  pulseWidth1 = minPulse;      // Set the motor position to the minimum
  pulseWidth2 = minPulse;      // Set the motor position to the minimum
  
  Serial.begin(9600);         // connect to the serial port
  Serial.println("servo_serial_better ready");
}
 
void loop() {
  val = analogRead(potPin);    // read the value from the sensor, between 0 - 1024
  val2 = analogRead(potPin2);
  if (val > 0 && val <= 999 ) {
    pulseWidth1 = val*2 + minPulse;  // convert angle to microseconds
    
    Serial.print("moving servo1 to ");
    Serial.println(pulseWidth1,DEC);
    
    
    Serial.print("moving servo2 to ");
    Serial.println(pulseWidth2,DEC);
  
  }
  if (val2 > 0 && val2 <= 999 ) {
    pulseWidth2 = val2*2 + minPulse;  // convert angle to microseconds
    
    Serial.print("moving servo to ");
    Serial.println(pulseWidth2,DEC);
  
  }
  updateServo();   // update servo position
  updateServo2();   // update servo position
  
}
 
// called every loop(). 
void updateServo() {
  // pulse the servo again if the refresh time (20 ms) has passed:
  if (millis() - lastPulse1 >= refreshTime) {
    digitalWrite(servoPin1, HIGH);   // Turn the motor on
    //digitalWrite(servoPin2, HIGH);
    delayMicroseconds(pulseWidth1);  // Length of the pulse sets the motor position
    digitalWrite(servoPin1, LOW);    // Turn the motor off
   // digitalWrite(servoPin2, LOW);  
    lastPulse1 = millis();           // save the time of the last pulse
  }
}
 
// called every loop(). 
 
void updateServo2() {
  // pulse the servo again if the refresh time (20 ms) has passed:
  if (millis() - lastPulse2 >= refreshTime) {
    digitalWrite(servoPin2, HIGH);   // Turn the motor on
    delayMicroseconds(pulseWidth2);  // Length of the pulse sets the motor position
    digitalWrite(servoPin2, LOW);    // Turn the motor off
    lastPulse2 = millis();           // save the time of the last pulse
  }
}
 
 
 
0
Your rating: None