用arduino控制伺服电机


const int servo = 8;
int pos;
void setup()
{
pinMode(servo, OUTPUT);
Serial.begin(9600);
}
void pesudoPwm(int pin, int totalUs, int highUs)
{
digitalWrite(pin, HIGH);
delayMicroseconds(highUs);
digitalWrite(pin, LOW);
delayMicroseconds(totalUs);...详情
const int servo = 8;
int pos;
void setup()
{
pinMode(servo, OUTPUT);
Serial.begin(9600);
}
void pesudoPwm(int pin, int totalUs, int highUs)
{
digitalWrite(pin, HIGH);
delayMicroseconds(highUs);
digitalWrite(pin, LOW);
delayMicroseconds(totalUs);
}
void loop()
{
//pulse period = 20ms
//0.5ms pulse width to -90 grade
//2.5ms pulse width to 90 grade
int val, angle;
for (angle = -90; angle < 90; angle++)
{
val = map(angle, -90, 90, 500, 2500);
pesudoPwm(servo, 201000, val);
delay(15);
}
for (angle = 90; angle > -90; angle--)
{
val = map(angle, -90, 90, 500, 2500);
pesudoPwm(servo, 20
1000, val);
delay(15);
}
}隐藏

标签: arduino视频