Relay feedback control I have a two-relay system set up to a DC motor. So, if I close one relay, the motor moves on one way and if I close the second relay (and open the first) the motor goes on the other direction.

I compare two variables (target inclination and real inclination) if they are not the same I would like to open one of the two relays UNTIL the are the same value. 

Both variables are calculated based on inclinometer input and some aritmethics.

I do know how to calculate the variables, how to close the relay based in the comparation of variables and which relay open. 

But I don´t know how I can tell to open the relay because the motor has reached target inclination. 

How I can recalculate and compare constantly target inclination and real inclination when the relay is closed and the motor moving?
Is this posible on Arduino? Is this multithreading?

Thank you very much, any ideas are welcome!
Answering my own question, I think I found some way to get the solar panel face the sun.

As said before, the inclinometer reads the position and makes some calculations using lookposition() function, then this happens:

void lookforsun() {
  while ((targetElevation + 3) &lt; realElevation | (targetElevation - 3) &gt; realElevation) {

    Serial.println("Elevation is not correct");

    while (realElevation &lt; targetElevation)
    { Serial.println("Moving to south");
      digitalWrite(south, LOW);
      lookposition();
    }
    digitalWrite(south, HIGH);

    while (realElevation &gt; targetElevation)
    { Serial.println("Moving to north");
      digitalWrite(north, LOW);
      lookposition();
    }
    digitalWrite(north, HIGH);
  }


  while ((targetHourangle+3) &lt; realHourangle |  (targetHourangle-3) &gt; realHourangle  ) {

    Serial.print("Hour angle is not correct");
    while (realHourangle &gt; targetHourangle)
    { Serial.println("Moving to east");
      digitalWrite(east, LOW);
      lookposition();
    }
    digitalWrite(east, HIGH);

    while (realHourangle &lt; targetHourangle)
    { Serial.println("Moving to west");
      digitalWrite(west, LOW);
      lookposition();
    }
    digitalWrite(west, HIGH);
  }


As you can see I took the frightening way of nested while loops, but appears to work.

The first while measures if realElevation is between +-3 degrees of targetElevation, if realElevation is far away (target+-3) enters the second while loop, which determines if needs to go up or down.

I included the +-3 because the sensor is so very sensible.

Thank you very much  Russell McMahon and portforwardpodcast for your ideas!