Serial Control

Hello, I'm wrote this program recently to control a servo which has been modified for continuous rotation. But I couldn't figure out how to set a time for the servo to rotate. Eg: I want the servo to rotate to 180 degree position which makes the modified servo rotate continuously but how do I set a time limit like 5 seconds for example so that it does not continue to move on forever (i.e it stops rotataing after 5 seconds and waits for another command). I will using this with the roboduino.

#include <Servo.h>
int moveservo;
Servo servo1;

void setup() {
  pinMode(3, OUTPUT);
  Serial.begin(9600);
  Serial.println("Control your servo using serial");
  Serial.println("Use , and . to control your servo!");
  Serial.println();
  servo1.attach(3);
}
  
  
void loop() {
  if (Serial.available() > 0) {
    moveservo = Serial.read();
    
    if (moveservo == 44) { servo1.write(180); };
    if (moveservo == 46) { servo1.write(-180); };
    if (moveservo == 32) { servo1.write(90); };
  }
}

Thnx,
Aditya

if (moveservo == ',') { servo1.write(180); };
    if (moveservo == '.') { servo1.write(-180); };
    if (moveservo == ' ') { servo1.write(90); };

is so much easier to read, don't you think?

Have you thought about using "delay"?
Or, if not, have you worked through the blink without delay example?

void loop() {
  if (Serial.available() > 0) {
    moveservo = Serial.read();
    
    if (moveservo == 44) { servo1.write(180); };
    if (moveservo == 46) { servo1.write(-180); };
    if (moveservo == 32) { servo1.write(90); };
    delay(5000); // delay for 5 seconds
    // add code to stop servo
  }
}

Better if you spell it "delay" to avoid compilation errors.

Corrected :fearful: