-
Notifications
You must be signed in to change notification settings - Fork 0
/
autopower.ino
191 lines (166 loc) · 4 KB
/
autopower.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address, number of columns and rows
double sensorValue1 = 0;
int val1[100];
int max_v1 = 0;
double VmaxD1 = 0;
double VeffD1 = 0;
double solarVoltage = 0;
double sensorValue2 = 0;
int val2[100];
int max_v2 = 0;
double VmaxD2 = 0;
double VeffD2 = 0;
double wapdaVoltage = 0;
double sensorValue3 = 0;
int val3[100];
int max_v3 = 0;
double VmaxD3 = 0;
double VeffD3 = 0;
double generatorVoltage = 0;
int current = 0;
int solarRelay = 5;
int wapdaRelay = 4;
int generatorRelay = 3;
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(115200);
pinMode(solarRelay, OUTPUT);
pinMode(wapdaRelay, OUTPUT);
pinMode(generatorRelay, OUTPUT);
digitalWrite(solarRelay, HIGH);
digitalWrite(wapdaRelay, HIGH);
digitalWrite(generatorRelay, HIGH);
}
void loop() {
// For Solar
for (int i = 0; i < 100; i++) {
sensorValue1 = analogRead(A0);
if (analogRead(A0) > 511) {
val1[i] = sensorValue1;
} else {
val1[i] = 0;
}
delay(1);
}
max_v1 = 0;
for (int i = 0; i < 100; i++) {
if (val1[i] > max_v1) {
max_v1 = val1[i];
}
val1[i] = 0;
}
if (max_v1 != 0) {
VmaxD1 = max_v1;
VeffD1 = VmaxD1 / sqrt(2);
solarVoltage = (((VeffD1 - 420.76) / -90.24) * -210.2) + 120;
} else {
solarVoltage = 0;
}
Serial.print("Solar Voltage: ");
Serial.print(solarVoltage);
VmaxD1 = 0;
// For Wapda
for (int i = 0; i < 100; i++) {
sensorValue2 = analogRead(A1);
if (analogRead(A1) > 511) {
val2[i] = sensorValue2;
} else {
val2[i] = 0;
}
}
max_v2 = 0;
for (int i = 0; i < 100; i++) {
if (val2[i] > max_v2) {
max_v2 = val2[i];
}
val2[i] = 0;
}
if (max_v2 != 0) {
VmaxD2 = max_v2;
VeffD2 = VmaxD2 / sqrt(2);
wapdaVoltage = (((VeffD2 - 420.76) / -90.24) * -210.2) + 120;
} else {
wapdaVoltage = 0;
}
Serial.print(" Wapda Voltage: ");
Serial.print(wapdaVoltage);
VmaxD2 = 0;
delay(100);
// For Generator
for (int i = 0; i < 100; i++) {
sensorValue3 = analogRead(A2);
if (analogRead(A2) > 511) {
val3[i] = sensorValue3;
} else {
val3[i] = 0;
}
delay(1);
}
max_v3 = 0;
for (int i = 0; i < 100; i++) {
if (val3[i] > max_v3) {
max_v3 = val3[i];
}
val3[i] = 0;
}
if (max_v3 != 0) {
VmaxD3 = max_v3;
VeffD3 = VmaxD3 / sqrt(2);
generatorVoltage = (((VeffD3 - 420.76) / -90.24) * -210.2) + 120;
} else {
generatorVoltage = 0;
}
Serial.print(" Generator Voltage: ");
Serial.println(generatorVoltage);
VmaxD3 = 0;
delay(100);
// Read the current sensor
current = analogRead(A3);
// Convert current reading to actual current value
float amps = ((current - 512) * 5.0) / 1024.0;
// Display voltage and current values on LCD
lcd.clear(); // Clear the display before updating
lcd.setCursor(0, 0);
lcd.print("Solar: ");
lcd.print(solarVoltage);
lcd.print("V ");
lcd.print(amps);
lcd.print("A");
lcd.setCursor(0, 1);
lcd.print("Wapda: ");
lcd.print(wapdaVoltage);
lcd.print("V ");
lcd.print(amps);
lcd.print("A");
lcd.setCursor(0, 2);
lcd.print("Gener: ");
lcd.print(generatorVoltage);
lcd.print("V ");
lcd.print(amps);
lcd.print("A");
// Check which source to use
lcd.setCursor(0, 3); // Set the cursor to the correct position
if (solarVoltage >= 170) {
digitalWrite(wapdaRelay, HIGH);
digitalWrite(generatorRelay, HIGH);
delay(2000);
digitalWrite(solarRelay, LOW);
lcd.print("Using Solar ");
} else if (wapdaVoltage >= 170) {
digitalWrite(solarRelay, HIGH);
digitalWrite(generatorRelay, HIGH);
delay(2000);
digitalWrite(wapdaRelay, LOW);
lcd.print("Using Wapda ");
} else {
digitalWrite(solarRelay, HIGH);
digitalWrite(wapdaRelay, HIGH);
delay(2000);
digitalWrite(generatorRelay, LOW);
lcd.print("Using Genera ");
}
delay(1000); // Delay to update the display
}