Servo motor and Serial

Hi all
I am having a hard time trying to understand why my basic servo motor isn't working as it should.
I tried the servo example and it works fine. However, as a next step, I wanted to add a serial port where, once it detects input from the serial port, it would activate the motor once and then stop. Problem is when I enter anything from the Serial Monitor window, the motor just constantly spins.
I want to believe it's something basic I'm missing. Any help will be appreciate
This is my code. My thinking is once there is input (any input for now) just spin the motor and empty the buffer.
Thanks

#include <Servo.h>

int servoPin = 9;
Servo servo;
int pos = 0;

void setup() {
servo.attach(servoPin);
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}

for (pos = 180; pos >= 0; pos -= 1) { 
  servo.write(pos);              
  delay(15); 
}
while (Serial.available() > 0) { // To empty the buffer

Serial.read();

}
}

}

  if (Serial.available() > 0) 

This checks if there is something to read.. and the next 2 for loops just cause the servo to move. Do you actually want to read what is being sent?

No I don't really need to know what is being read (for now). Just trying to get it to read once and then empty the buffer

OK So it seems like the problem is with the servo part of it.
I was able to get a different servo motor and it works with that one
The difference between both is one goes to 180 deg (the one that works) and the other is 360 deg.
The site says this about the 360 deg
"For the 360° servo ,1500 mhz reach the center,500Mhz is Clockwise,2500mhz is Counterclockwise."
"The high time is 1.0ms-2.0ms,when 2.0ms it is counterclockwise. When 1.5ms it is clockwise. When 1.5ms it is Stopped."
Maybe the issue is I have to change some settings on the servo side?

What type of Servo Motor you are using?

This is the one I want to use that is causing problems

is this a continuous rotation servo?

if so, i believe it needs to be set to 90deg to stop the motor. 0 and 180 move in either CW or CCW

see Continuous Rotation Servo Motor

That referred page does not say about its type number. If you have purchased the Servo, then provide the type number or a link to its manufacturer for Technical Specs.

I am afraid if this Motor is compatible with Servo.h Library!

The motor says TD-8130MG. I did some searching and found this. I'm hoping it ca help


n

do the servo change direction every 1.35 sec (90 * 15msec)?

Thank you for providing the specs according to which the motor is similar to the traditional one of type SG-90. It swings from 00 to 1800 and not from 00 to 3600. The motor is fully compatible with Servo.h Library.

You may exercise the following Tutorial to operate the Servo using Android Phone and Bluetooth in order to give you an idea of how to operate Servo using UART /SUART Port. Servo is connected with UNO's 5V just for test purposes; its supply should be from separate 5V source.

Send FRW Command from Phone, the Servo will turn in the forward direction by 100 and will stop at 1800 position. You can send the BKW command to go back to original zero position by 100 step.

Sketch:

#include<Servo.h>
Servo myServo;

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);

char myData[10] = {0};
void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);

  myServo.attach(8);
  //--testing that the Servo is healthy---------
  myServo.write(0);
  delay(2000);
  myServo.write(65);
  delay(2000);
  myServo.write(0);
}

void loop()
{
  while (SUART.available() == 0)
  {
    ;
  }

  byte m = SUART.readBytesUntil('\n', myData, 10);
  myData[m] = '\0';
  if (strcmp(myData, "FRW") == 0)
  {
    for (int i = 0, pos = 10; i < 18; i++, pos = pos + 10)
    {
      myServo.write(pos);
      delay(200);
      if (pos == 180)
      {
        break;
      }
    }
  }
  else
  {
    if (strcmp(myData, "BKW") == 0)
      for (int i = 0, pos = 180; i < 18; i++, pos = pos - 10)
      {
        myServo.write(pos);
        delay(200);
        if (pos == 0)
        {
          break;
        }
      }
  }
}

that spec sheet indicates that the limit angle is 360 deg

What doe the following of the data sheets mean -- ?
Running degree 1800 +/-30

see line 3.4
are these conflicting?

By default, the Servo will swing across 0 deg to 180 deg. (I guess) If the internal limiter is removed/unlocked, the Servo will make the whole 360 deg circle.

Thank you so much for the help.
I did connect to the Arduino just for testing and using a PC with Arduino IDE and serial output to test. I will use a separate power supply in the final install.
My final system will be a PC based one, where the PC sends a signal via the USB cable to the Arduino ... so was thinking of just using Serial.read()
All I'll be sending on the serial is a number between 1 and 9. The number determines the amount of full revolutions (so 1 is 360 deg, 2 will be 360 deg twice etc.. via a loop)
I tried to adjust your code to use the break points if 0 or 180 but it doesn't seem to work.
The Amazon website has this, so not sure if this can give any hints on where I'm going wrong

in most cases Amazon does not provide detailed technical specifcations of the products.

And in a lot of cases amazon has not the lowest price for electronics.
Anyway it seems to be a servo that can rotate contious for a inifinity number of full rotations.

A Servosignal with pulse-length 1500 microseconds (not 1500 Mhz like the amazon text says) should make the servo stop to not rotate clockwise nor rotate counterclockwise

The Amazon-link you provided says


If this is true you have the continious rotating servo.

Is a continious rotatting servo that thing that you need. Shall the servo-Axle rotate clockwise, clockwise, clockwise more than 360 degrees?
and at some other time
Shall the servo-Axle rotate counterclockwise, clounterclockwise, counterclockwise more than 360 degress?

If yes this is a suitable servo.

If you want a servo-Arm move to the left less than 360 degrees
move to the right less than 360 degeress and then the servo-horn should hold this position.
This 360-degree-servo is very bad suited.

For the operation of the continious-rotating servo:
standing still is given for a pulse-length of 1500 microseconds.
Through deviations in manufacturing it might be that the servop stands still at 1480 microseconds or 1530 microseconds

Please write about your project. Give an overvuew about your project.

best regards Stefan

Hi. Thanks for the info
My project involves rotating a shaft 360 degrees one way (doesn't matter clockwise or anti clockwise)
It can rotate once, twice, three times... etc up to maybe 6 times. For now I just want it to rotate a full 360 deg then stop. I can put that in a loop and have it rotate any amount of times after, but basically it has to rotate 360 deg once, then stop. The codes I try keep making it rotate without stopping.
Hope this helps
Thanks again

If it has to be precise 360,0 degrees not 366 degrees nor 355 degress nor 725 degrees etc.
Then this kind of servo is very hard to use because you have to catch the exact time to change the servo-pulse-length from rotating pulse-length to stop-pulse-length

The reason is this servo has no position-feedback

You still haven't described your overall application and the final purpos of rotating 360 degrees. Rotating 360 is not a self-purpose. there is something behind it.
As soon as you are willing to describe the final purpose
really good suggestions can be made.

If you prefer to play a ping-pong-game of riddling from detail to detail this has just the effect that it takes longer to finish your project.

Hi. I thought I explained what I needed, sorry.
I'm hooking this up to a cereal dispenser. The dispenser rotates to give the cereal. Usually 1 rotation is 360 degrees. If it rotates 366 or 355 it's OK. I'm controlling the dispenser from a PC. I did not decide on how many rotations as yet but I know for now I want it to just rotate more or less 360 deg then stop.
Hope I am clearer now