Blazemaguire
Senior Member
Hi,
I'm using an Xbox, sprung joystick potentiometer as an input. These obviously spring back to the center neutral position, and thus give an 'at rest' reading of about 127 to 128 (as you'd expect, being half way between 0 and 255) when reading the pot via ADC.
I want to use the joystick to act as a speed controlling forward and reverse input to drive a motor, i.e, push up, the motor goes forward, faster depending how much you push up, and when pushing down, the motor goes backwards, again, faster the harder down you press.
I'm more than happy with the output end and circuity (done this loads before) (Ill use the PWM pins to drive the inputs to a H bridge... got that bit working already)
For some stupid reason (and because I'm rubbish at Maths, especially 8 bit, non floating point Maths!) I can't figure out how to scale the joystick potentiometer reading to be opposite to it's read value, and also scaled from 0 to 255 (to give the full PWM duty cycle range)
What I want is below:
At joystick home position, the motor is off (I'll set a deadband area of ADC reading from around 125 to 130 with an IF statement) - This bit I'm happy with.
When I push DOWN past the programmed deadband on the joystick, values start at 125, then drop down incrementally to 0, but I want to get numbers into a variable that vary from 0 to 255 (from home, to fully down)
When I push UP on the joystick, the ADC records readings from 130 up to 255, but I want these to scale from 0 to 255.
I hope that makes sense?
I don't need help with the code, just the suggestion/hints of how to do the Maths. It might be because It's Sunday and I've been marking all day, but my brain can't seem to grasp it.!
Thanks in advance.
Rob
I'm using an Xbox, sprung joystick potentiometer as an input. These obviously spring back to the center neutral position, and thus give an 'at rest' reading of about 127 to 128 (as you'd expect, being half way between 0 and 255) when reading the pot via ADC.
I want to use the joystick to act as a speed controlling forward and reverse input to drive a motor, i.e, push up, the motor goes forward, faster depending how much you push up, and when pushing down, the motor goes backwards, again, faster the harder down you press.
I'm more than happy with the output end and circuity (done this loads before) (Ill use the PWM pins to drive the inputs to a H bridge... got that bit working already)
For some stupid reason (and because I'm rubbish at Maths, especially 8 bit, non floating point Maths!) I can't figure out how to scale the joystick potentiometer reading to be opposite to it's read value, and also scaled from 0 to 255 (to give the full PWM duty cycle range)
What I want is below:
At joystick home position, the motor is off (I'll set a deadband area of ADC reading from around 125 to 130 with an IF statement) - This bit I'm happy with.
When I push DOWN past the programmed deadband on the joystick, values start at 125, then drop down incrementally to 0, but I want to get numbers into a variable that vary from 0 to 255 (from home, to fully down)
When I push UP on the joystick, the ADC records readings from 130 up to 255, but I want these to scale from 0 to 255.
I hope that makes sense?
I don't need help with the code, just the suggestion/hints of how to do the Maths. It might be because It's Sunday and I've been marking all day, but my brain can't seem to grasp it.!
Thanks in advance.
Rob