Go Down

### Topic: Dual Axis Solar Tracking Using 4 ldr's (Read 1 time)previous topic - next topic

#### OhmsLawBreaker

##### Mar 24, 2013, 07:54 pm
Hi everyone,

Im currently working on a project using 4ldr's to track where the sun is strongest orientation is a north south east and west style. Im not too sure what sort of logic i should use for this type of layout? heres the code i have written:

#include <Servo.h>

Servo servoX;
Servo servoY;

int controlXLeft = 0;
int controlXRight = 1;

int controlYUP = 2;
int controlYDown = 3;

int posX = 0;
int posY = 0;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int pos4 = 0;

int servoXpos = 0;
int servoYpos = 0;

void setup ()
{
Serial.begin(9600);

servoX.attach(9);
servoX.write(90);
servoY.attach(11);
servoY.write(90);
}

void loop ()
{
Serial.print("Cell 1 = ");

Serial.print("Cell 2 = ");

Serial.print("Cell 3 = ");

Serial.print("Cell 4 = ");

pos1 = constrain (pos1, 32, 982);

pos2 = constrain (pos2, 8, 996);

pos3 = constrain (pos3, 0, 1000);

pos4 = constrain (pos4, 45, 996);

// Horizontal Control Structure

if (pos1 > pos2)
{
servoXpos = map(pos1, 32, 982, 0, 255);
posX = map(servoXpos, 0, 255, 0, 179);
servoX.write(posX);

Serial.print("Servo X Angle = ");
Serial.println(posX);
}

else if (pos2 > pos1)
{
servoXpos = map(pos2, 8, 996, 0, 255);
posX = map(servoXpos, 0, 255, 0, 179);
servoX.write(posX);

Serial.print("Servo X Angle = ");
Serial.println(posX);
}

else
{
servoX.write(posX);

Serial.print("Servo X Angle = ");
Serial.println(posX);
}

// Vertical Control Structure

if (pos3 > pos4)
{
servoYpos = map(pos3, 0, 1000, 0, 255);
posY = map(servoYpos, 0, 255, 0, 179);
servoY.write(posY);

Serial.print("Servo Y Angle = ");
Serial.println(posY);
}

else if (pos4 > pos3)
{
servoYpos = map(pos4, 45, 996, 0, 255);
posY = map(servoYpos, 0, 255, 0, 179);
servoY.write(posY);

Serial.print("Servo Y Angle = ");
Serial.println(posY);

}

else
{
servoY.write(posY);

Serial.print("Servo Y Angle = ");
Serial.println(posY);
}

delay(1000);

}

Now I'm not sure but my tracker seems to follow shadows more then sunlight, and its movement is not fluent and smooth...

any ideas ?

#### tobyb121

#1
##### Mar 24, 2013, 09:09 pm
In terms of it following shadows, I can't really say from your code, and I think it's probably your hardware, you might want to check your circuit eg. are you measuring the voltage across the series resistor or your LDR, I would also double check how the resistance changes with light.
In terms of the jerky movement, this is because your LDR will have quite a noisy signal, which will cause the voltage to jump around. The easiest way to deal with this is to average your samples, for example:
Code: [Select]
`servoXpos=servoXpos/2+map(pos1, 32, 982, 0, 255)/2;` You can tweak the values that you use, so you could do 2/3 and 1/3, as long as they add up to 1. The more you average the values, the smoother it becomes, but this comes at the cost of being less responsive, though for things like measuring the sun you could really make it heavily damped, this will also help get rid of random errors, like someone walking past and momentarily casting a shadow over your circuit. You really need to play with a few different values and see what works for you.

Go Up