Servo and serial read out of game

Hello,

I want my servo to rotate on a iput of a game.
The value’s are with a lot of numbers (4,865022045) but I only whant to juse 4 or 4,86.
I want to juse the numbers out the game from -50, 0, 50 and set it to a servo angle 0 degrees, 90 degrees, 180 degrees.

This is my program;
#include <Servo.h>

//Servo myservo;
Servo servoPitch;
Servo servoRoll;

const int PORT_PITCH = 9;
const int PORT_ROLL = 10;

int servoPosition = 1;

void setup() {
//myservo.attach(PORT_PITCH);
servoPitch.attach(PORT_PITCH);
servoRoll.attach(PORT_ROLL);
Serial.begin(9600);
}

void HandlePitch()
{
static int val = 0;
bool exit = false;

while (!exit)
{
char ch = Serial.read();

switch(ch)
{
case ‘0’…‘9’:
val = val * 10 + ch - ‘0’;
break;
case ‘s’:
servoPitch.write(val);
val = 0;
exit = true;
break;
}
}
}

void HandleRoll()
{
static int val = 0;
bool exit = false;

while (!exit)
{
char ch = Serial.read();

switch(ch)
{
case ‘0’…‘9’:
val = val * 10 + ch - ‘0’;
break;
case ‘s’:
servoRoll.write(val);
val = 0;
exit = true;
break;
}
}
}

void loop()
{
if(Serial.available())
{
char ch = Serial.read();

/*switch(ch)
{
case ‘0’…‘9’:
val = val * 10 + ch - ‘0’;
break;
case ‘s’:
myservo.write(val);
val = 0;
break;
} */

switch(ch)
{
case ‘p’:
HandlePitch();
break;
case ‘r’:
HandleRoll();
break;
}
}
}

who can help me???

Hks76:
The value’s are with a lot of numbers (4,865022045) but I only whant to juse 4 or 4,86.

You’ll have to elaborate on what you mean by this.

Are you saying the values have a large number of places after the decimal point?

Did you read the sticky topic at the top of this section? Any good reason why not?

Hello,

Thanks for the replay.
I getting data out of flightsimulator into visual basic.

That data I want to convert to the servo angle (0 degrees till 180 degrees).
the servo makes now large movements on a small number change from Visual Basic.
That’s why I want the servo to juse whole numbers (1) or up to two decimal (1,34) places and not use the rest.
Also I don’t want the servo to use the numbers above 50 and below -50.

So that -50 out of visual basic is servo 0 degrees en 0 out of visual basic is 90 degrees for the servo and 50 out of visual basic is 180 degrees.
(and everything in between).

I hope you understand what I mean.

Sorry for my bad englisch (I’m from Holland)

VB.jpg

The servo methods only use integers, so your only problem is whether to round or truncate.