连续舵机两种编程调用方式效果不同呀
连续舵机两种编程调用方式效果不同1.采用舵机用库函数
myservo.write(val); // sets the servo position according to the scaled value
2.采用脉冲高电平
digitalWrite(9, HIGH); // sets the LED on
delayMicroseconds(val); // waits for a second
digitalWrite(9, LOW); // sets the LED off
效果不同 A.用库函数会连续转动,即代码执行完了还在转,估计有定时时钟调用,而使用第二种方式执行过后,舵机停止
B。采用第二种方式,修正微调舵机,让输入为16进制的80、81、82(即~1051ms~)舵机停止,转而采用方式1时,舵机参数要在93度时才停止,这个不同是个怎么回事呢,两种调用有偏差?
代码附后
1.采用舵机用库函数
#include <Servo.h>
Servo myservo;// create servo object to control a servo
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9);// attaches the servo on pin 8 to the servo object
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
// get incoming byte:
val = Serial.read();
Serial.println(val);
myservo.write(val); // sets the servo position according to the scaled value
delay(300); // waits for the servo to get there
}
delay(100); // waits for the servo to get there
}
2.采用脉冲高电平
int potpin = 0;// analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int inByte = 0; // incoming serial byte
void setup()
{
pinMode(9, OUTPUT);
;// attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
Serial.println(inByte);
val = map(inByte, 0, 255, 1000, 2000); // scale it to use it with the servo (value between 0 and 180)
Serial.println(val);
for(int i=0;i<10;i++)
{
{
digitalWrite(9, HIGH); //
delayMicroseconds(val); // waits for a second
digitalWrite(9, LOW); //
delay(20); //
}
}
}
delay(100); // waits for the servo to get there
}
表示同情中.... 这个可以怎么用蓝牙控制啊
页:
[1]