本帖最后由 xmile 于 2015-4-25 15:36 编辑
用蓝牙控制的机械手
程序:
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int r;
int h=0;
int en=4;
int state=7;
int rx=0;
int tx=1;
int l=90;
int f=90;
int u=90;
int delaytime=1000;
void setup() {
pinMode(en,OUTPUT);
pinMode(state,INPUT);
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
servo1.attach(3);
servo1.write(179);
delay(500);
servo2.attach(6);
servo2.write(90);
delay(500);
servo3.attach(9);
servo3.write(90);
delay(500);
servo4.attach(11);
servo4.write(90);
delay(500);
digitalWrite(en,HIGH);
Serial.begin(9600);
}
void loop() {
r=Serial.read();
if(r!=-1){
h=r;
}
switch(h)
{
case 99:
servo1.write(135);
delay(delaytime);
break;
case 110:
servo1.write(179);
delay(delaytime);
break;
case 108:
l+=15;
if(l>164)l=164;
servo2.write(l);
delay(delaytime);
break;
case 114:
l-=15;
if(l<15)l=15;
servo2.write(l);
delay(delaytime);
break;
case 117:
u+=15;
if(u>164)u=164;
servo3.write(u);
delay(delaytime);
break;
case 100:
u-=15;
if(u<15)u=15;
servo3.write(u);
delay(delaytime);
break;
case 102:
f+=15;
if(f>164)f=164;
servo4.write(f);
delay(delaytime);
break;
case 98:
f-=15;
if(f<15)f=15;
servo4.write(f);
delay(delaytime);
break;
case 0:
delay(delaytime);
}
}
程序更新了,不过还是不够好,不知道是左右转动的舵机质量太差还是它负载太重
|