#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1;
int val; // variable to read the value from the analog pin
int val1;
char a[6];
void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
myservo.attach(9);
myservo1.attach(10); // attaches the servo on pin 9 to the servo object
Serial3.begin(9600);
}
void loop()
{
char i;
if(Serial3.available())
{
i=0; //如果有数据输入.....
delay(30); //等待30毫秒让所有输入数据从串口传输完毕.....
if (Serial3.available() <= 6)
{ //如果输入数据位数'<=3'.....
while (Serial3.available())
{ //开始读取数据直到[串口输入缓存被清空]
a[i++] = Serial3.read(); //读取数据到[数组"a"]
}// reads the value of the potentiometer (value between 0 and 1023)
}
for(i=0;i<6;i++)
{
a[i]-='0';
}
val=a[0]*100+a[1]*10+a[2];
val1=a[3]*100+a[4]*10+a[5];
val = constrain(val, 0, 180);
val1 = constrain(val1, 0, 180); // scale it to use it with the servo (value between 0 and 180)
Serial3.println(val);
Serial3.println(val1);
}
myservo.write(val);
myservo1.write(val1);
delay(15);
}
定位舵机的问题:
1、是蓝牙。经过处理后发送到arduino的数据能变换到0-180;这个程序以及解决!
2、是舵机定位,定位靠val,val12个变量来控制角度的,我采用的是write这个指令,但是现在问题来。
调试后舵机抖动基本上是在复位类似的动作状态,根本到不了val变量的位置,请大神帮忙看看问题出在哪里。
另外我是用外部电源供电舵机的,和arduino共地了! |