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...
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.
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);
}
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++) {
analogRead(sensorPin[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] += analogRead(sensorPin[i]);
vals[i] /= sac;
}
int diff = vals[0] - vals[1];
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
}
}
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;
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.