Summary
This was a bot we used to teach workshops about using an Arduino to control a walking robot.
The robot moved only forward and backward but showed users how to wire up a joystick, power switch, batteries and servos to an Arduino and then program it all to work.
Instructions
//
// Code for a 3D Printed Robot
// Learn more at: http://www.instructables.com/id/3D-Printed-Robot/
// This code is in the Public Domain
//
//add the servo library
include
//Create two servo instances
Servo myservo;
Servo myservo1;
int frontPin = 9;
int backPin = 10;
//Change this numbers until the servos are centered!!!!
//In theory 90 is perfect center, but it is usually higher or lower.
int FrontBalanced = 91;
int BackCentered = 102;
int angle = 25;
//Variables to compensate for the back center of balance when the front shifts
int backRight = BackCentered - 10;
int backLeft = BackCentered + 10;
int xPin = A4;
int yPin = A5;
const int FRONT = 1;
const int BACK = 2;
const int LEFT = 3;
const int RIGHT = 4;
//Setup initial conditions of the Servos and wait 2 seconds
void setup()
{
myservo1.attach(frontPin);
myservo.attach(backPin);
myservo1.write(FrontBalanced);
myservo.write(BackCentered);
// delay(5000);
Serial.begin(9600);
}
void frontLeg(int x) {
myservo1.write(x);
}
void backLeg(int x) {
myservo.write(x);
}
void loop()
{
int x = analogRead(xPin);
int y = analogRead(yPin);
Serial.print("x :");
Serial.println(x);
Serial.print("y :");
Serial.println(y);
int direction = 0;
if (x > 700) {
direction = BACK;
} else if (x < 300) {
direction = FRONT;
}
// if (y > 700) {
// direction = RIGHT;
// } else if (y < 300) {
// direction = LEFT;
// }
switch (direction) {
case FRONT:
front();
break;
case BACK:
back();
break;
case LEFT:
left();
break;
case RIGHT:
right();
break;
default:
straight();
break;
}
delay(1);
}
//Walking function
void front(){
frontLeg(FrontBalanced + angle);
delay(50);
backLeg(BackCentered - angle);
delay(200);
frontLeg(FrontBalanced - angle);
delay(50);
backLeg(BackCentered + angle);
delay(200);
}
void back(){
backLeg(BackCentered - angle);
delay(50);
frontLeg(FrontBalanced + angle);
delay(200);
backLeg(BackCentered + angle);
delay(50);
frontLeg(FrontBalanced - angle);
delay(200);
}
//Walking function
void left(){
frontLeg(FrontBalanced + angle - 5);
delay(100);
backLeg(BackCentered - angle);
delay(300);
frontLeg(FrontBalanced - angle - 5);
delay(100);
backLeg(BackCentered + angle);
delay(300);
}
//Walking function
void right(){
frontLeg(FrontBalanced + angle + 5);
delay(100);
backLeg(BackCentered - angle);
delay(300);
frontLeg(FrontBalanced - angle + 5);
delay(100);
backLeg(BackCentered + angle);
delay(300);
}
void straight() {
frontLeg(FrontBalanced);
backLeg(BackCentered);
}
タグ
モデルソース
