用串口看的画,有时候不输出值,等一下再 输出值。导致舵机不能很好的别摇杆控制,有时会卡顿不动,
我用的是arduino min,不知道是什么原因。
代码如下
#include <Servo.h>
Servo myservo1,myservo2; // create servo object to control a servo
//int potpin = 0; // analog pin used to connect the potentiometer
int val,val2; // variable to read the value from the analog pin
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(8);
Serial.begin(38400);
}
void loop()
{
val = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
val2 = analogRead(A1);
delay(50);
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
val2 = map(val2, 0, 1023, 0, 179);
if(0<val<180&&0<val2<180)
{
myservo1.write(val); // sets the servo position according to the scaled value
myservo2.write(val2);
}
// waits for the servo to get there
Serial.print(val);Serial.print("\t");
Serial.println(val2);
}
我用的是arduino min,不知道是什么原因。
代码如下
#include <Servo.h>
Servo myservo1,myservo2; // create servo object to control a servo
//int potpin = 0; // analog pin used to connect the potentiometer
int val,val2; // variable to read the value from the analog pin
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(8);
Serial.begin(38400);
}
void loop()
{
val = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
val2 = analogRead(A1);
delay(50);
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
val2 = map(val2, 0, 1023, 0, 179);
if(0<val<180&&0<val2<180)
{
myservo1.write(val); // sets the servo position according to the scaled value
myservo2.write(val2);
}
// waits for the servo to get there
Serial.print(val);Serial.print("\t");
Serial.println(val2);
}