Go Down

### Topic: syntax for fscale code (Read 1 time)previous topic - next topic

#### SouthernAtHeart

##### Sep 20, 2013, 12:51 am
In reference to this code that changes the curve or scale of numbers:
http://playground.arduino.cc/main/fscale

I have this code that give my the desired curve on a reading that is from 1 to 64, but I can't figure out how to get the same result for the values -1 to -64
Code: [Select]
`  if (motor_speed > 0)  motor_speed = fscale( 1, 64, 1, 64, motor_speed, -1.5);`

I thought this would do it, but all it gives me is zeros:
Code: [Select]
`  if (motor_speed < 0)  motor_speed = fscale( -1, -64, -1, -64, motor_speed, -1.5);`

#### lar3ry

#1
##### Sep 20, 2013, 01:00 am
The motor still has a speed, yes?
I'm no math whiz, and I don't know what circuit you're using, but can't you just negate the value(s) returned from the function? In other words, feed it athe abs(motor_speed), then negate the reult and fee it to motor_speed.

If I'm actually way off, I beg your indulgence.

#### SouthernAtHeart

#2
##### Sep 20, 2013, 02:17 am
-1 to -64 is the reverse speed...

#### PeterH

#3
##### Sep 20, 2013, 02:32 am
I haven't tested that function but it appears to have explicit support for negative ranges. Have you got a sample sketch we could run showing the incorrect values when you try to apply it to a negative range?

If you can't be bothered figuring out what the problem is, you could just negate the input motor_speed if it was negative, and then invert the result too.

#### SouthernAtHeart

#4
##### Sep 20, 2013, 02:54 am
I think I got it:
Code: [Select]
`  if (motor_speed < 0)  motor_speed = fscale( -1, -64, -1, -64, motor_speed, -1.5);`
This doesn't fit the rules:
originalMin - the minimum value of the original range - this MUST be less than origninalMax
originalMax - the maximum value of the original range - this MUST be greater than orginalMin

-1 isn't lower than -64

#### lar3ry

#5
##### Sep 20, 2013, 05:20 am

-1 to -64 is the reverse speed...

Yes, of course, but a value of, lets say, -10 is just the same speed as +10, but in the opposite direction, no?
If you want to increase the reverse speed, you send it a more negative number, right?
It would be worth a shot to try it. Just make all the values you pass the same as you would pass if it was a forward speed. Negate the result and set the speed. See if it works. It's what I would try right away, partly because it makes sense, and partly because it's dead easy to try.

#### robtillaart

#6
##### Sep 20, 2013, 11:49 am

In reference to this code that changes the curve or scale of numbers:
http://playground.arduino.cc/main/fscale

I have this code that give my the desired curve on a reading that is from 1 to 64, but I can't figure out how to get the same result for the values -1 to -64
Code: [Select]
`  if (motor_speed > 0)  motor_speed = fscale( 1, 64, 1, 64, motor_speed, -1.5);`

I thought this would do it, but all it gives me is zeros:
Code: [Select]
`  if (motor_speed < 0)  motor_speed = fscale( -1, -64, -1, -64, motor_speed, -1.5);`

use the positive working variation?
Code: [Select]
`  if (motor_speed < 0)  motor_speed = -fscale( 1, 64, 1, 64, -motor_speed, -1.5);`
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up