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]