Using serial from PC to send drive angles to two servos is not working.

I am attempting to drive two servo motor in response to a serial command from a c++ application on my PC. I have been able to drive the two servo motor in response to a sensor using SPI but can not get a stable solution for serial over usb. Ideally I am looking for something that could support at least 60 Hz or even 120 Hc which I figure should be no problem.

Within the attached sketch.cpp file everything appears to work and is stable if I comment out either of the servos. servo.write(...) call and the associated servo articulates as expected. However if I have both the servo.write() active in the code (line ~48 and 49) the SerialFromPC.cpp file tends to fail on the WriteFile(hSerial...) line.

Another interesting thing is that the failure only seemed to happen after a negative command was sent to the arduino uno. I attempted to only pass positive values then offset it. I offset by 50 so the resulting valRoll was midRool + rollAng - 50; but that failed as well...

Also I am sure the byte handling for sending floats over serial is garbage. if you have any suggestions that would be great. I was hoping to do something along the lines of r2.324p-12.04 to set the roll and pitch to 2.324 and -12.04 degrees respectively.

sketch.cpp (1.31 KB)

SerialFromPC.cpp (2.67 KB)

Pleas post your code

Good luck finding servos that are accurate to 4 / 1000th of a degree. Most hobby servos aren't even accurate to a whole degree.

Steve

You can use the below to check your servo setup using the serial monitor.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#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()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}