Solar Tracker Code - how to limit steppers to max & min angle? a couple of friends and i are working on a double axis solar tracker with a Adafruit Motor-shield V2 and 2x Adafruit bipolar stepper motors (200/rev).
We got the following code so far but are stuck with getting the steppers to stop at a certain angle (90° for vertical and 180° for horizontal) so that we dont tangle up the wires and avoid unnecessary movement. Any help and sugestions would be much appreciated. 

Cheers

#include &lt;Wire.h&gt;
#include &lt;Adafruit_MotorShield.h&gt;
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *stepperh = AFMS.getStepper(200, 1);  // declaring horizontal stepper
Adafruit_StepperMotor *stepperv = AFMS.getStepper(200, 2);  // declaring vertical stepper

int ldr1 = A0;  // right ldr        
int ldr2 = A1;  // centre ldr
int ldr3 = A2;  // left ldr
int ldr4 = A3;  // top ldr
int ldr5 = A4;  // bottom ldr

const int threshold = 550;  // limit to stop tracker from moving at night

const int tol = 50;  // tolerance for ldrs // maybe vertical AND horizontal depending on difference?

//int = countv
//int = counth

//const int maxh = ?    // to prevent overturning horizontal (50 steps = 90°) 
//const int minh = ?    // combine with count int's // i.e. if counth &gt; 90 - stop
//const int maxv = ?    // to prevent overturning vertical
//const int minv = ?

void setup()
{
  AFMS.begin();
  stepperv-&gt;setSpeed(10);  
  stepperh-&gt;setSpeed(10);    
}

void loop()
{
  int right = analogRead(ldr1);  
  int centre = analogRead(ldr2);
  int left = analogRead(ldr3);
  int up = analogRead(ldr4);
  int down = analogRead(ldr5);

  //STOP MOVEMENT//
  if ((left &lt; threshold) &amp;&amp; (right &lt; threshold) &amp;&amp; (up &lt; threshold) &amp;&amp; (down &lt; threshold)) {}  // stop any movement if sensor values below threshold 
                                                                                               // at begining to check after every sensor reading

  //HORIZONTAL MOVEMENT//
  if((right &gt; centre + tol) &amp;&amp; (left + tol &lt; centre))   // &amp;&amp; (right - ? &gt; tol)     
  {
    stepperh-&gt;onestep(BACKWARD,DOUBLE);  // add +1 to int counth // if (minh &lt; counth &lt; maxh) - move // else - stop // counth=counth++ , counth=counth--                            
  }
  else if((left &gt; centre + tol) &amp;&amp; (right + tol &lt; centre))  // &amp;&amp; (left - ? &gt; tol)  
  {
    stepperh-&gt;onestep(FORWARD,DOUBLE);                      
  }
  else {}  // do nothing

  //VERTICAL MOVEMENT//
  if((up &gt; centre + tol) &amp;&amp; (down + tol &lt; centre))  // &amp;&amp; (up - ? &gt; tol)  
  {
    stepperv-&gt;onestep(BACKWARD,DOUBLE);   
  }
  else if((down + tol &gt; centre) &amp;&amp; (up + tol &lt; centre))  // &amp;&amp; (down - ? &gt; tol)  
  {
    stepperv-&gt;onestep(FORWARD,DOUBLE);  
  }
  else{}  // do nothing
}
The only sure way is to use one or more position or limit switches. Steps can be lost (motor didn't move as commanded due to, f/ex, overload), the motor could be back-driven while powered down or if over-powered by the mechanical system, or your step counting could be faulty due to a bug or a power glitch.

At power-up or at any time your software thinks it might have got out of sync with the mechanicals, back into the limit switch at low speed (so you don't hard-crash), and reset your software counters, and reposition from there.