wrong calculation using MAP instruction in Arduino UNO with Sabertooth 2x32

Hello all,

I am new here as i want to build my own segway, i face a calculation error when using map function as follows:

motorLeft= map(motorLeftSpeed, -100, 100, 1, 127);

motorRight=map(motorRightSpeed, -100, 100, 128, 255);

for motorLeft the result is ok is correct, as when speed is 0 (zero) it returns 64, but for motorRight the return for speed =0 is -64 !! and not 190-191 as expected for no speed(motor stopped)!!

i have also changed the new range from 1 to 255 and looks ok 127-128 but when changed to 128-255 is a problem.

I look forward for an advice :confused:

Regards,
Ion.

Generally, the two map() calls should be the same, but use the returned values differently:

  motorLeft = map(motorLeftSpeed, -100, 100, 1, 127);

  motorRight = 255 - map(motorRightSpeed, -100, 100, 1, 127);

To be sure what your problem is, we'd need to see all of your code (including proof that map is working incorrectly, which I doubt).

i guess is ok to use 255 - map(motorRightSpeed, -100, 100, 1, 127) as in case of a motorRight stopped (64) will return 255-64 =191 which is speed zero for MotorRight !!

Will try this!
Thank you Paul!!

is not working... still -64 returned ..!

You need to print the inputs to the function. What MotorSpeedX yields a value of -64?

IONUTZ:
Hello all,

I am new here as i want to build my own segway, i face a calculation error when using map function as follows:

motorLeft= map(motorLeftSpeed, -100, 100, 1, 127);

motorRight=map(motorRightSpeed, -100, 100, 128, 255);

for motorLeft the result is ok is correct, as when speed is 0 (zero) it returns 64, but for motorRight the return for speed =0 is -64 !! and not 190-191 as expected for no speed(motor stopped)!!

i have also changed the new range from 1 to 255 and looks ok 127-128 but when changed to 128-255 is a problem.

I look forward for an advice :confused:

Regards,
Ion.

What datatype is motorRight?

Please post the rest of your code. I suspect that the real problem occurs in a different part of your code.