# 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 <Wire.h>

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 > 90 - stop
//const int maxv = ?    // to prevent overturning vertical
//const int minv = ?

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

void loop()
{

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

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

//VERTICAL MOVEMENT//
if((up > centre + tol) && (down + tol < centre))  // && (up - ? > tol)
{
stepperv->onestep(BACKWARD,DOUBLE);
}
else if((down + tol > centre) && (up + tol < centre))  // && (down - ? > tol)
{
stepperv->onestep(FORWARD,DOUBLE);
}
else{}  // do nothing
}
``````
• Well first you'll probably need to define your limits. I see you have `maxh` `minh` and so on.. how do you know when you've reached the max/min? May 10 '14 at 18:12
• Thanks for all the replies. Could i maybe use a magnetic switch or an accelerometer - something like this: Accelerometer? How accurate are these things? May 12 '14 at 9:31
• "how do you know when you've reached the max/min?"... You can create a routine of track angles. Check the maximun ammount of energy that the solar pan is getting. At this point, this is the best angle. At the end, this is the final goal. Do you have your project concluded? Apr 8 '16 at 15:35