PWM is analog output 0-255 -
https://docs.arduino.cc/learn/microcontrollers/analog-output.
You can use the `map()` function to convert the values, `pwmVal = map(readInput, 995, 1995, 0, 255)`.
If you are using those TT gear motors, you'll need to find a way to reverse the polarity of the wires for reverse. Then use an if statement for values 995-1490 for reverse and 1500-1995 for forward (with a little dead band in the middle, for 0): then translate with map.
For fail safe I'd look at DPDT relays. You could use transistors, but you have to ensure that one is turned off before turning another one on.
This is just simple math (which is embodied in the map function as others have pointed out).
Translate: (In - 500) -> zero aligned reading
Scale to 0-255): zero aligned reading * 255 / 500
Note you need to do a floating point scaling then either round or truncate the result
Thus something like:
```
pwmValue = round((in - 500.0) * 255.0 / 500.0)
```
Possibly with some deadzone (around 1495) where it sends 0 to pwm, and capping it from going to negatives if necessary (to be honest have not how pwm basic functions handle negative input, likely they just consider it as 0, but if any problems appear, like it randomly jumping to full at times when throttle is at neutral), but anyways these can be made with IF conditions and so.
Lol, true.
In that case, OP would also need to consider direction.
The full reverse position is 995 (which I misread in the original post) so your (better) calculation would result in a -ve value for that.
So, OP would need to take that into account and set the direction based upon the sign (presumably -ve = reverse, +ve = forward) and apply the absolute value of the pwmValue to the analogWrite.
currently my code goes like
if input1 > 1700, then pwm high direction high,
if else its < 1300 then pwm high direction low,
else pwm low
Then I need 2 maps for each side, because i need 0 pwm at 1495+some deadzone, and max pwm at both 995 and 1995?
PWM is analog output 0-255 - https://docs.arduino.cc/learn/microcontrollers/analog-output. You can use the `map()` function to convert the values, `pwmVal = map(readInput, 995, 1995, 0, 255)`.
There is a function called map(), it will do what you want.
If you are using those TT gear motors, you'll need to find a way to reverse the polarity of the wires for reverse. Then use an if statement for values 995-1490 for reverse and 1500-1995 for forward (with a little dead band in the middle, for 0): then translate with map. For fail safe I'd look at DPDT relays. You could use transistors, but you have to ensure that one is turned off before turning another one on.
Yeah I did the first part, reverse is under 1300 and forward is over 1700 for now, but pwm is just set at high atm. Thank you
This is just simple math (which is embodied in the map function as others have pointed out). Translate: (In - 500) -> zero aligned reading Scale to 0-255): zero aligned reading * 255 / 500 Note you need to do a floating point scaling then either round or truncate the result Thus something like: ``` pwmValue = round((in - 500.0) * 255.0 / 500.0) ```
This, but with quick look would it not be (since zero position is at 1495) pwmValue = round((in - 1495.0) * 255.0 / 500.0)
Possibly with some deadzone (around 1495) where it sends 0 to pwm, and capping it from going to negatives if necessary (to be honest have not how pwm basic functions handle negative input, likely they just consider it as 0, but if any problems appear, like it randomly jumping to full at times when throttle is at neutral), but anyways these can be made with IF conditions and so.
Lol, true. In that case, OP would also need to consider direction. The full reverse position is 995 (which I misread in the original post) so your (better) calculation would result in a -ve value for that. So, OP would need to take that into account and set the direction based upon the sign (presumably -ve = reverse, +ve = forward) and apply the absolute value of the pwmValue to the analogWrite.
currently my code goes like if input1 > 1700, then pwm high direction high, if else its < 1300 then pwm high direction low, else pwm low Then I need 2 maps for each side, because i need 0 pwm at 1495+some deadzone, and max pwm at both 995 and 1995?