#include <Servo.h> // include Servo library
unsigned long previousMillis = 0;
const long interval = 1000; // serial print interval
Servo horizontal; // horizontal servo (LEFT <-> RIGHT)
int servoh = 90; // 90; // stand horizontal servo
int servohLimitHigh = 175; // this value needs to be checked prior to first start up
int servohLimitLow = 5; // this value needs to be checked prior to first start up
Servo vertical; // vertical servo (UP <-> DOWN)
int servov = 35; // 35; // stand vertical servo
int servovLimitHigh = 70; // this value needs to be checked prior to first start up
int servovLimitLow = 0; // this value needs to be checked prior to first start up
/*brown = GND
* red = VCC
* orange = Signal
*/
// LDR pin connections
// name = analogpin;
int ldrtl = 0; //LDR top left
int ldrtr = 1; //LDR top right
int ldrbl = 2; //LDR bottom left
int ldrbr = 3; //LDR bottom right
void setup() {
Serial.begin(9600);
horizontal.attach(10);
vertical.attach(11);
horizontal.write(180); // horizontal start up angle: 180° => east, 0° => west
vertical.write(35); // vertical start up angle: 45° => mid angle between high noon and sunset
delay(2000);
}
void loop() {
int tl = analogRead(ldrtl); // top left
int tr = analogRead(ldrtr); // top right
int bl = analogRead(ldrbl); // bottom left
int br = analogRead(ldrbr); // bottom rigt
// the more sunlight the lower the resistance the lower the analog reading the lower the voltage (example: direct sunlight => 100 lux, 79 Ohm, 0.04 Volt, 8)
// int dtime = analogRead(4)/20; // read potentiometers
// int tol = analogRead(5)/4;
int dtime = 100; // determines the interval between each run through and therefore the speed of movement
int tol = 20; // determines the adjustment tolerance
int avt = (tl + tr) / 2; // average value top (top left vs. top right)
int avb = (bl + br) / 2; // average value bottom (down left vs. down right)
int dvert = avt - avb; // difference TOP vs. BOTTOM
int avl = (tl + bl) / 2; // average value left (top left vs. down left)
int avr = (tr + br) / 2; // average value right (top right vs. down right)
int dhoriz = avl - avr;// difference LEFT vs. RIGHT
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.print("avt:");
Serial.print(avt);
Serial.print(" avb:");
Serial.print(avb);
Serial.print(" avl:");
Serial.print(avl);
Serial.print(" avr:");
Serial.print(avr);
Serial.print(" dhoriz:");
Serial.print(dhoriz);
Serial.print(" dvert:");
Serial.print(dvert);
/*Serial.print(" dtime:");
Serial.print(dtime);
Serial.print(" tol:");
Serial.print(tol);*/
Serial.print(" servoh:");
Serial.print(servoh);
Serial.print(" servov:");
Serial.print(servov);
Serial.println();
}
//----------------- VERTICAL (UP/DOWN) ---------------------------
if (dvert < -1 * tol || dvert > tol) { // if TOP/DOWN difference is smaller than tolerance OR TOP/DOWN difference is higher than tolerance
if (avt > avb) { // if TOP is brigher than BOTTOM
servov = ++servov; // then move panel upwards
if (servov > servovLimitHigh) { // if panel reaches TOP limit stop there
servov = servovLimitHigh;
}
}
else if (avt < avb) { // if top is darker than bottom
servov = --servov; // then move panel downwards
if (servov < servovLimitLow) { // if panel reaches bottom limit stop there
servov = servovLimitLow;
}
}
vertical.write(servov);
}
//----------------- HORIZONTAL (LEFT/RIGHT) ---------------------------
if (dhoriz < -1 * tol || dhoriz > tol) { // if LEFT/RIGHT difference is smaller than tolerance OR LEFT/RIGHT difference is higher than tolerance
if (avl > avr) { // if LEFT is brighter than RIGHT
servoh = --servoh; // then move panel to LEFT
if (servoh < servohLimitLow) { // if panel reaches LEFT limit stop there
servoh = servohLimitLow;
}
}
else if (avl < avr) { // if LEFT is darker than RIGHT
servoh = ++servoh; // then move panel to RIGHT
if (servoh > servohLimitHigh) { // if panel reaches RIGHT limit stop there
servoh = servohLimitHigh;
}
}
else if (avl = avr) {
// nothing
}
horizontal.write(servoh);
}
delay(dtime); // delay between every run through
}