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!

  • What do you use for an inclinometer? How do you read it? How long to return a reading? Do the readings make sense when the motror is operating or does movement scramble trhe results? May 8 '15 at 11:59
  • Thanks for looking into Russel! 1.- I measure inclination using a Murata SCA100T-D2 chip. 2.- The chip is conected to the Arduino board using SPI. 3.- The reading has no delays. 4.- I don´t know , but Its a very interesting idea, it looks something logical as the movement is not perfect and the sensor is very precise (and fast!) Maybe I need to ad some delay? May 8 '15 at 13:33
  • IF the sensor can be read "on the fly" with motor moving it sounds easy to do. Is not target inclination fixed and only real changes? If so then a loop that reads sensor and compares and exists when target is reached seems all that is needed. Am I missing something? May 8 '15 at 14:34

This is a classic system where PID will really pay off. Basically this will allow you to tune your system so that if you are off by 5 degree it will open the relays for 3 seconds. But if you are off by 1 degree it will open them for half a second. (Or whatever works for you). It might be tricky for you to get right but it will give you the smooth response that looks really good and feels natural.

Take a look here http://playground.arduino.cc/Code/PIDLibrary

  • Thank you very much portforwardpodcast! Will be looking into PId, really seems what I need! May 31 '15 at 15:13

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) < realElevation | (targetElevation - 3) > realElevation) {

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

    while (realElevation < targetElevation)
    { Serial.println("Moving to south");
      digitalWrite(south, LOW);
    digitalWrite(south, HIGH);

    while (realElevation > targetElevation)
    { Serial.println("Moving to north");
      digitalWrite(north, LOW);
    digitalWrite(north, HIGH);

  while ((targetHourangle+3) < realHourangle |  (targetHourangle-3) > realHourangle  ) {

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

    while (realHourangle < targetHourangle)
    { Serial.println("Moving to west");
      digitalWrite(west, LOW);
    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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.