CR servo control using millis

Im trying to get a vex continuous rotation servo to sweep from full speed forward to full speed reverse, by using similar code as sweep example in arduino.
But I have to control many servos and sensors so cant use delays for pwm timing. Which is why Im trying to use millis as replacement (like in Blink without delay example). Here is my sketch:

#include <Servo.h>
Servo testservo;
void setup () {
Serial.begin(9600);
testservo.attach(6); //pin 6 is where the white signal line from vex goes to
}

void loop() {
int pos=40; //arbitrary starting speed in range of 0-180
unsigned long prevtime=0;
int pauseval=20; //pulse low time interval
unsigned long timer=millis();
while (pos <140) {
//stops when 140 is reached, ccw or cw speed dun care atm

if (timer-prevtime>pauseval) {
//only send pulse to servo when set time between pulses reached
prevtime=timer; //reset timing
testservo.write(pos);
pos=pos+1;
Serial.println(pos); //see stuff for debugging

}
}

}

So…what am I missing?

So...what am I missing?

You are missing a description of the problem you are having. You posted some code, but didn't say if the code worked or not. If not, what is happening that you don't want to have happen, or what is not happening that you do want to have happen?

You may want to think about making "prevtime" static or global.

oh sorry, the code uploaded but all I got was spinning in one direction and at constant speed. I was hoping it would start up from spinning one direction, slow to zero speed when pos =90, then speed up in other direction until pos=140. And when I open serial monitor, it doesnt show me the value of pos like I wanted.

You may also want to think about making "pos" either static or global.

And when I open serial monitor, it doesnt show me the value of pos like I wanted

In Roman numerals?

I was hoping it would start up from spinning one direction, slow to zero speed when pos =90, then speed up in other direction until pos=140.

I have no idea what you based this expectation on. Typically, continuous rotation servos are driven using Servo.writeMicroseconds() with values that range from around 500 to around 2200, with the low values turning fastest in one direction and high values turning fastest in the other direction. Somewhere in the middle of the range, the servo holds still. You will, of course, need to experiment to find the maximum, minimum, and stationary values for your servo.

You can use the below code to test your servo. Start with the midpoint value 90 and increase or decrease until the servo stops moving. Once the servo is stopped, then change the values from the stop value one increment at a time until the servo is moving at maximum speed. This will controllable speed range values.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor

#include <WString.h>
String readString = String(100);
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
      Serial.begin(9600);
        myservo.attach(9);
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();
        readString.append(c); }
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      int n;
      n = atoi(readString); //convert string to number
      myservo.write(n);
      readString="";
      } 
   }

Somewhere in the middle of the range, the servo holds still

...if you're lucky. Often, it creeps very slowly in one direction or the other. If this happens, it may be best to "detach" the servo if you really want it to stop.

...if you're lucky. Often, it creeps very slowly in one direction or the other.

I've never used a continuous rotation servo that needed to be controlled this way, but I wonder if the drift is caused by the need to use integers in the writeMicroseconds call.

If you could specify writeMicroseconds(1403.7) instead, when it drifts one way with 1403 and the other way with 1404, would it hold still then?

well my main concern was how to replace delays with millis use in a general way but thought of simple code for that.

To reply to AWOL. I was basing it on the .write() function call description in the servo library. It said that you could control position of a servo with just .write() and 0 to 180 input. So i thought that would carry over to a continuous rotation servo since its just modified servos, with 90 corresponding to full stop instead of 90deg.

here's the text:

"write() Description On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement). "

Also I wrote up this simple fcn to replace delay. Its basically an empty while loop that ends when specified pause time is exceeded, using millis() to measure time.

my question is, would this allow me to do other stuff during the function call or interfere with other code running at the same time? I plan to use it whenever I need a delay but dont want to stop other stuff such as sensors/motors.

void wait(int waitTime) {
unsigned long ref_time=millis();
while (millis() - ref_time < waitTime) {
//just go through loop,
}

}

I would call it for things like this:

for (…) {
servo.write(…)
wait(…)
…}

servo.write simple maps the 0-180 input to a number that servo.writemicroseconds can use, and then it calls servo.writemicroseconds.

So, consider them functionally identical except for the input range.

Below is the code modified for the microsecond values. My continous rotation servos have a no movement band of ~10us, so using microseconds makes it easier to keep the servo in the no movement band.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
//for writeMicroseconds, use a value like 1500

#include <WString.h> //provides easy string handling
String readString = String(100);
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
      Serial.begin(9600);
        myservo.attach(9);
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        readString.append(c); } //adds byte to readString
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      int n;
      n = atoi(readString); //convert string to number
      myservo.writeMicroseconds(n);
      //myservo.write(n);
      readString="";
      } 
   }