Hi all,
I'm building a mini dual axis solar tracker which will be up scaled to full size once I have this code working as required.
For a start I'm using an Arduino Nano clone but will most likely upgrade to a Mega when I run out of pins on the nano and need a reliable micro processor. I'm using mini gear motors for actuators, a Pololu dual motor driver and 4 LDR sensors + 4 10K resistors.
This is just the start of the sketch and I plan on introducing limit switches, LED's and a wind sensor for safety after I get past what I'm stuck on now.
As per usual I found a sketch that looks close to what I want and modified it to suit my needs.
What I'd like to implement on this sketch is a timed "MotorSeek", say... 2 sec where the motors are aloud to set the position of the LDR's directly facing the sun.
Then a timed "MotorStop" for about 6 sec where the motors are shut off.
Then 2 sec for adjustment and 6 sec switch off, and so on.
In the full scale version these times would be more like 1 min Seek time and 20-30 mins Stop time.
I would like to use "millis" NOT delay. I don't no where to insert this line or if indeed it's the correct line to useif ((unsigned long)(currentMillis - previousMillisSeek) >= MotorsSeek)
and or if ((unsigned long)(currentMillis - previousMillisStop) >= MotorsStop)
Here is what I have so far.
/* Dual axis solar tracker using 12v DC motor linier actuators,
4 LDR's, 4 limit switches and a wind sensor */
//LDR PINS
const int LeftTopLDR = A0;
const int RightTopLDR = A1;
const int LeftBottomLDR = A2;
const int RightBottomLDR = A3;
//MOTOR DRIVER PINS
const int AIN1 = 4;
const int AIN2 = 5;
const int APWM = 6;
const int BIN1 = 7;
const int BIN2 = 8;
const int BPWM = 9;
const int StandBy = 10;
//LED PINS
const int MovementLimitLED = 3;
//WIND SENSOR
const int WindSensor = 13;
//LIMIT SWITCH PINS
const int HorizontalLimitSwitch1 = 11;
const int HorizontalLimitSwitch2 = 12;
const int VerticalLimitSwitch1 = 1;
const int VerticalLimitSwitch2 = 2;
//MOTORS SEEK AND STOP VARIABLES
unsigned long previousMillisStop = 0;
unsigned long previousMillisSeek = 0;
unsigned long motorsStop = 6000;
unsigned long motorsSeek = 2000;
void setup()
{
Serial.begin(9600);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(APWM, OUTPUT);
pinMode(BPWM, OUTPUT);
pinMode(StandBy, OUTPUT);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
digitalWrite(APWM, LOW);
digitalWrite(BPWM, LOW);
digitalWrite(StandBy, LOW);
}
void loop()
{
unsigned long currentMillis = millis();
// LimitReset();
// FlatMode();
// Debug();
//LDR's READ AND CALCULATE
int lt = analogRead(LeftTopLDR);
int rt = analogRead(RightTopLDR);
int lb = analogRead(LeftBottomLDR);
int rb = analogRead(RightBottomLDR);
int avt = (lt + rt) / 2; // average value top
int avb = (lb + rb) / 2; // average value bottom
int avl = (lt + lb) / 2; // average value left
int avr = (rt + rb) / 2; // average value right
int dvert = avt - avb; // check the diffirence of top and bottom
int dhoriz = avl - avr;// check the diffirence of left and rigt
int tolerance = 10;
// check if the diffirence is in the tolerance else change vertical angle
if (-1*tolerance > dvert || dvert > tolerance)
{
if (avt > avb)
{
digitalWrite(StandBy, HIGH);
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
analogWrite(APWM, 180);
}
else if (avt < avb)
{
digitalWrite(StandBy, HIGH);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
analogWrite(APWM, 180);
}
else if (avt = avb)
{
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(APWM, LOW);
}
// check if the diffirence is in the tolerance else change horizontal angle
if (-1*tolerance > dhoriz || dhoriz > tolerance)
{
if (avl > avr)
{
digitalWrite(StandBy, HIGH);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
analogWrite(BPWM, 180);
}
else if (avl < avr)
{
digitalWrite(StandBy, HIGH);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
analogWrite(BPWM, 180);
}
else if (avl = avr)
{
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
digitalWrite(BPWM, LOW);
}
}
}
}