View Single Post
  #3   Spotlight this post!  
Unread 20-02-2007, 13: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: Sonar Function Issue

Quote:
Originally Posted by Andrew Blair View Post
...my temp variable is never changing...

Code:
int delta_distance(void)
{
    int temp;
    
    temp -= distance();

    return (int) temp;

    temp=distance;
}
The temp variable in your code evaporates as soon as delta_distance() returns. If you want to keep a local variable's value between calls to a function, you need to declare it static:
Code:
int delta_distance(void)
{
    static int last_distance = 0; // set to zero only on program start, retains value between function calls
    int delta; 
    delta = distance()-last_distance; // compute difference between distances
    last_distance = distance(); // remember this distance for next time
    return delta;
}