Go Down

Topic: [no need to read]Problems with servo's and hardware Serial (Read 532 times) previous topic - next topic

Filip_J

Jun 19, 2013, 06:49 pm Last Edit: Jun 20, 2013, 03:43 am by Filip_J Reason: 1
Hi All

i'm quite new to this forum, so i hope this is the proper place to put this up:)

as the title suggests: I'm having problems with my serial connection when i include the servo library.

I'm currently working on a C# library for communicating with the Arduino ( also made a library for arduino). just a simple protocol for data transfer between them.


so here' s the deal: i've set it up  so that i have packet that looks like this

"ID" 1 byte -> for deciding what kind of packet i'm recieving/sending;
"Package Size" 1 byte -> for determing when i have received the entire package
"payload"

its working like a charm until i include the servo library.

the arduino sends my package like this (O represents an OK byte while X represents a random zero)

OXOXOXOXOXOX for a 6 byte package.

while the incoming bytes are just fine.

any help will be appreciated
Thanks:)

Erdin

We have to know more.

Which Arduino board are you using ?
Do you connect a servo motor to the board ? how ?
Could you upload you sketch between code tags ? (use the '#'-button above the text input field).
Does your library/sketch declare an array of hundreds of bytes ?
Does your library/sketch use a hardware timer ?

zoomkat

Servo test code that uses the hardware serial port.

Code: [Select]

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// 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.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // 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

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Filip_J

Hi again thanks for the response seems i was just being silly and somehow had a line in my c# code like this


                    int size = Port.BytesToRead;
                    while (size> 0)
                    {....}
   
it shoulhave been an if statement this way i would randomly read things normaly ( if 1 byte was recieved ) or very weirdly (if more bytes were recieved)

sorry again guys


Erdin


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy