Created
December 22, 2013 20:30
-
-
Save nazrdogan/8088023 to your computer and use it in GitHub Desktop.
Arduino Solar tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int LDR1 = A0; | |
| int LDR2 = A1; | |
| int LDR3 = A2; | |
| int LDR4 = A3; | |
| int pos = 30; | |
| int a=0; | |
| int motorPin1 = 8; | |
| int motorPin2 = 9; | |
| int motorPin3 = 10; | |
| int motorPin4 = 11; | |
| int delayTime = 10; | |
| #include <Servo.h> | |
| Servo myservo; | |
| int top=0; | |
| int down=0; | |
| int right=0; | |
| int left=0; | |
| void setup(){ | |
| Serial.begin(9600); | |
| myservo.attach(14); | |
| pinMode(motorPin1, OUTPUT); | |
| pinMode(motorPin2, OUTPUT); | |
| pinMode(motorPin3, OUTPUT); | |
| pinMode(motorPin4, OUTPUT); | |
| } | |
| void clockwise() | |
| { | |
| a=a--; | |
| Serial.println(a); | |
| digitalWrite(motorPin1, HIGH); | |
| digitalWrite(motorPin2, LOW); | |
| digitalWrite(motorPin3, LOW); | |
| digitalWrite(motorPin4, LOW); | |
| delay(delayTime); | |
| digitalWrite(motorPin1, LOW); | |
| digitalWrite(motorPin2, HIGH); | |
| digitalWrite(motorPin3, LOW); | |
| digitalWrite(motorPin4, LOW); | |
| delay(delayTime); | |
| digitalWrite(motorPin1, LOW); | |
| digitalWrite(motorPin2, LOW); | |
| digitalWrite(motorPin3, HIGH); | |
| digitalWrite(motorPin4, LOW); | |
| delay(delayTime); | |
| digitalWrite(motorPin1, LOW); | |
| digitalWrite(motorPin2, LOW); | |
| digitalWrite(motorPin3, LOW); | |
| digitalWrite(motorPin4, HIGH); | |
| delay(delayTime); | |
| } | |
| void counterClockwise(){ | |
| a=a++; | |
| Serial.println(a); | |
| digitalWrite(motorPin1, LOW); | |
| digitalWrite(motorPin2, LOW); | |
| digitalWrite(motorPin3, LOW); | |
| digitalWrite(motorPin4, HIGH); | |
| delay(delayTime); | |
| digitalWrite(motorPin1, LOW); | |
| digitalWrite(motorPin2, LOW); | |
| digitalWrite(motorPin3, HIGH); | |
| digitalWrite(motorPin4, LOW); | |
| delay(delayTime); | |
| digitalWrite(motorPin1, LOW); | |
| digitalWrite(motorPin2, HIGH); | |
| digitalWrite(motorPin3, LOW); | |
| digitalWrite(motorPin4, LOW); | |
| delay(delayTime); | |
| digitalWrite(motorPin1, HIGH); | |
| digitalWrite(motorPin2, LOW); | |
| digitalWrite(motorPin3, LOW); | |
| digitalWrite(motorPin4, LOW); | |
| delay(delayTime); | |
| } | |
| void loop() | |
| { | |
| int LDRReading1 = analogRead(LDR1); | |
| int LDRReading2= analogRead(LDR2); | |
| int LDRReading3= analogRead(LDR3); | |
| int LDRReading4= analogRead(LDR4); | |
| left=(LDRReading1+LDRReading3)/2; | |
| right=(LDRReading2+LDRReading4)/2; | |
| top=(LDRReading1+LDRReading2)/2; | |
| down=(LDRReading3+LDRReading4)/2; | |
| if (top>down){ | |
| myservo.write(pos); | |
| pos=--pos; | |
| delay(5); | |
| } | |
| else if(down>top) | |
| { | |
| myservo.write(pos); | |
| pos=++pos; | |
| delay(5); | |
| } | |
| if(pos>180){ | |
| pos=180; | |
| } | |
| if(pos<30){ | |
| pos=30; | |
| } | |
| if(right>left){ | |
| clockwise(); | |
| } | |
| if(left>right) | |
| { | |
| counterClockwise(); | |
| } | |
| //Serial.print("--right=="); | |
| // Serial.print(right); | |
| // Serial.print("--left=="); | |
| //Serial.println(left); | |
| delay(30); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment