How to bring back a worm gear motor to initial position?

Hello

Im new in this field. I have Pololu High-Power 18V, 15A motor driver, Arduino uno microcontroler and DC worm gear Motor (12V). Im building a prototype for tracking a solar panel. I would like to ask for a help regarding some sample code, that after tracking the panel with warm gear Motor it returns the panel in their initial position (horizontal).

I have this sample code: First I im doing sensor reading

int readSensor1()
{
  //  interrupts();
  int avg = 0;
  /*SHIFT DATA*/
  for (int i = 0; i < array_size; i++)
  {
    avg = avg + sensor1_data_hinge[i];
    sensor1_data_hinge[i] = sensor1_data_hinge[i+1];
  }
  /*ACCEPT NEW READING*/
  sensor1_data_hinge[array_size-1] = analogRead(sensor1);
  avg = avg/array_size;

  // noInterrupts();
  return avg;
}

Than I have implemented procedure for motor control

void check_motor1()
{
  //read sensor
  data1 = readSensor1();
  //plan which movement to take
  if (data1 >= upperthres)
  {
    planned_motor1 = 1;
  }
  else if (data1 <= lowerthres)
  {
    planned_motor1 = 2;
  }
  else planned_motor1 = 0;
}
//wait for a bit
//delay(2000);  //MAYBE SPLIT THE PROCEDURE INTO 2 PHASES: CHECKING AND EXECUTING

void execute_motor1()
{
  //read sensor again
  data1 = readSensor1();
  //check if planned movement is still correct after waiting
  if (data1 >= upperthres && planned_motor1 == 1)
  {
    //enable motor timer
    motor_time_flag = 1;
    hingeCW();
    //continually check sensor and count down to stop motor
    while (data1 >= upperthres && time_out_flag == 0)
    {
      data1 = readSensor1();
    }
    motor_time_counter = 0;
    motor_time_flag = 0;
    time_out_flag = 0;
    stopHingeMotor();
  }
  else if (data1 <= lowerthres && planned_motor1 == 2)
  {
    //enable motor timer
    motor_time_flag = 1;
    hingeCCW();
    //continually check sensor and count down to stop motor
    while (data1 <= lowerthres && time_out_flag == 0)
    {
      data1 = readSensor1();
    }
    //reset timer  parameters incase the loop exits because hinge reaches correct position
    motor_time_counter = 0;
    motor_time_flag = 0;
    time_out_flag = 0;
    stopHingeMotor();
  }
  //if planned movement does not match, reset everything
  else planned_motor1 = 0;
}

/*END OF MOTOR CONTROL*/

hingeCW(); and hingeCCW(); are procedure for turning the motor left and right respectively. My problem is how to turn the panel in the initial position (for example at the end of the day, when sun goes down and its dark, I like the motor turn the panel in the initial position).

Any help?
Thanks

warm gear motor or worm gear motor?

Sorry its worm 12V DC motor

You need to have an external switch to detect when the mechanism is at the HOME position. That could be a simple microswitch of some form of optical detector. The Arduino would keep checking the switch while the motor is moving and would stop the motor when the switch is triggered.

...R

Well I need to bring the motor in initial position (home) after let we say one hour (10 hours , or any time ) doesnt meter if it still moving or not.

So , from example in the morning the panels are in their initial (home, horizontal position) and start tracking the sun and motor is moving. That at the end of the day motor should bring the panel in initial (home, horizontal position) and keep that till next day morning. So I just wont to bring the panel to initial position based on the timer(external real time clock or internal) , so after let we say for example 10 hours need to turn the panel to initial position

astronaut71:
So I just wont to bring the panel to initial position based on the timer(external real time clock or internal) , so after let we say for example 10 hours need to turn the panel to initial position

Did you read Reply #3 ?

...R

You are focused on the motor, but what you want to do isn't bring back the motor to its start position, but the mechanism that controls the solar panel.
So what Robin2 has suggested really is the best thing to do.
This kind of thing has been used thousands if not millions of times already, in satellite dish positioner devices for instance.

You could use a magnetometer (so long as not too close to any motors / steelwork), which gives
compass heading. Solid state, reliable (until someone brings a big magnet nearby!)

You can use a heavy duty switch and diodes to stop the motor drive when the switch opens up like in the attached pix.

reversemotor.jpg

Robin2:
You need to have an external switch to detect when the mechanism is at the HOME position. That could be a simple microswitch of some form of optical detector. The Arduino would keep checking the switch while the motor is moving and would stop the motor when the switch is triggered.

...R

But I need to bring the panel to its home position (means stop the motor and turn the motor left or right so the panel is it its home , initial position)

Is this micro switch a good option? Mini Microswitch - SPDT (Lever) - COM-12742 - SparkFun Electronics

That is exactly the sort of thing you need.

astronaut71:
But I need to bring the panel to its home position (means stop the motor and turn the motor left or right so the panel is it its home , initial position)

Then maybe you need a switch at each end. When the Arduino detects that the (say) right-hand switch is pressed it runs the code to move the motor back to the left-hand HOME position.

...R

I like to do it automatically. So let we say after 10 hours operations i like that panel goes to its initial (home) position without pressing any button. Is that ok?Any code helping with this?

astronaut71:
I like to do it automatically. So let we say after 10 hours operations i like that panel goes to its initial (home) position without pressing any button. Is that ok?Any code helping with this?

Then you just need to keep track of the time that has elapsed. If it doesn't need to be too precise you can use millis(). 1000 * 60 * 60 * 10 is the number of millisecs in 10 hours (I hope ).

The demo several things at a time shows how to use millis()

If you need time keeping as accurate as a clock then you will need a Real Time Clock (RTC) module.

I have the feeling that you may need to approach the organization of your program more methodically. Have a look at planning and implementing a program - which also uses millis() to manage timing.

...R

Any help or tutorial how to connect mini switch like this http://www.jaycar.com.au/Electromechanical-Components/Switches/Micro-Switches/Microswitch-SPDT-ON-ON/p/SM1039 ?

So with this mini switch I will be able automatically bring the panel to initial position?

That is just an ordinary switch. Use INPUT_PULLUP on one of the I/O pins and connect the NO (Normally Open) connection between the pin and GND.

If you are not sure which is the NO connection you can easily test it with your multimeter.

...R

Ok. Thanks Robin-2. Any help on some example code?

astronaut71:
Ok. Thanks Robin-2. Any help on some example code?

Several things at a time
Planning and implementing a program

I think it is time for you to put in a little hard work.

...R