School Project Help- Continuous Rotation Servos

HELP!! :frowning:

I have a CR servo attached to pin 2 on my Arduino Mega 2560. What I want to do is when I press 'a' on the laptop keyboard the Servo would spin in the forward direction for 5 seconds then come to a stop. Also when I press 'b' it would spin in the backward direction for 5 seconds then come to a stop. I am a total newbie to Arduino. Can it be done? Can anybody help me with the code please????

Any help at all would be much appreciated!!!!



Post your code you have sofar, I can't see it from here.

Can it be done?

People have gone to the moon and back in the seventies with a less capable computer so, yes it can be done.

(the bigger the "nasa" computer got the less far they came or is that a coincidence ?)

@ravrichard0: Have you done any research yourself. There are dozens of threads on the forum for controlling servos with serial input. There are links in the Playground. There are example sketches provided in the IDE for servo control, and serial input. As this is a school project, I doubt your teachers want someone else to do the work for you.

Yes I have done Research on my own. So far what I can do is just control the servo based on using ‘a’ to make it spin forward and ‘b’ to make it spin backwards. With no time limit.
This is my code so far,

#include <Servo.h>

int minPulse1 = 1170;
int maxPulse1 = 1770;
int turnRate1 = 75;

int centerServo1;
servo servo1;

void setup() {
centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
Serial.println(“Arduino Serial Servo Control”);

void loop() {
if (Serial.available() > 0) {
int data =;
case ‘a’ : pulseWidth1 = pulseWidth1 + turnRate1; break;
case ‘b’ : pulseWidth1 = pulseWidth1 - turnRate1; break ;
case ‘c’ : pulseWidth1 = pulseWidth1 = centerServo1; break;

if (pulseWidth1 > maxPulse1) { pulseWidth1 = maxPulse1; }
if (pulseWidth1 < minPulse1) { pulseWidth1 = minPulse1; }


If you'd posted that to start with, we could all have saved some time. Try to put your code in code tags though. Personally, I prefer to see one statement per line too, but as I said, that's me.

You have two options. One is easy, and one is harder but better in the long term.

EASY: Put a delay(5000); statement after your Servo.write, then follow it with a Servo.write to stop the rotation.
BETTER: Study the 'blink without delay' sketch, and learn how to initiate events based on elapsed time.

Thank You Sir!!! :slight_smile:

I would explore the 2 options you gave. If I have any problem I would post back!

Thank You again. :slight_smile:

Some code I’ve used with my continous rotation servos setting rotation direction via characters sent from the serial monitor. If you want to send characters straight from the pc keyboard to the pc serial port, you will need to find/write an application to do that function. Hyprttrtminal might be an option.

// zoomkat 11-14-11 serial servo test
// type servo position 1f, 1r, xx, etc. in serial monitor and enter
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  myservo1.writeMicroseconds(1500); //set initial servo position if desired
  myservo2.writeMicroseconds(1500); //set initial servo position if desired
  myservo1.attach(6);  //the pin for the servo control 
  Serial.println("servo-test-22"); // so I can keep track of what is loaded

void loop() {
  while (Serial.available()) {
    char c =;  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  // allow buffer to fill with next character

if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    if (readString == "1f") myservo1.writeMicroseconds(2000);
    if (readString == "1r") myservo1.writeMicroseconds(1000);
    if (readString == "1x") myservo1.writeMicroseconds(1500);    
    if (readString == "2f") myservo2.writeMicroseconds(2000);
    if (readString == "2r") myservo2.writeMicroseconds(1000);
    if (readString == "2x") myservo2.writeMicroseconds(1500);    

    if (readString == "xx"){
    if (readString == "ff"){
    if (readString == "rr"){
    readString=""; //empty for next input

Thank You Very Much ZOOMKAT!! :slight_smile:

I will give our code a look in a while and post back my results.

I am using PUTTY as my serial port application.

You seem to be the god of Servos and Arduino.