Last active
August 29, 2015 13:56
-
-
Save nataliefreed/8809361 to your computer and use it in GitHub Desktop.
Robotic arm speed control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Robotic arm speed control demo | |
Natalie Freed, Jan 2014 | |
*/ | |
#include <Servo.h> | |
Servo base, shoulder, elbow, wrist, gripper; | |
void setup() | |
{ | |
base.attach(2); | |
shoulder.attach(3); | |
elbow.attach(4); | |
wrist.attach(10); | |
gripper.attach(11); | |
base.write(90); //start at 90 | |
delay(500); //give it a moment to get there | |
} | |
void loop() | |
{ | |
//use move function! | |
move(base, 0, 500); //take half a second to move to 0 (fast!) | |
move(base, 180, 100000); //take 10 seconds to move to 180 | |
move(base, 180, 2000); //stay at 180 for 2 seconds | |
move(base, 0, 3000); //take 3 seconds to move back to 0 | |
//go back to start of loop and repeat | |
} | |
void move(Servo servo, int targetPos, int moveTime) | |
{ | |
int currentPos = servo.read(); //not actually a sensor, just the last position sent to the servo | |
int totalDistance = targetPos - currentPos; | |
int delayAmount = abs(moveTime / totalDistance); //abs = absolute value | |
int stepLen = totalDistance/abs(totalDistance); | |
for(int i=0;i<abs(totalDistance);i++) | |
{ | |
servo.write(currentPos); | |
delay(delayAmount); | |
currentPos += stepLen; //add step amount to current position | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment