#include <Servo.h>
#define POTPIN A0
#define SERVOPIN 9
#define LED 13
Servo myservo;
String command;
long blinkStartTime;
bool shouldBlink = false;
bool shouldSync = false;
void setup() {
Serial.begin(9600);
Serial.println("Hello Arduino\n");
pinMode(POTPIN, INPUT);
pinMode(LED, OUTPUT);
myservo.attach(SERVOPIN);
}
void loop() {
// process incoming command if there's one
if( Serial.available() ){
command = Serial.readString();
command.trim();
Serial.println(command);
if( command == "RIGHT" ) {
myservo.write(135);
shouldSync = false;
}
if( command == "LEFT" ) {
myservo.write(45);
shouldSync = false;
}
if( command == "SYNC" ) {
shouldSync = true;
}
if( command == "BLINK" ) {
blinkStartTime = millis();
shouldBlink = true;
}
}
// if servo is in sync mode, just sync it
// with the mapped potentiometer value
if(shouldSync){
int syncedDegree = map(analogRead(POTPIN), 0, 1023, 45, 135);
myservo.write(syncedDegree);
}
if ( shouldBlink && (blinkStartTime - millis() < 5000) ){
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
}
else if ( blinkStartTime - millis() > 5000 ){
shouldBlink = false;
}
}