Hey i recently viewed this video on youtube. Sun Tracking Solar Panel w/ Arduino - Powers ITSELF!!! - YouTube
I am not a smart person when it comes to code. Please if you can find this code that does exactly this or if you can try to make it yourself that would be great, because i really needed something like this...

:::::))))))))))))Robotkid249

If you freeze frame the video at about 37 seconds it looks like there are three light dependent resistors on the little brown board, one facing forward, one to the left a bit and one to the right. I guess the system constantly reads the light level from each of the three sensors, works out which one is brightest and turns a motor to rotate the whole assembly in that direction. When the middle sensor reads the most light it stops turning 'cause it's facing the sun.

Just my guess...

Andrew

yes i think that sounds right. ill try to create the code when i get a chance.

I finished the code, sadly all of the four servos i have are hacked, and i cant try it until i get some un-hacked servos. I compiled the program and it says it's fine. If you are a code genius please tell me that the code is all right. Thanks robotkid249

int servoPin = 7;
int servoPin1 = 4;
int myAngle;
int pulseWidth;
int pulseWidth1;
int myAngle1;
int val = 0;
int val2 = 1;
int val3 = 2;

void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 11) + 500; // converts angle to microseconds
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount
digitalWrite(servoPin, LOW); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}
void servoPulse1(int servoPin1, int myAngle1) {
pulseWidth1 = (myAngle1 * 11) + 500; // converts angle to microseconds
digitalWrite(servoPin1, HIGH); // set servo high
delayMicroseconds(pulseWidth1); // wait a very small amount
digitalWrite(servoPin1, LOW); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}

void setup() {
pinMode(servoPin, OUTPUT); // set servoPin pin as output
pinMode(servoPin1, OUTPUT);
pinMode(val, INPUT);
pinMode(val2, INPUT);
pinMode(val3, INPUT);
}

void loop() {
if(val > val2){
for (myAngle=0; myAngle=10; myAngle++) {
(servoPin, myAngle);
}
}
if(val < val2){
for (myAngle=10; myAngle=0; myAngle++) {
(servoPin, myAngle);
}
}
if(val3 > val2){
for (myAngle=0; myAngle=0; myAngle++) {
(servoPin, myAngle);
}
}
if(val3 > val){
for (myAngle=0; myAngle=0; myAngle++) {
(servoPin, myAngle);
}
}
delay(300);
}

anyone please i really need this to work. thanks

hm
ur code wont work, because:

if(val > val2){
for (myAngle=0; myAngle=10; myAngle++) {

"myAngle=10" is always true so that this for-loop runs forever...
furthermore u do not read the analog pins...
and u never call servoPulse()

how do u connect the servo to the arduino? do u use a driver?
the arduino output pins can just do 40mA each...
a L293 helps here...

this is how i would do it:

``````int servoPin = 7;
int servoPin1 = 4;
int sc = 3; // sensor count
int sensorPin[sc] = {0,1,2}; // analog pins 0, 1 and 2

// just one function for both servo pins
void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 11) + 500;  // converts angle to microseconds
digitalWrite(servoPin, HIGH);       // set servo high
delayMicroseconds(pulseWidth);      // wait a very small amount
digitalWrite(servoPin, LOW);        // set servo low
delay(20);                          // refresh cycle of typical servos (20 ms)
}

// setup(): like u did it
void setup() {
pinMode(servoPin, OUTPUT);          // set servoPin pin as output
pinMode(servoPin1, OUTPUT);
for (int i=0; i<sc; i++) {
pinMode(sensorPin[i], INPUT);
digitalWrite(sensorPin[i]+14,LOW); // make sure that pullup is off
}
analogReference(DEFAULT);
}

void loop() {
int vals[sc];
for (int i=0; i<sc; i++) {
delay(1); // wait for the signal to settle
const int sac = 16;
vals[i] = 0;
for (int j=sac; j>0; j--)
vals[i] /= sac;
}
int diff = vals - vals;
if (diff > 10)
servoPulse(servoPin,diff);
else if (diff < -10)
servoPulse(servoPin1,-diff);
else { // we just have a small difference between the left and right sensor
delay(20);
// maybe u want to make sure, that the middle sensor has nearly the same value... i mean: possibly the analog pins have different offsets or the sensors r not really the same
}
}
``````

I tries my own version of the code based on this .... http://dukevisualstudies.org/lastmiles/?p=11

Now i tested it and the servo moves and its not supposed to move because the LDR sensors are not active. (i have not connected the sensors yet) So i do not know if theres a bug in my code, or there is some ambient data randomly passing through the serial port. So if i connect my sensors should this work fine or is it my code??

int Hservo = 7;
int Vservo = 6;
int Lsensor = 0;
int Msensor = 1;
int Rsensor = 2;

void setup()
{

beginSerial(9600);
pinMode(Lsensor, INPUT);
pinMode(Msensor, INPUT);
pinMode(Rsensor, INPUT);
pinMode(Hservo, OUTPUT);
pinMode(Vservo, OUTPUT);
}
void loop()
{
int temp;

if(Lsensor > Rsensor){
for (temp = 0; temp <= 200; temp++)
digitalWrite(Vservo,HIGH);
delayMicroseconds(1800); // 1.8ms moves the servo clockwise
digitalWrite(Vservo,LOW);
delay(20); // 20ms

}

if(Lsensor < Rsensor){
for (temp = 0; temp <= 200; temp++)
digitalWrite(Vservo,HIGH);
delayMicroseconds(1200); // 1.2ms moves the servo counterclockwise
digitalWrite(Vservo,LOW);
delay(20); // 20ms
}

if(Lsensor < Msensor){
for (temp = 0; temp <= 200; temp++)
digitalWrite(Vservo,LOW);
delay(20); // 20ms
}

if(Rsensor < Msensor){
for (temp = 0; temp <= 200; temp++)
digitalWrite(Vservo,LOW);
delay(20); // 20ms
}

if(Lsensor > Rsensor){
for (temp = 0; temp <= 200; temp++)
digitalWrite(Vservo,HIGH);
delayMicroseconds(1800); // 1.8ms moves the servo clockwise
digitalWrite(Vservo,LOW);
delay(20); // 20ms
}
}

For starters you appear to be comparing the pin numbers you are going to attach sensors to:

``````if(Lsensor > Rsensor)
``````

rather than comparing readings from those pins:

``````if(analogRead(Lsensor) > analogRead(Rsensor))
``````

and if you have nothing physically connected to those pins then the readings may float all over the place anyway.

Andrew

i guess two LDRs are enough, don't know why you want to have 3.
another idea: make it time dependent and program the same process for every day. sun takes same path every day, well depends where you live it might have different variation during the year. that way you only need one resistor to trigger the start of the process when the sun comes up.
i think the setup in the video might consume more energy that the panels produce, cause it regulates the whole time like crazy.