|
本帖最后由 arduino甲 于 2015-12-15 18:05 编辑
新人:arduino识别哪个指令给哪个舵机(目前是两个)a是1号舵机,结果输入a1时,结果有时候正常有时候不正常,求帮助
int angle1;
int servo1=9;
int pulsewidth1;
void servopulse1(int servo1,int angle1)
{
pulsewidth1=(angle1*11)+500;
digitalWrite(servo1,HIGH);//将舵机接口电平至高
delayMicroseconds(pulsewidth1);//延时脉宽值的微秒数
digitalWrite(servo1,LOW);//将舵机接口电平至低
delay(20-pulsewidth1/1000);
}
void setup(){
Serial.begin(9600);
Serial.println("Ready");
pinMode(servo1,OUTPUT);
}
void loop(){
char ch=Serial.read();
switch(ch){
case 'a':
int val1=Serial.read();
val1=val1-'0';
val1=val1*(180/9);
Serial.print("servo1 is on,");
Serial.print("moving servo1 to");
Serial.println(val1,DEC);
for(int i=0;i<=30;i++){
servopulse1(servo1,val1);}
break;
}
}
|
|