go_away

Author Topic: Driving servos for specific time period.  (Read 1260 times)

0 Members and 1 Guest are viewing this topic.

Offline flokosTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Driving servos for specific time period.
« on: May 25, 2010, 12:16:46 PM »
Hi im new here .
I'm working on a small robot that uses 2 parallax futaba continue rotation servos ,but i dont know how to make them rotate for specific time together at different speeds.
For microcontroller it uses an atmega 328 on an arduino duemilanove so i need a code to work with the arduino board.
Thanks for reading...

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Driving servos for specific time period.
« Reply #1 on: May 25, 2010, 12:57:27 PM »
Pseudocode:

void setup
{
initiate servos
}

void loop
{
//drive for one second
drive servo1 @0
drive servo2 @180
delay(1000);
//stop for one second
drive servo1 @90
drive servo2 @90
delay(1000);


}


90 is centered, and 0 and 180 are the different directions.

Offline flokosTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: Driving servos for specific time period.
« Reply #2 on: May 26, 2010, 05:07:53 AM »
I have done this one but the delay() is not the time the servos will be rotating but the delay between the pulses .

Offline Gungrave

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Driving servos for specific time period.
« Reply #3 on: May 26, 2010, 01:03:49 PM »
You could try something like:

Code: [Select]
int curTime = 0;
int servoTimer = 0;
int servoRunTime(10);

void setup() {
//initiate servos
}

void loop
{

if (user presses button ) {
    servoTimer = curTime;
}

if ( curTime < servoTimer + servoRunTime ) {
//Run servos
}

curTime ++;
}

You'd adjust servoRunTime to make it run for longer periods. Hope I didn't screw anything up ^^

Offline flokosTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: Driving servos for specific time period.
« Reply #4 on: May 27, 2010, 10:48:15 AM »
I made my version of your pseydocode in real code but i havent test it yet.
Here is the code tell me if  there is any mistake.
Code: [Select]
int curTime = millis(); //current time
int servoTimer = 0;
int servoRunTime = 5000; //5000 ms
int buttonval = LOW;//current button state
int buttonPin = 0;//button input pin
int servoPin1 = 3;//servo 1 output pin for pulses
int servoPin2 = 4;//servo 2 output pin for pulses
void setup() {
   pinMode(servoPin1,OUTPUT);//declaring servo pin 1 as an output
   pinMode(servoPin2,OUTPUT);//declaring servo pin 2 as an output
   pinMode(buttonPin,INPUT);//declaring button pin as an input
}

void loop
{
buttonval = digitalRead(buttonPin);
if (buttonval == HIGH ) {
     servoTimer = curTime;
}

if ( curTime < servoTimer + servoRunTime ) {
digitalWrite(ServoPin1,HIGH);//running servo 1
delayMicroseconds(1000);
digitalWrite(ServoPin2,HIGH);//running servo 2
delay(20);
}

int curTime = 0
}


I removed the curTime ++; because i can get the time the arduino is running from the function millis();.
Your idea with the button is pretty good but i cant use a button to make my robot move so i will do it with a led and
with checking its state.
« Last Edit: May 27, 2010, 10:55:46 AM by flokos »

Offline Gungrave

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Driving servos for specific time period.
« Reply #5 on: May 27, 2010, 02:51:17 PM »
I made my version of your pseydocode in real code but i havent test it yet.
Here is the code tell me if  there is any mistake.
...
I removed the curTime ++; because i can get the time the arduino is running from the function millis();.
Your idea with the button is pretty good but i cant use a button to make my robot move so i will do it with a led and
with checking its state.


I'm not familiar with the arduino, so I can't tell you if you've set all the pins up properly and stuff. But one conceptual mistake you had was setting curTime to zero at the end of the loop(). It should be set to the current time. Though, since you're using the arduino's timer you can eliminate the curTime variable entirely, and use "millis()" in its place for the if statement.

 


Get Your Ad Here