Chopstick Crawler (Servo Motor)

Submitted by Jenton on Sun, 03/17/2013 - 22:11

http://www.youtube.com/watch?v=34Srm3d3fj0

Description:

My chopstick crawler awkwardly ambles its way forward using a chopstick with a weight attached to one end. Not exactly sure why it works, but it does, and I'm leaving it at that.

In my code, I've implemented an on/off switch in the serial monitor. When you turn it on, the servo motor cycles from position 500 to position 1156 in intervals. To create the intervals, I first tried using delay(), but that didn't work. I couldn't figure out how to use the millis and the lastpulse logic, so I just brute forced an interval delay setup by creating a counter, and then incrementing the counter for each loop. Once the counter hit 50,000, the 1156 position would activate. Once the counter hit 100,000, the 500 position would activate and the counter resets to 0.

Items used:

  • 1 pot

  • Arduino Uno

  • Breadboard

  • Lots of cable

  • Servo Motor

  • Chopstick, balsa wood, model magic, tubing, steel wire

 

Code (I didn't do any alterations, this is just the example code given last week):

/*
 * Servo Control Serial
 * modified for TUI March 2013
 * Servo Serial Better
 Jenton Lee
 
 Makes my servo motor crawl!
 
 Type in '1' to turn on, and '0' to turn off
 */
 
int servoPin = 12;      // Control pin for servo motor
 
int pulseWidth = 0;    // Amount to pulse the servo
long lastPulse = 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 serial port
int val2;              // val2 is for my turn on/off logic
long counter = 1;  
/* 
 I couldn't figure out how to properly delay the updateServo() function
 properly, so I just brute forced it using a counter.
 */
int minPulse = 500;   // minimum pulse width
int maxPulse = 2250;  // maximum pulse width
 
void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pulseWidth = minPulse;      // Set the motor position to the minimum
  Serial.begin(9600);         // connect to the serial port
  Serial.println("Servo control program ready");
 
}
 
void loop() {
  val = Serial.read();      // read the serial port
  if (val == '1') {        //my on-off logic
    val2 = val;
  }
  else if (val == '0') {
    val2 = val;
  }
 
  if (val2 == '1') {      // ON code
    if (counter == 50000) { 
      Serial.println("50!");
      pulseWidth = 1156;
      Serial.print("Moving servo to position ");
      Serial.println(pulseWidth,DEC);
      //    updateServo();
 
    }
    else if (counter == 100000) {
      Serial.println("100");
      pulseWidth = 500;
      Serial.print("Moving servo to position ");
      Serial.println(pulseWidth,DEC);
      //   updateServo();
      counter = 0;
    }
    updateServo();
    counter++;
    /*
   the counter starts at 1, and increments by one for each loop. Once it
     hits 50,000, then the servomotor will move to position 1156.
     Once the counter hits 100,000, the servomotor will move to position 500
     and the counter resets to 0
     */
  }
}
 
// called every loop(). 
// uses global variables servoPi, pulsewidth, lastPulse, & refreshTime
void updateServo() {
  // pulse the servo again if rhe refresh time (20 ms) have passed:
  if (millis() - lastPulse >= refreshTime) {
    //Serial.println("Hello!");
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulseWidth);  // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse
  }
}
 
 
 
 
 
 
 
 
 
 

 

0
Your rating: None
Drupal theme by Kiwi Themes.