Devices & Components
Arduino Nano
40 colored male-male jumper wires
10 jumper wires 150mm male
Hot Glue
parrallax servo
Popsicle Stick
5.5 V 1 watt solar panel
Foamboard
Hand-size cardboard box
Chopstick
Breadboard 100x70
Duct Tape
Hardware & Tools
Screwdrivers
Hot Glue Gun
Software & Tools
Arduino IDE
Project description
Code
Code
cpp
Code
1#include <Servo.h> 2 3 4// Define servos 5Servo horizontalServo; // Rotates base left/right 6Servo verticalServo; // Tilts panel up/down 7 8 9// LDR pin assignments 10const int ldr1 = A1; // top-left 11const int ldr2 = A2; // top-right 12const int ldr3 = A3; // bottom-left 13const int ldr4 = A4; // bottom-right 14 15 16// Servo positions 17int horizontalPos = 90; 18int verticalPos = 90; 19 20 21// Function to read averaged LDR value 22int readLDR(int pin) { 23 int total = 0; 24 for (int i = 0; i < 5; i++) { 25 total += analogRead(pin); 26 delay(2); 27 } 28 return total / 5; 29} 30 31 32void setup() { 33 // Attach servos 34 horizontalServo.attach(9); 35 verticalServo.attach(10); 36 37 38 // Start at center 39 horizontalServo.write(horizontalPos); 40 verticalServo.write(verticalPos); 41 42 43 // Start serial monitor 44 Serial.begin(9600); 45 delay(500); 46} 47 48 49void loop() { 50 // Read LDR values (averaged) 51 int val1 = readLDR(ldr1); // top-left 52 int val2 = readLDR(ldr2); // top-right 53 int val3 = readLDR(ldr3); // bottom-left 54 int val4 = readLDR(ldr4); // bottom-right 55 56 57 // Show readings 58 Serial.print("LDR1: "); Serial.print(val1); 59 Serial.print(" | LDR2: "); Serial.print(val2); 60 Serial.print(" | LDR3: "); Serial.print(val3); 61 Serial.print(" | LDR4: "); Serial.println(val4); 62 63 64 // Calculate averages 65 int topAvg = (val1 + val2) / 2; 66 int bottomAvg = (val3 + val4) / 2; 67 int leftAvg = (val1 + val3) / 2; 68 int rightAvg = (val2 + val4) / 2; 69 70 71 // Differences 72 int verticalDiff = topAvg - bottomAvg; 73 int horizontalDiff = leftAvg - rightAvg; 74 75 76 // Sensitivity threshold 77 int threshold = 30; 78 79 80 // Vertical tracking 81 if (abs(verticalDiff) > threshold) { 82 if (verticalDiff < 0 && verticalPos < 180) { // MORE light at bottom → TILT DOWN 83 verticalPos--; 84 } else if (verticalDiff > 0 && verticalPos > 0) { // MORE light at top → TILT UP 85 verticalPos++; 86 } 87 verticalServo.write(verticalPos); 88 } 89 90 91 // Horizontal tracking 92 if (abs(horizontalDiff) > threshold) { 93 if (horizontalDiff < 0 && horizontalPos < 180) { // MORE light at right → ROTATE RIGHT 94 horizontalPos++; 95 } else if (horizontalDiff > 0 && horizontalPos > 0) { // MORE light at left → ROTATE LEFT 96 horizontalPos--; 97 } 98 horizontalServo.write(horizontalPos); 99 } 100 101 102 delay(30); // Smoother tracking 103}
Documentation
Circuit diagram
Mock circuit diagram using tinkercad
IMG_1861.png

Comments
Only logged in users can leave comments