-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolar chaser.ino
132 lines (116 loc) · 3.32 KB
/
solar chaser.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <Servo.h>
// motor/joystick constants
int enableAPin = 3;
int in1Pin = 4;
int in2Pin = 5;
int enableBPin = 8;
int in4Pin = 9;
int in3Pin = 10;
// solar tracker constants
Servo horizontal; // horizontal servo
int servoh = 90;
int servohLimitHigh = 20;
int servohLimitLow = 160;
Servo vertical; // vertical servo
int servov = 90;
int servovLimitHigh = 60;
int servovLimitLow = 120;
// photoresistor pin connections
int topleft = A2; // yellow
int topright = A1; // orange
int botleft = A3; // green
int botright = A4; // blue
void setup() {
pinMode(enableAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
horizontal.attach(12);
vertical.attach(13);
Serial.begin(9600);
}
void loop() {
motorControl();
// intiate servos
horizontal.write(80);
vertical.write(90);
solarTrack();
}
void motorControl() {
int motorPWMSpeed = 0;
int joystickValue = analogRead(A0); //Joystick gives values ranging from 0 to 1023. So we will consider center value as 512 and lets keep some deadband at center.
if (joystickValue >= 530) //This will move motor in forward direction
{
motorPWMSpeed = map(joystickValue, 530, 1023, 0, 255);
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
analogWrite(enableAPin, motorPWMSpeed);
digitalWrite(in4Pin, HIGH);
digitalWrite(in3Pin, LOW);
analogWrite(enableBPin, motorPWMSpeed);
}
else if (joystickValue <= 490) //This will move motor in reverse direction
{
motorPWMSpeed = map(joystickValue, 490, 0, 0, 255);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
analogWrite(enableAPin, motorPWMSpeed);
digitalWrite(in4Pin, LOW);
digitalWrite(in3Pin, HIGH);
analogWrite(enableBPin, motorPWMSpeed);
}
else //Stop the motor
{
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
digitalWrite(in4Pin, LOW);
digitalWrite(in3Pin, LOW);
}
}
void solarTrack() {
int lt = analogRead(topleft); // top left
int rt = analogRead(topright); // top right
int ld = analogRead(botleft); // down left
int rd = analogRead(botright); // down right
int dtime = 50; int tol = 90; // dtime=diffirence time, tol=toleransi
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1*tol > dvert || dvert > tol) {
if (avt > avd) {
servov = ++servov;
if (servov > servovLimitHigh)
{servov = servovLimitHigh;}
}
else if (avt < avd)
{servov= --servov;
if (servov < servovLimitLow)
{ servov = servovLimitLow;}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr) {
servoh = ++servoh;
delay(20);
if (servoh < servohLimitLow) {
servoh = servohLimitLow;
}
}
else if (avl < avr) {
servoh = --servoh;
delay(20);
if (servoh > servohLimitHigh) {
servoh = servohLimitHigh;
}
}
else if (avl = avr) {
delay(5000);
}
horizontal.write(servoh);
}
delay(dtime);
}