Wow, that was fast. Ok. how about this:
#include <Servo.h>
char byteIn;
char numberChar[6];
int sVal;
int x;
int y;
float number = 0.000;
Servo myservo;
void setup() {
Serial1.begin(9600); // start serial communication at 9600bps
Serial.begin(9600);
myservo.attach(9);
myservo.write(90);
}
void loop()
{
if (Serial1.available())
{
byteIn = Serial1.read();
if (byteIn == 'E')
{
x = 0;
y = 0;
if (numberChar[0]=='-')\
{
y = 1;
}
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);
if (numberChar[0]=='-')\
{
number = number*-1;
}
sVal = (int)map(number, -10, 10, 0, 179);
myservo.write(sVal);
Serial.print("Raw value: ");
Serial.println(number);
Serial.print("Servo angle: ");
Serial.println(sVal);
Serial.println();
delay(10);
}
else
{
numberChar[x] = byteIn;
x++;
}
}
}
The input is actually coming from a python script running on scripting layer for android (SL4A) over bluetooth through a JY-MCU module to the Serial1 input, so it's a little hard to tell you exactly what is coming in, given that the data goes through a couple layers of android data manipulation then the bluetooth module, but I can tell you with reasonable certainty that the value coming in (for this example) is "5.057" as a series of bytes through Serial1. The output is
Raw value: 5.06
Servo angle: 134
So yes, I am pretty sure the data is right. The servo essentially does nothing. Hence the problem, since it started at 90 degrees and should be at 134.