Browse code

Initial commit

haywalk authored on 30/10/2020 21:24:46
Showing 2 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,3 @@
1
+# Solar Panel Project
2
+
3
+This is a project I'm working on in school, with the goal of making a sun-tracking solar panel. It's a work-in-progress, but here's the code.
0 4
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+/*
2
+ * Sun-following solar panel prototype source code
3
+ * Hayden Walker, 30 October 2020
4
+ */
5
+
6
+#include <Servo.h>
7
+
8
+Servo myservo; // Create a new servo object
9
+#define servoPin 9 // Define 9 as being the servo pin
10
+
11
+void setup() {
12
+  myservo.attach(servoPin); // Attach the servo to pin 9
13
+  int startPosition = 30; // Set the start position
14
+  myservo.write(startPosition); // Start with the servo at the start position
15
+}
16
+
17
+void loop() {
18
+  adjust(startPosition); // Start adjusting from the starting position
19
+}
20
+
21
+void adjust(int currentAng){
22
+  // Get sensor readings
23
+  int valueRight = analogRead(A0); 
24
+  int valueLeft = analogRead(A1);
25
+
26
+  // Check if right has more light
27
+  if ( (valueRight - valueLeft >= 30) and (currentAng > 0) ) {
28
+    currentAng -= 1;
29
+  }
30
+  // Check if left side has more light
31
+  else if ( (valueLeft - valueRight >= 30) and (currentAng < 130) ) {
32
+    currentAng += 1;
33
+  }
34
+
35
+  // Move the servo to its new position
36
+  myservo.write(currentAng);
37
+
38
+  // Wait 100ms
39
+  delay(100);
40
+
41
+  // Recurse and set the new angle as the current one
42
+  adjust(currentAng);
43
+}