I've got an 5V Arduino Nano (it still works despite it's age) hooked up to a DGServo S05NF STD and I'm trying to rotate it 180 and back to 0 after receiving a SerialEvent. The rotation sort of works, but starts to twitch back and forth for a few seconds and then doesn't receive any more SerialEvent calls. If I comment out the servo calls, the builtin LED works fine as an indicator, and SerialEvent works over several messages. Using the USB supplied 5V and tested with Sweep successfully.
Any thoughts would be appreciated:
#include <Servo.h>
const int SERVO_PIN = 9;
Servo servo;
int pos = 0;
String input = "";
bool inputComplete = false;
bool servoRunning = false;
unsigned long pMillis = 0;
const long DELAY = 3000;
void setup() {
Serial.begin(9600);
input.reserve(200);
servo.attach(SERVO_PIN);
}
void loop() {
if (inputComplete) {
if (input.equals("d")) {
initServo();
}
input = "";
inputComplete = false;
}
if (servoRunning && millis() - pMillis > DELAY) {
returnServo();
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n') {
inputComplete = true;
} else {
input += inChar;
}
}
}
void initServo() {
pMillis = millis();
servoRunning = true;
digitalWrite(LED_BUILTIN, HIGH);
servo.write(180);
}
void returnServo() {
servoRunning = false;
digitalWrite(LED_BUILTIN, LOW);
servo.write(0);
}