View Single Post
  #21   Spotlight this post!  
Unread 01-05-2008, 18:39
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: Need help with PWM 1-2ms pulse control

When you have two mutually exclusive tests like that, you can use the if statement's else clause.

Code:
if  (rc_dig_in06 == 1)
{
	pwm01 = (long) Get_Analog_Value(rc_ana_in01) *  127 / 1023 + 127;
}
else
{
	pwm01 = (long) Get_Analog_Value(rc_ana_in01) * -127 / 1023 + 127;
}
Note that I've removed the parentheses around (127/1023). With them in place, the intermediate result of the division will be zero, and I believe you'll find the output never varies from 127. Instead, you want to defer the division until as late as possible so you don't lose bits that you need.



I'd actually have done it a little differently:
Code:
pwm01 = (int)127 + Get_Analog_Value(rc_ana_in01) * rc_dig_in06?127:-127 / 1023;
But that's probably more cryptic than it needs to be for your purposes.