My Servo won't move [SOLVED]

Can I introduce you to a good friend of mine?

How is the servo connected and powered?

     number = numberChar[0+y]-'0';
     number += ((numberChar[2+y]-'0')/10.0);
     number += ((numberChar[3+y]-'0')/100.0);
     number += ((numberChar[4+y]-'0')/1000.0);

0, 2, 3, 4? Are you missing a finger?

No, he's missing the point :wink:

Lol, yes I am missing the point. :wink:
As to the servo, the white is connected to pin 9, the red is on VIN (Straight to the USB cable powering the device), and the black is obviously ground. Servos usually don't draw more than 50 mA, or so I have read, and USB can supply up to 500mA, so I think power is fine. What makes me think the wiring is good is that the "sweep" example works fine. I am at rather a loss here, not even getting any errors. I tried myservo.writeMicroseconds, mapping the data between 1000 and 2000, but that didn't work either.

Servos usually don't draw more than 50 mA

Bullpoop. Servos can pull up to an amp under load.

If your servo works with the sweep example, then the problem isn't with the servo or the wiring. It is with the input that you are sending it, or the way you are processing that input.

What are you sending the Arduino? From what?

What happens if you set the servo to 0 degrees in setup?

Paul, yes, but this servo is not under load, as I am just testing it. As to the input, my thoughts exactly. Read my first two posts to see what the input is.
If I set it to 0, then it goes to 0 and stays there for the duration of the program. Currently it goes to 90 and stays there.

Read my first two posts to see what the input is.

You've explained what you think they are, not what they actually are, or where the input is coming from. Let's start with some really simple questions, one at a time.

Are you using the Serial Monitor to send data to the Arduino?

Au contraire, mon ami. I have explained exactly where the data is coming from. It originates in an android phone, it is sent by a python script to the JY-MCU bluetooth module connected to the Serial1 input of the Arduino Mega 2560. The data received by the bluetooth chip is sent to the arduino, fed into a character array until the program hits the end character 'E', then it converts the character array to a floating point number, maps it to the servo's range of 0 to 179, and sends it to the servo via myservo.write(sVal). The data is sent to the computer via Serial both before and after mapping and appears to be correct. Another possibly related issue is that I have the program set up to go to the thousandths place on the float value, but I appear to only be getting hundredths. For a specific example, refer to my second post.

The reason for the questions about the source of the data are because you are assuming that EVERY character read from the serial port is useful, and goes in a specific spot. If the sender is tacking a carriage return/line feed after the E, that will screw up the next iteration, since the carriage return and line feed are going where you expect the ones digit and the decimal point.

but I appear to only be getting hundredths.

Getting? Or printing?

Yes, I see your point, but no there is no carriage return at the end of transmission. If I create a program that just relays information from one serial to another, I just get a long string of numbers separated by 'E's. Also, I know that's not the case because the data being printed to the computer would also be screwed up, but it's not. About the precision, I am wondering what would cause there to be a difference between calculating hundredths and printing hundredths. By the way, thanks for the help so far.
Just for clarity, here is the python script on the phone:

import android, time

BT_DEVICE_ID = '00:12:09:13:97:48'

droid = android.Android()
droid.bluetoothConnect('00001101-0000-1000-8000-00805F9B34FB', BT_DEVICE_ID)
x = 0
while x<100:
	droid.startSensingTimed(1, 25)
	time.sleep(0.1)
	s4 = droid.sensorsReadAccelerometer().result
	droid.stopSensing()
	ret = round(s4[1], 3)
	droid.bluetoothWrite(str(ret)+"E")
	print(ret)
	x += 1

exit()

, I am wondering what would cause there to be a difference between calculating hundredths and printing hundredths.

The default two places of decimals for the "print" method?

     Serial.print("Raw value: ");
     Serial.println(number);
     Serial.print("Servo angle: ");
     Serial.println(sVal);

number is a float. By default, floats are printed to two decimal places. You need an optional 2nd argument to get more (or less) than 2 decimal places.

Well, that would do it. Any other ideas about the servo?

If the sweep example works then the hardware should be OK. I can't see any reason why the sketch shouldn't move the servo to the angle it is printing out, so there's a faulty assumption here somewhere.

I suggest you take a copy of the sketch and replace the whole of loop() with something like this:

void loop()
{
  delay(5000);
  sVal = 134;
  myservo.write(sVal);
  Serial.print("Servo angle: ");
  Serial.println(sVal);
}

If everything works as we assume, the sketch should move the servo to angle 90, wait five seconds and then move it to angle 134. It will also print out a message to show that it has done it. Does it print out the message? Does the servo move as expected?

Ok, I tried what you said, the servo moves as expected, and the message prints just fine. Now I will try bringing back the servo related bits of the void loop and see what happens.

Got it working, it had to do with the fact that sVal was assigning improperly due to being an integer. I mad sVal a float, and it started working, but I had to type cast sVal to int in the myservo.write statement.

I had to type cast sVal to int in the myservo.write statement.

You didn't have to, but it would do no harm.

AWOL:
You didn't have to, but it would do no harm.

Hmm, well it didn't work before I type cast it. Oh well, it works. Anyway, thanks guys for all the help!