/* Solar Tracker Code with 4 buttons for manual orientation and
4 ldr sensors to automaticaly traking the sun.
*/
//set constant pin numbers
int wbuttonPin = 4;
int ebuttonPin = 2;
int tbuttonPin = 8;
int bbuttonPin = 12;
int switch1Pin = 7;
int switch2Pin = 5;
int eldr = A0;
int wldr = A1;
int tldr = A2;
int bldr = A3;
int IN1 = 13;
int IN2 = 9;
int IN3 = 10;
int IN4 = 11;
int eastldr = 0;
int westldr = 0;
int topldr = 0;
int botldr = 0;
int error = 0;
int poserror = 0;
int tberror = 0;
int tbposerror = 0;
int westbutton = 0;
int eastbutton = 0;
int topbutton = 0;
int botbutton = 0;
int onoffswitch = 0;
int singledualswitch = 0;
void setup() {
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A3, INPUT);
pinMode (A4, INPUT);
pinMode (8, INPUT_PULLUP);
pinMode (12, INPUT_PULLUP);
pinMode (13, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
pinMode (4, INPUT_PULLUP);
pinMode (2, INPUT_PULLUP);
pinMode (5, INPUT_PULLUP);
pinMode (7, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {Serial.print("start programm");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);
while (onoffswitch == LOW ) {
//manual rotating of the panel
eastldr = analogRead(A0);
westldr = analogRead(A1);
topldr = analogRead(A2);
botldr = analogRead(A3);
eastbutton = digitalRead(ebuttonPin);
if (eastbutton == HIGH)
{ digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} // rotate to east side when you push the right button
else {
digitalWrite(IN1, LOW);
}
westbutton = digitalRead(wbuttonPin);
if (westbutton == HIGH)
{ digitalWrite(IN2, HIGH);
} //rotate to west side when you push the left button
else {
digitalWrite(IN2, LOW);
}
topbutton = digitalRead(tbuttonPin);
if (topbutton == HIGH)
{ digitalWrite(IN4, HIGH);
} //rotate to west side when you push the left button
else {
digitalWrite(IN4, LOW);
}
botbutton = digitalRead(bbuttonPin);
if (botbutton == HIGH)
{ digitalWrite(IN3, HIGH);
} //rotate to west side when you push the left button
else {
digitalWrite(IN3, LOW);
} onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);
Serial.println(eastldr);
Serial.println(westldr);
Serial.println(topldr);
Serial.println(botldr);
}
//automaticaly rotating of the panel
//check east-west orientation first
/* if (eastldr < 400 && westldr < 400)
{digitalWrite(IN1,HIGH)
delay(7500)
digitalWrite(IN1,LOW)}*/
onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);
while (onoffswitch == HIGH && singledualswitch == LOW ){
eastldr = analogRead(A0);
westldr = analogRead(A1);
topldr = analogRead(A2);
botldr = analogRead(A3);
error = (((eastldr+topldr)/2) - ((westldr+botldr)/2));
poserror = abs(error);
if (poserror > 10){
if (error > 0) {
digitalWrite(IN2, HIGH);
// Serial.print("IN2 HIGH");
}
else if (error < 0) {
digitalWrite(IN1, HIGH);
//Serial.print("IN1 HIGH");
}}
else if (poserror <= 10 ) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
//Serial.print("LOW BOTH");
}
Serial.println(eastldr);
Serial.println(westldr);
Serial.println(topldr);
Serial.println(botldr);
onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);}
onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);
while (onoffswitch == HIGH & singledualswitch == HIGH ){
topldr = analogRead(A2);
botldr = analogRead(A3);
eastldr = analogRead(A0);
westldr = analogRead(A1);
tberror = (((topldr+westldr)/2) - ((botldr+eastldr)/2));
tbposerror = abs(tberror);
error = (((eastldr+topldr)/2) - ((westldr+botldr)/2));
poserror = abs(error);
if (poserror > 10){
if (error > 0) {
digitalWrite(IN2, HIGH);
// Serial.print("IN2 HIGH");
}
else if (error < 0) {
digitalWrite(IN1, HIGH);
//Serial.print("IN1 HIGH");
}}
else if (poserror <= 10 ) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
Serial.println(eastldr);
Serial.println(westldr);
if (tbposerror > 10)
{ if (tberror > 0) {
digitalWrite(IN4, HIGH);
} else if (tberror < 0) {
digitalWrite(IN3, HIGH);
}
}
else if (poserror <= 10 ) {
digitalWrite(IN4, LOW);
digitalWrite(IN3, LOW);
}
onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);}
onoffswitch = digitalRead(switch1Pin);
singledualswitch = digitalRead(switch2Pin);
delay(200);
}
Comments