Make a Servo Turn for a Specific Time

Hi Forum,
I am using Servos to make a two wheeled robot that should go forward for 5 seconds, then turn 180° and go that way for 5 seconds, then turn back around and so on. I am using a Tinkerkit shield to control the servos. This is what I have so far:

#include <TinkerKit.h>
#include <Servo.h> 
#include <Time.h>

//constants
Servo motorR;
Servo motorL;
Servo mount;
float duration;
const int servoPin = 9;




//constants to make servos move forward at full speed 

#define R_FullSpeed_Forward 180;

#define L_FullSpeed_Forward 180;  

//consntats to make servos move backward at full speed 

#define R_FullSpeed_Backward 0;
#define L_FullSpeed_Backward 0;

//constants to make servos stop
#define R_Stop 90;
#define L_Stop 90;



//setup


void moveForward(){
  motorL.write(L_FullSpeed_Forward);
  motorR.write(R_FullSpeed_Forward);
 
}

void turnAround(){
  motorL.write(L_FullSpeed_Backward);
  motorR.write(R_FullSpeed_Forward);

}

void stop(){
  motorL.write(L_Stop);	
  motorR.write(R_Stop);
}





//setup sets the servos up
void setup(){
  Serial.begin(9600);
  motorL.attach(O0);
  motorR.attach(O1);
}

void loop(){

		moveForward();
                turnAround();
                moveForward();
 
  
}

How do I add a clock/time/counter into this. Please respond quickly this is due soon!!! :fearful:

How do I add a clock/time/counter into this. Please respond quickly this is due soon!!!!! :fearful:

Well, your project probably will not work they way you think/want. Does your code do anything the way you want? Using time for positioning is often inaccurate for bot movement. How will you know when your bot has rotated 180 deg?

For timing look at how to use millis() in several things at a time.

However, as @zoomkat says that may not be sufficient.

...R

@zoomkat Sorry, I forgot to specify that I am using a Continuos Servo, so I don't really care if it gets to a certain angle I just want it to go for a little bit. It doesn't have to be exactly 5 seconds either, so if there's anyway to ensure it moves for a short period of time I'm fine. As for the 180, I heard using .write with a continuous servo sets a speed and 180 is full speed forward. Is this wrong? Thanks for all your help, ma7730

below is some test code you can use with your servos to evaluate various speed commands for the servos. Bottom is simple code for continuous rotation servos on opposite sides of a bot Also note that continuous rotation servos may need to be calibrated for their neutral command position.

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a 
// to decrease) and enter to change servo position 
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position 
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

void loop()
{
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    if(readString.indexOf('x') >0) { 
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos); 
    }
  }
  readString=""; //empty for next input
}
//zoomkat 10-09-14 serial servo test
//type servo position 0 to 180 in serial monitor
//opposite side servos rotate together

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

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);
  myservo2.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    myservo1.write(n);
    myservo2.write(180-n); //turns opposide servo in desired direction
    readString="";
  } 
}

I don't think zk was talking about the angle of the servo, but the direction of the robot.

I think he meant, how will you know that when you "hang a u" so to speak, that you will have turned exactly 180 and be facing back to where you came from? It will take some serious trial and error to get that timing right, by which I mean the spinning of the wheels in opposite directions to get the robot to turn on its axis. It's very likely to depend on the floor surface.

You're right about the control of a continuous servo, in that 90 is stopped (well, maybe 88 or 92....), and 0 and 180 are full speed in opposite directions.

Hi....

void moveForward(){ motorL.write(L_FullSpeed_Forward); motorR.write(R_FullSpeed_Forward); This would do a turn or spin. }

Will this not drive one servo forward, but the other backward, as it's on the other side of the chassis?? Only did one servo bot and did'ent find it that good, but like DC motors one goes CW and the other as to go CCW, so both move forward...

Are you controlling both servos from pin 9? you can't do that if you want to control each wheel seperately!! and you need to do that...

Just noticed this: const int servoPin = 9;

And yet you have this... How many servos and on what pins..

//setup sets the servos up
void setup(){
  Serial.begin(9600);
  motorL.attach(O0);
  motorR.attach(O1);

Not a good idea to use pins 0 and 1, they are used for serial comms and programing, USB, etc. move them up a bit.. Let us see a schematic or diagram, etc. See my bots here: www.melsaunders.com

Hope it helps, Regards.

Mel.

@CactusFace Thanks for your response. I'm actually using a TinkerKit shield so those aren't pins 1 and two but actually special pins for TK stuff on the shield. The const int servoPin = 9; is something I left there from an example by accident. Thanks for telling me that both at 180 would make them spin. I didn't think about that. @JimboZA I'm a little confused. I though the 180 was full speed. It doesn't matter to me f the wheel makes a full circle or less or more. I just want it to go for a bit. Also, what does "hang a u" mean? Thank you so much. @Zoomkat Thank you so much for the example code, I'm trying it now. Thank you all for your kindness and generosity, ma7730

Sorry “hang a u” is bad slang for “make a u-turn”, ie turn round and go back where you came.

On a continuous servo, 180 degrees (or more correctly, a 2000us pulse) is full speed turning one direction. 0 degrees (a 1000us pulse) is also full speed, but turning the other way. 90 degrees (1500us pulse) is stop.

But with a continuous servo you have no control over how far the wheel turns: you send it 2000us and it starts spinning; send it 1000us and it stops. But you won’t control how many turns it did (and therefore how far the robot moved) in the meantime.

On a continuous servo, 180 degrees (or more correctly, a 2000us pulse) is full speed turning one direction. 0 degrees (a 1000us pulse) is also full speed, but turning the other way. 90 degrees (1500us pulse) is stop.

Just for info, with the DIY continuous rotation servos I've made with the servo pot adjusted for no motion at 1500us, the full rotation speeds are at ~1400us and ~1600us. Also the normal servos I've tinkered with only rotate ~90 deg between 1000us and 2000us. As usual, YMMV!

For the underlying question I'd suggest learning about programming state machines, since this is the fundamental technique used for controlling stuff without using delay().

First off thank you everyone for your help. I really appreciate it. Because the exact time the servo goes doesn't concern me can I just say motorR.write(R_FullSpeed_Forward); delay(5000); //whatever I to do want next won't this set the speed to full speed and go for 5 seconds until whatever I do next? Also some of these questions might sound weird because I don't have the motors in front of me but when i get them I only have at most half an hour to use them before this must be finished. Thanks, ma7730