Hi everybody,
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>
#include <Adafruit_MotorShield.h>
#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 > 90 - stop
//const int maxv = ? // to prevent overturning vertical
//const int minv = ?
void setup()
{
AFMS.begin();
stepperv->setSpeed(10);
stepperh->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 < 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
}