View Single Post
  #5   Spotlight this post!  
Unread 07-02-2006, 21:56
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: change pwm range preserve input sensitivity

You have exactly the right idea -- multiply by the desired output range, divide by the available input range, and add an offset to get it to start where you want.

I will give you two warnings. First, the OI doesn't really want to see a potentiometer giving it a voltage. It wants to see a variable resistance. You won't necessarily get the full 0-254 range of values in either case, so you might need to tweak your formula slightly to compensate.

Second, the compiler is notorious for overflowing intermediate results when you do things like multiplying values that each fit in eight bits. A bit of (int)-casting will force the code to do 16-bit arithmetic, and you should be fine.
Code:
  pwm02 = (int)p3_aux * 154 / 254 + 100;