我试着为我的孩子制作肥皂泡机器人。很简单。伺服器上有一个杠杆,可以将环浸泡在肥皂水中并放在风扇前面。问题是当我激活 PWM 伺服器时开始出现异常行为(摇晃、颤抖到 0 位置),尽管它们没有同时运行。似乎在 PWM 期间伺服引脚中有一些噪音。
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define MOTOR_PIN 11
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(4);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(MOTOR_PIN, OUTPUT);
}
void loop() {
analogWrite(MOTOR_PIN,0);
delay(100);
for (pos = 30; pos <= 120; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(10);
}
for (pos = 120; pos >= 30; pos -= 1) {
myservo.write(pos);
delay(10);
}
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(150);
digitalWrite(LED_BUILTIN, HIGH);
delay(150);
analogWrite(MOTOR_PIN, 400);
digitalWrite(LED_BUILTIN, LOW);
delay(150);
digitalWrite(LED_BUILTIN, HIGH);
delay(150);
digitalWrite(LED_BUILTIN, LOW);
delay(150);
delay (3000);
}