servo moving very slow,only works for delay of 1500 or more

im trying to rotate the servo motor by using this code,but this seems to be very slow how can i fast this thing up,so as to imitate mouse motion in real time

following code for vc++
#include “stdafx.h”
#include “conio.h”
#include “iostream”
#include “windows.h”
#include “WinDef.h”
#include “WinUser.h”
#include “windowsx.h”
#using <mscorlib.dll>
#pragma comment(lib, “gdi32.lib”)
#pragma comment(lib, “User32.lib”)

using namespace System;
using namespace System::IO::Ports;

int main(array<system::string ^=""> ^args)
{
String^ answer;
String^ answernew;
POINT coord;
answernew=“hello”;
// arduino settings

SerialPort^ arduino;
arduino = gcnew SerialPort(“COM5”, 9600);
// open port
try{
arduino->Open();
while(1)
{
GetCursorPos(&coord);
answer=Convert::ToString(int(coord.x/7.80))->PadLeft(3,‘0’);
//7.80 is scaleing factor of screenwidth by max angle of servo motor

//if mouse moves to new location
if(String::Compare(answer,answernew))
{
answernew=answer;
Sleep(1500); //servo moving only after this delay or more
//1 is motor no im trying to control
arduino->Write(String::Concat(“1”,answernew));
}
}
// close port to arduino
arduino->Close();
}
catch (IO::IOException^ e ) {
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
catch (ArgumentException^ e) {
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
}
Sleep(500);
return 0;
}

code for arduino
#include <servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;// a maximum of eight servo objects can be created

void setup()
{
myservo1.attach(7 );
myservo2.attach(8 );
myservo3.attach(9 );
// attaches the servo on pin 9 to the servo object

Serial.begin(9600);
}

void loop()
{
if(Serial.available()>0)
{
int pos=Serial.parseInt();
int servo=pos/1000;
pos=pos%1000;
Serial.print(pos);
if(pos>0&&pos<176)
switch(servo)
{
case 1:myservo1.write(pos);break;
case 2:myservo2.write(pos);break;
case 3:myservo3.write(pos);break; // tell servo to go to position in variable ‘pos’
}
}
}

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

Serial.begin(9600);

We're out of the stone age. Get the speed up.

 int servo=pos/1000;
pos=pos%1000;
Serial.print(pos);

printing data takes time. If you need speed, quit doing things that slow you down.

int pos=Serial.parseInt();

Sending two bytes - the servo # and the servo position - would be orders of magnitude faster then sending a string that needs to be converted back to an int.

Definitely use Serial.begin(115200) - 12 times faster!

You might do better to encode the position into a single byte, then send this single byte to change the servo - rather than an ascii representation.

Serial speed - maybe he has other hardware that needs this, and besides, it really is not the issue, just your personal preference.

not sure if this is the case, but if you use mosfets to control your motor, there needs to be a resistor between the pinout and the mosfet control. If you give a mosfet full power its inner fieldeffect doesnt do a good job of discharging resulting in less fast switching off turned on mosfets resulting in previous cycle of magnets not powering off fast enough, and slow motor performance. Like a transistor a mosfet only requires little power to drive their output

it seams to be an error that happens to a lot of people

PGTBOOS: not sure if this is the case, but if you use mosfets to control your motor, there needs to be a resistor between the pinout and the mosfet control. If you give a mosfet full power its inner fieldeffect doesnt do a good job of discharging resulting in less fast switching off turned on mosfets resulting in previous cycle of magnets not powering off fast enough, and slow motor performance. Like a transistor a mosfet only requires little power to drive their output

it seams to be an error that happens to a lot of people

I am having a lot of trouble understanding any of this posting - can you rephrase?

ehm you do use Mosfets ?

well, a mosfet has 3 pins, lets suppose a pin is connected to 12V (V in) and another pin to your arduino to control the mosfet (gate), and another (V out)to your motors magnet from where it goes to ground.

The principle of the Mosfet is that a charge in the gate creates a tunnel inside the Mosfet so the magnet inside your motor gets power.

Mosfet however require little charge to control that tunnel, this gate charge is tiny compared to the their (V out) power. If you overwhelm it with a lot of charge, then it takes more time before the charge is gone (and you might even destroy the Mosfet if you give it to much on the gate).

If it takes more time to discharge then it means it doesnt switch off as fast as you like.

So that would result that inside a motor more magnets might still be turned on.

Resulting in weak motor performance, for both speed and torsion

To resolve use resistors on the gate (its also protective for the Mosfet)

ehm you do use Mosfets ?

The OP uses the Servo library, so I think it is safe to assume that MOSFETs are not driven directly.

Serial speed - maybe he has other hardware that needs this, and besides, it really is not the issue, just your personal preference.

The Serial speed IS related to the issue. It takes time to send serial data. The amount of time depends on the baud rate. After the data is received, it needs to be parsed. The parseInt function is waiting a while to see if there will be more data. If not, it eventually ends, and the servo can be moved.

Meanwhile, the PC application is having to wait for the serial data to be received, the parseInt function to determine that there will be no more data, and for the servo to move. That is why the long sleep time is required.

Sending something from the PC that lets Serial.parseInt() know that the end of the int has arrived, such as a space, would speed up parseInt. Setting a higher speed would decrease the time that the serial data took to arrive, too.

Nothing will change how long the servo takes to move, but I got the impression that the speed of the servo wasn't the issue. It was how long it took before the PC could send a new position that was the issue.

sorry for the missing code snipplet.im using basic circuit diagram as .
i tried removing print statements,but still the same,does increasing baud rate has any harmful effect on arduino uno chip im using?

anuran:
im trying to rotate the servo motor by using this code,but this seems to be very slow how can i fast this thing up,so as to imitate mouse motion in real time

following code for vc++

#include “stdafx.h”
#include “conio.h”
#include “iostream”
#include “windows.h”
#include “WinDef.h”
#include “WinUser.h”
#include “windowsx.h”
#using <mscorlib.dll>
#pragma comment(lib, “gdi32.lib”)
#pragma comment(lib, “User32.lib”)

using namespace System;
using namespace System::IO::Ports;

int main(array<system::string ^=""> ^args)
{
String^ answer; 
String^ answernew; 
POINT coord;   
answernew=“hello”;
// arduino settings

SerialPort^ arduino;
arduino = gcnew SerialPort(“COM5”, 9600);
// open port
try{
arduino->Open();
while(1)
{   
GetCursorPos(&coord);
answer=Convert::ToString(int(coord.x/7.80))->PadLeft(3,‘0’);
//7.80 is scaleing factor of screenwidth by max angle of servo motor

//if mouse moves to new location
if(String::Compare(answer,answernew))
{
answernew=answer;
Sleep(1500); //servo moving only after this delay or more
//1 is motor no im trying to control
arduino->Write(String::Concat(“1”,answernew));
}
}
// close port to arduino
arduino->Close();
}
catch (IO::IOException^ e  ) {
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
catch (ArgumentException^ e) {
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
}
Sleep(500);   
return 0;
}



code for arduino


#include <servo.h>
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;// a maximum of eight servo objects can be created

void setup()

myservo1.attach(7 );
myservo2.attach(8 );
myservo3.attach(9 );
// attaches the servo on pin 9 to the servo object

Serial.begin(9600);
}

void loop()
{
if(Serial.available()>0)   
{
int pos=Serial.parseInt();     
int servo=pos/1000;
pos=pos%1000;
Serial.print(pos);
      if(pos>0&&pos<176)     
switch(servo)     
{     
case 1:myservo1.write(pos);break;
      case 2:myservo2.write(pos);break;
      case 3:myservo3.write(pos);break;                  // tell servo to go to position in variable ‘pos’     
}   
}                         
}

i tried using increasing the baud rate,deleted print line,and using two Serial.parseInt(),but still there has to be a delay of 1500 or more otherwise,its not working after first angle.i want to do something like this http://www.youtube.com/watch?v=lFvQRWXX24U is it possible from ms visual cpp that im using?

anuran: i tried removing print statements,but still the same,does increasing baud rate has any harmful effect on arduino uno chip im using?

No, why would it?

http://www.gammon.com.au/serial

anuran: sorry for the missing code snipplet.im using basic circuit diagram as . i tried removing print statements,but still the same,does increasing baud rate has any harmful effect on arduino uno chip im using?

It's not clear whether your problem is the time taken to send movement commands from the PC to the Arduino, and/or the Arduino's capability to move the servo as fast as you're commanding.

If I were you I'd write a sketch that just moved the servo from one end of its travel to the other, and confirm the servo is actually capable of moving as fast as you want. Powering the servo via the Arduino looks rather dodgy (what is the peak current taken by the servo?), could possibly be overloading the Arduino and could explain why the servo is lethargic.

I don't think the apparent initial ~1.5 sec delay before the servo ~accuratly tracks the mouse movements has anything to do with the 9600 baud rate. Is possibly the mouse application possibly somehow opening/closing the pc serial portr, causing the arduino to reset? Except for the initial delay, the mouse following looks as expected.

You aren't really powering that servo from the Arduino, are you? Servos generally draw way too much current for that.

ya i am providing power from arduino only,is that the cause? and servo is working fine for examples like sweep http://arduino.cc/it/Tutorial/Sweep

Depending on the servo, you may be able to power a single one from the arduino but it is marginal at best. Since the sweep example works though, I suggest you follow peterH’s advice in reply #15 and see whether the servo performs as you require.