Description-
I have made a whole container for my arduino and servo. The servo arm has a pencil eraser attached at the end to give it traction as it reaches out of the can and pushes. The original idea was to get the servo to flip the can and while the can was flipping, the servo would reset itself and be ready to push again when it rotated fully. This did not happen because I have stuffed the can too full. I included the arduino unit in there as well.
In future versions of this, I would not include the arduino unit in the can to save on weight, making it easier for the servo to flip the whole unit.
As is, the unit will inch along slowly as the servo moves forward and back because of an imbalance in the whole unit. This is certainly no winner of a race though.
Code-
//For some reason that I don't yet understand, I can't seem to directly modify the pulse width
//I cannot replicate the full range of the servo that was possible in the "Servo Control Serial" sketch.
// This code is a modified version of the "servo control serial" sketch. The loop calls 2 functions
// separated by a delay, each of the functions will move the servo up or down. The loop makes
// the servo alternate up and down by the delay specified thus 'pushing' and 'reseting' with the servo.
/*
* Servo Control Serial
* modified for TUI October 2007
* Servo Serial Better
* -------------------
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com>
* http://todbot.com/
*
* adapted from "http://itp.nyu.edu/physcomp/Labs/Servo"
*/
int servoPin = 7; // 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 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 up(){
pulseWidth =500;
updateServo();
}
void down(){
pulseWidth =1375;
updateServo();
}
void loop() {
up();
delay(500);
down();
delay(500);
}
// 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) {
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
}
}
- Login to post comments