anyone familiar with high speed constant servo?

Hi there,
I am using a high speed constant servo but I cannot control it properly.

What I want to do is that:

send a '1', let it turn clockwise for like 5 seconds, then stop;
send a '2', let it turn anticlockwise for 5s, then stop;
send a '0', stop it.

Following is my code (not decent enough), It can only stop when I put a '0' in Serial, is there any way for me to stop it after 5s-rotation?

Thanks a lot!

#include <VarSpeedServo.h>


//Look at Servo library. A continuous rotation servo will need to be set
//at approx 90 degrees to stop. 0 will turn one direction and 180 the other.
//There will be some deadband around 90 and the speed will be faster as you
//give it a value farther from 90.

//initiate
VarSpeedServo myservo;
int pos = 0;
int servoPin = 9;
int ledPin = 3;


//conncect to processing
char val;

//replace delay
unsigned long currentTime;
unsigned long previousTime = 0;
unsigned long servoCurrentTime;
unsigned long servoPreviousTime;
int interval = 1000;
int servoInterval = 10;


void setup()
{
  //led
  pinMode(ledPin, OUTPUT);

  //  servo
  myservo.attach(servoPin);
  myservo.write(94, 1, true); //reset position
  Serial.begin(9600);
}

void loop()
{
  currentTime = millis();
  servoCurrentTime = millis();

  //delay for Serical.read();
  if (currentTime - previousTime >= interval  && Serial.available())
  {
    previousTime = currentTime;
    val = Serial.read ();
  }


  if (val == '1') {
    digitalWrite(ledPin, LOW);
    myservo.attach(servoPin);
    for (int pos = 0; pos < 94; pos ++)       // clockwise
    {
      if(servoCurrentTime - servoPreviousTime > servoInterval)
      {
      servoPreviousTime = servoCurrentTime;
      myservo.write(pos, 10, true);
      Serial.println(myservo.read());     
      }
    }
  } else if (val == '2') {
    digitalWrite(ledPin, HIGH);
    myservo.attach(servoPin);
    for (int pos = 180; pos < 190; pos++) //anticlockwise
    {
      if(servoCurrentTime - servoPreviousTime > servoInterval)
      {
      servoPreviousTime = servoCurrentTime;
      myservo.write(pos, 10, true);
      Serial.println(myservo.read());
      }
      
    }
  } else if (val == '0') {
    if(servoCurrentTime - servoPreviousTime > servoInterval)
      {
        servoPreviousTime = servoCurrentTime;
        myservo.write(94, 10, true);
      }
  }
}
[code][code][code]

[/code][/code][/code]

Yes, add a separate test in loop () for wheter 5 seconds has elapsed. The test for Serial.available is
an independent event, you cannot use a single if.

MarkT:
Yes, add a separate test in loop () for wheter 5 seconds has elapsed. The test for Serial.available is
an independent event, you cannot use a single if.

Thanks, i have modified my serial.available sentence, but when I try to stop motor after 3 seconds, it dose not work. I wonder what is wrong with my code this time....

if (val == '1') {
digitalWrite(ledPin, LOW);
myservo.attach(servoPin);
if(//whaaaaaaaaaaat?)
{
for (int pos = 0; pos < 94; pos ++) // clockwise
{
if(servoCurrentTime - servoPreviousTime > servoInterval)
{
servoPreviousTime = servoCurrentTime;
myservo.write(pos, 50, true);
Serial.println(myservo.read());
}
}
}
else if (servoPreviousTime>=3000) //dose not work??????
{
if(servoCurrentTime - servoPreviousTime > servoInterval)
{
servoPreviousTime = servoCurrentTime;
myservo.write(94, 10, true);
}
}

Hi,
Please post all of your code,not just a section.

Before posting PLEASE in the IDE, select TOOLS then Auto-Format.
It will help with the indenting of your loops and if statements.

Then post it, DON'T forget the code tags.

Thanks.. Tom.. :slight_smile:

I just meant change

  //delay for Serical.read();
  if (currentTime - previousTime >= interval  && Serial.available())
  {
    previousTime = currentTime;
    val = Serial.read ();
  }

to

  //delay for Serical.read();
  if (timing && currentTime - start_time >= TIMEOUT)
  {
     // handle timout
     timing = false ;
     motor_stop () ;
  }
  if (Serial.available () > 0)
  {
    val = Serial.read () ;
    // handle char, such as starting motor, setting up timer
  }

so that both conditions are handled independently as they are independent events.

Setting up the timer is just:

  timing = true ;
  start_time = millis () ;