#include <Servo.h>
int angle1,angle2,angle3,angle4,angle5,angle6;
int pos=10;
//定义舵机
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
char val;
//底盘盘靠一个按键控制,一个方向转到底再回转
int key6=0;
/****************** 舵机控制函数 **************************/
//舵机控制函数
void moveservo1()
{
angle1=45;
if(angle1>=40) //判断是否超限
angle1=45;
myservo1.write (angle1);
delay(15);
}
void moveservo11()
{
angle1-=10;
if(angle1<50)
{
angle1-=2;
if(angle1<0) //判断是否低限
angle1=0;
}
myservo1.write (angle1);
delay(15);
//舵机控制函数
void moveservo1()
{
angle1=45;
if(angle1>=40) //判断是否超限
angle1=45;
myservo1.write (angle1);
delay(15);
}
void moveservo11()
{
angle1-=10;
if(angle1<50)
{
angle1-=2;
if(angle1<0) //判断是否低限
angle1=0;
}
myservo1.write (angle1);
delay(15);
}
void moveservo2()
{
angle2+=pos;
if(angle2>180) //判断是否超限
angle2=180;
myservo2.write (angle2);
delay(15);
}
void moveservo22()
{
angle2-=pos;
if(angle2<0) //判断是否低限
angle2=0;
myservo2.write (angle2);
delay(15);
}
void moveservo3()
{
angle3+=pos;
if(angle3>180) //判断是否超限
angle3=180;
myservo3.write (angle3);
delay(15);
}
void moveservo33()
{
angle3-=pos;
if(angle3<0) //判断是否低限
angle3=0;
myservo3.write (angle3);
delay(15);
}
void moveservo4()
{
angle4+=pos;
if(angle4>180) //判断是否超限
angle4=180;
myservo4.write (angle4);
delay(15);
}
void moveservo44()
{
angle4-=pos;
if(angle4<0) //判断是否低限
angle4=0;
myservo4.write (angle4);
delay(15);
}
void moveservo5()
{
angle5+=pos;
if(angle5>180) //判断是否超限
angle5=180;
myservo5.write (angle5);
delay(15);
}
void moveservo55()
{
angle5-=pos;
if(angle5<0) //判断是否低限
angle5=0;
myservo5.write (angle5);
delay(15);
}
void moveservo6()
{
if(0==key6)
{
angle6+=pos;
myservo6.write (angle6);
delay(15);
if(angle6>=180) //判断是否超限
{
key6=1;
angle6=180;
}
}
else
{
angle6-=pos;
myservo6.write (angle6);
delay(15);
if(angle6<=0) //判断是否di限
{
key6=0;
angle6=0;
}
}
}
//爪子的抓取(松开)靠发送a(aa)
//下面四个舵机靠蓝牙按键控制
//底盘盘靠一个按键控制,一个方向转到底再回转
void moveservo2()
{
angle2+=pos;
if(angle2>180) //判断是否超限
angle2=180;
myservo2.write (angle2);
delay(15);
}
void moveservo22()
{
angle2-=pos;
if(angle2<0) //判断是否低限
angle2=0;
myservo2.write (angle2);
delay(15);
}
void moveservo3()
{
angle3+=pos;
if(angle3>180) //判断是否超限
angle3=180;
myservo3.write (angle3);
delay(15);
}
void moveservo33()
{
angle3-=pos;
if(angle3<0) //判断是否低限
angle3=0;
myservo3.write (angle3);
delay(15);
}
void moveservo4()
{
angle4+=pos;
if(angle4>180) //判断是否超限
angle4=180;
myservo4.write (angle4);
delay(15);
}
void moveservo44()
{
angle4-=pos;
if(angle4<0) //判断是否低限
angle4=0;
myservo4.write (angle4);
delay(15);
}
void moveservo5()
{
angle5+=pos;
if(angle5>180) //判断是否超限
angle5=180;
myservo5.write (angle5);
delay(15);
}
void moveservo55()
{
angle5-=pos;
if(angle5<0) //判断是否低限
angle5=0;
myservo5.write (angle5);
delay(15);
}
void moveservo6()
{
if(0==key6)
{
angle6+=pos;
myservo6.write (angle6);
delay(15);
if(angle6>=180) //判断是否超限
{
key6=1;
angle6=180;
}
}
else
{
angle6-=pos;
myservo6.write (angle6);
delay(15);
if(angle6<=0) //判断是否di限
{
key6=0;
angle6=0;
}
}
}
//爪子的抓取(松开)靠发送a(aa)
//下面四个舵机靠蓝牙按键控制
//底盘盘靠一个按键控制,一个方向转到底再回转
/*************************************** 初始化 ***************************************************/
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo1.attach(3); //连接舵机
myservo2.attach(5);
myservo3.attach(6);
myservo4.attach(9);
myservo5.attach(10);
myservo6.attach(11);
angle1=0; //舵机
angle2=90;
angle3=90;
angle4=0;
angle5=90;
angle6=90;
myservo1.write (angle1); //舵机位置初始化
myservo2.write (angle2);
myservo3.write (angle3);
myservo4.write (angle4);
myservo5.write (angle5);
myservo6.write (angle6);
Serial.println("Hello");
}
angle2=90;
angle3=90;
angle4=0;
angle5=90;
angle6=90;
myservo1.write (angle1); //舵机位置初始化
myservo2.write (angle2);
myservo3.write (angle3);
myservo4.write (angle4);
myservo5.write (angle5);
myservo6.write (angle6);
Serial.println("Hello");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available())
{
val=Serial.read();
switch (val)
{
case 'a':
{
moveservo1() ;
Serial.println("x");
break;
}
case'b':
{
moveservo11();
break;
}
case'c':
{
moveservo2();
break;
}
case'd':
{
moveservo22();
break;
}
case'e':
{
moveservo3();
break;
}
case'f':
{
moveservo33();
break;
}
case'g':
{
moveservo4();
break;
}
case'h':
{
moveservo44();
break;
}
case'i':
{
moveservo5();
break;
}
case'j':
{
moveservo55();
break;
}
case'k':
{
moveservo6();
break;
}
}
}
}
// put your main code here, to run repeatedly:
if(Serial.available())
{
val=Serial.read();
switch (val)
{
case 'a':
{
moveservo1() ;
Serial.println("x");
break;
}
case'b':
{
moveservo11();
break;
}
case'c':
{
moveservo2();
break;
}
case'd':
{
moveservo22();
break;
}
case'e':
{
moveservo3();
break;
}
case'f':
{
moveservo33();
break;
}
case'g':
{
moveservo4();
break;
}
case'h':
{
moveservo44();
break;
}
case'i':
{
moveservo5();
break;
}
case'j':
{
moveservo55();
break;
}
case'k':
{
moveservo6();
break;
}
}
}
}
用电池给舵机供电,用蓝牙的键盘实现快速控制。因为买不起舵机控制板了,没办法。接下来会用舵机控制板做!