View Single Post
  #20   Spotlight this post!  
Unread 21-01-2007, 11:03
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: Potentiometer PID

Quote:
Originally Posted by NextPerception View Post
i would do this

Code:
      if ( (Button1 * Button2) + (Button2 * Button3) + (Button3 * Button1) + ( Button1 + Button2 + Button3 ) == 1 )   //prevents crazy values if more than one button is pressed and keeps the same value if no button is pressed
      {
            Target = (Button1 * 200) + (Button2 * 400) + (Button3 * 600)
      }
this just makes more sense to me and I just post it here for any like minded people out there. I think it takes a little less cpu time too.
It makes sense if you're averse to using procedural programming and prefer functional programming. The logic behind your code gives the same result as the one using if statements, but the reason for doing it that way is obscure to anyone encountering it cold.

And it takes a lot more time to do it your way. A few comparisons and branches are faster than many multiplications and additions. Note that the original code never does more than three comparisons and one assignment. Yours always does six multiplications, thirteen additions, and one comparison. It would be instructive to compile both alternatives and compare the generated assembly language code in the listing file.

As an aside, wouldn't it work just as well to simplify the condition to this?
Code:
if ( ( Button1 + Button2 + Button3 ) == 1 )