Browse code

Added constants; changed variables; renamed directory.

haywalk authored on 01/11/2020 00:52:46
Showing 1 changed files

1 1
similarity index 52%
2 2
rename from tracking_2020oct30/tracking_2020oct30.ino
3 3
rename to light-tracking-code/tracking_2020oct30.ino
... ...
@@ -3,14 +3,21 @@
3 3
  * Hayden Walker, 30 October 2020
4 4
  */
5 5
 
6
-#include <Servo.h>
6
+#include <Servo.h> // Include the Arduino servo library
7 7
 
8 8
 Servo myservo; // Create a new servo object
9
+
10
+/*
11
+ * Define constants
12
+ */
9 13
 #define servoPin 9 // Define 9 as being the servo pin
14
+#define startPosition 30 // Set the start position
15
+#define maxPosition 110 // Set maximum angle
16
+#define margin 30 // Set sensitivity
17
+
10 18
 
11 19
 void setup() {
12 20
   myservo.attach(servoPin); // Attach the servo to pin 9
13
-  int startPosition = 30; // Set the start position
14 21
   myservo.write(startPosition); // Start with the servo at the start position
15 22
 }
16 23
 
... ...
@@ -24,20 +31,17 @@ void adjust(int currentAng){
24 31
   int valueLeft = analogRead(A1);
25 32
 
26 33
   // Check if right has more light
27
-  if ( (valueRight - valueLeft >= 30) and (currentAng > 0) ) {
28
-    currentAng -= 1;
34
+  if ( (valueRight - valueLeft >= margin) and (currentAng > startPosition) ) {
35
+    currentAng -= 1; // Move right
29 36
   }
30 37
   // Check if left side has more light
31
-  else if ( (valueLeft - valueRight >= 30) and (currentAng < 130) ) {
32
-    currentAng += 1;
38
+  else if ( (valueLeft - valueRight >= margin) and (currentAng < maxPosition) ) {
39
+    currentAng += 1; // Move left
33 40
   }
34 41
 
35
-  // Move the servo to its new position
36
-  myservo.write(currentAng);
37
-
38
-  // Wait 100ms
39
-  delay(100);
42
+  myservo.write(currentAng); // Move the servo to its new position
43
+  delay(100); // Wait 100ms
40 44
 
41
-  // Recurse and set the new angle as the current one
45
+  // Recurse and make the new angle the current one
42 46
   adjust(currentAng);
43 47
 }