Skip to content

Instantly share code, notes, and snippets.

@SenpaiRar
Last active May 14, 2019 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SenpaiRar/681cdab1d0e1addf6abf058d4a8cfa98 to your computer and use it in GitHub Desktop.
Save SenpaiRar/681cdab1d0e1addf6abf058d4a8cfa98 to your computer and use it in GitHub Desktop.
This is the code for walking.
//Arduino provides a set of prebuilt code to run servos. To bring it into our code, we use this command.
#include <Servo.h>
//A class is a collection of differnet data and functions you can use to make coding more conveinent. In this case, we
//make a class that holds every servo on our robot so we can easily control it
class ServoManager{
public:
//Every servo on the robot is created in our class
Servo FrontRightThigh;
Servo FrontRightKnee;
Servo BackRightThigh;
Servo BackRightKnee;
Servo FrontLeftThigh;
Servo FrontLeftKnee;
Servo BackLeftThigh;
Servo BackLeftKnee;
//We define a function which will be run at the start.
void setup(){
//To start using the servos through the servo library we brought in early, we have to use the
//attach command on servos. The attach command takes in the physical port the Servo's signal wire is attached to.
//We go through every servo, using the attach command passing in the port the servos are attached to
FrontRightThigh.attach(2);
BackRightThigh.attach(3);
FrontLeftThigh.attach(4);
BackLeftThigh.attach(5);
FrontRightKnee.attach(8);
BackRightKnee.attach(9);
FrontLeftKnee.attach(10);
BackLeftKnee.attach(11);
}
//Here we make a function that takes 8 different numbers, representing the 8 different angles of the servos.
//The function then takes those 8 numbers and uses the Servo's built in function, write() which takes in an angle to rotate the servo,
//and uses that command on each of the servo with the corresponding number that was passed in.
//This function is used to change the servo's rotations on the quadruped.
void writeLegs(int FRT, int BRT, int FLT, int BLT,
int FRK, int BRK, int FLK, int BLK ){
FrontRightThigh.write(FRT);
BackRightThigh.write(BRT);
FrontLeftThigh.write(FLT);
BackLeftThigh.write(BLT);
FrontRightKnee.write(FRK);
BackRightKnee.write(BRK);
FrontLeftKnee.write(FLK);
BackLeftKnee.write(BLK);
}
};
//here we make the ServoManager object, calling it Manager
ServoManager Manager;
void setup(){
//Using setup() which is called once, we use the setup() function we defined above, attaching all the servos
Manager.setup();
}
void loop(){
//And this is where the robot starts to move! Using the writeLegs() command we made above, we pass in different sets of numbers
//To change the rotation of each servo. We then wait using the delay command which takes in the number of milliseconds to wait
//We string these together to have the robot move its legs!
Manager.writeLegs(90,90,90,90,90+30,90-35,90-30,90+35);
delay(1000);
Manager.writeLegs(60,90,110,90,90+15,90-35,90-30,90+35);
delay(5000);
Manager.writeLegs(90,60,110,90,90+30,90-65,90-30,90+35);
delay(1000);
Manager.writeLegs(70,60,110,90,90+30,90-65,90-30,90+35);
delay(1000);
Manager.writeLegs(70,60,110,120,90+30,90-65,90-30,90+35);
delay(1000);
Manager.writeLegs(90,90,90,90,90+30,90-35,90-30,90+35);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment