top of page
Project Code

Below are the Program for our Senior Design project.

Solar Tracking Program Code
#include <Servo.h> // Currently for 4 solar tracker system const int debug = 0; const int numPanels = 4; // Motors - Directed by LDR Servo motors[numPanels * 2]; int motorPins[] = {8, 9, 10, 11, 12, 13, 14, 15}; // LoMotorOne, HiMotorOnce, etc... int ldrPins[] = {A0, A1, A2, A4, A5, A6, A8, A9, A10, A12, A13, A14}; // LeftLDROne, RightLDROne, TopLDROne, etc... // Bias indicates the observed difference between the installed light sensor pairs that needs to be corrected in code int leftBias[] = {-12, -9, 10, 7}; int topBias[] = {-24, -1, 10, 10}; // Lower motor position limits int loMotorPosL = 0; int loMotorPosH = 270; // Upper motor position limits int upMotorPosL = 45; int upMotorPosH = 135; // The minimum difference between light levels to trigger a movement int threshold = 5; void setup() { // Check to see if debug mode is enabled if (debug == 1) { Serial.begin(9600); } // Initialize the motors for each panel for (int i = 0; i < numPanels; i++) { // Lower Motors Servo low; low.attach(motorPins[i * 2]); motors[i * 2] = low; // Upper Motors Servo up; up.attach(motorPins[(i * 2) + 1]); motors[(i * 2) + 1] = up; } delay(500); } void loop() { // Adjust the position of each panel for (int i = 0; i < numPanels; i++) { movePanel(i); } delay(50); } void movePanel(int panelIndex) { // Read the current motor positions Servo loMotor = motors[panelIndex * 2]; Servo upMotor = motors[(panelIndex * 2) + 1]; int loMotorPos = loMotor.read(); int upMotorPos = upMotor.read(); // Read the LDR data and offset by the observed variances (a lower value indicates more light detected) int leftLDR = analogRead(ldrPins[panelIndex * 3]) + leftBias[panelIndex]; int rightLDR = analogRead(ldrPins[(panelIndex * 3) + 1]); int topLDR = analogRead(ldrPins[(panelIndex * 3) + 2]) + topBias[panelIndex]; int avgTop = topLDR; int avgBot = (leftLDR + rightLDR) / 2; // Bottom LDRs averaged together int avgLeft = leftLDR; int avgRight = rightLDR; // Print out the LDR and motor data when in debug mode if (debug == 1) { Serial.print(panelIndex); Serial.print(" Top: "); Serial.print(avgTop); Serial.print(" Left: "); Serial.print(avgLeft); Serial.print(" Right: "); Serial.print(avgRight); Serial.print(" loMotorPos: "); Serial.print(loMotorPos); Serial.print(" upMotorPos: "); Serial.println(upMotorPos); } // Adjust upper motor position if the difference between top and bottom LDRs is greater than the threshold if (avgBot - avgTop >= threshold) { // In this case the bottom LDRs read higher than the top LDR, so the panel needs to be moved up // Make sure that the motor is not moved past the maximum degree if (upMotorPos < upMotorPosH) { upMotor.write(upMotorPos + 1); } } else if (avgTop - avgBot >= threshold) { // In this case the top LDR reads higher than the bottom LDRs, so the panel needs to be moved down // Make sure that the motor is not moved past the minimum degree if (upMotorPos > upMotorPosL) { upMotor.write(upMotorPos - 1); } } }

Motor Testing Program Code
#include <Servo.h> int botServoPin=8; int botServoPos= 90; Servo botServo; int botUp = 1; int topServoPin=9; int topServoPos= 90; Servo topServo; int topUp = 1; int botServoPin1=10; int botServoPos1= 90; Servo botServo1; int botUp1 = 1; int topServoPin1=11; int topServoPos1= 90; Servo topServo1; int topUp1 = 1; void setup() { // put your setup code here, to run once: //Serial.begin(9600); botServo.attach(botServoPin); botServoPos = botServo.read(); topServo.attach(topServoPin); topServoPos = topServo.read(); botServo1.attach(botServoPin1); botServoPos1 = botServo1.read(); topServo1.attach(topServoPin1); topServoPos1 = topServo1.read(); } void loop() { // put your main code here, to run repeatedly: if (botServoPos < 113 && botUp == 1) { botServo.write(++botServoPos); botServoPos = botServo.read(); // delay(75); } else if (botServoPos >= 113) { botUp = 0; } if (botServoPos > 68 && botUp == 0) { botServo.write(--botServoPos); botServoPos = botServo.read(); delay(75); } else if (botServoPos <= 68) { botUp = 1; } if (topServoPos1 < 113 && topUp1 == 1) { topServo1.write(++topServoPos1); topServoPos1 = topServo1.read(); // delay(75); } else if (topServoPos1 >= 113) { topUp1 = 0; } if (topServoPos1 > 68 && topUp1 == 0) { topServo1.write(--topServoPos1); topServoPos1 = topServo1.read(); // delay(75); } else if (topServoPos1 <= 68) { topUp1 = 1; } if (botServoPos1 < 113 && botUp1 == 1) { botServo1.write(++botServoPos1); botServoPos1 = botServo1.read(); // delay(75); } else if (botServoPos1 >= 113) { botUp1 = 0; } if (botServoPos1 > 68 && botUp1 == 0) { botServo1.write(--botServoPos1); botServoPos1 = botServo1.read(); // delay(75); } else if (botServoPos1 <= 68) { botUp1 = 1; } if (topServoPos < 113 && topUp == 1) { topServo.write(++topServoPos); topServoPos = topServo.read(); // delay(75); } else if (topServoPos >= 113) { topUp = 0; } if (topServoPos > 68 && topUp == 0) { topServo.write(--topServoPos); topServoPos = topServo.read(); // delay(75); } else if (topServoPos <= 68) { topUp = 1; } delay(75); }

Light Sensor Program Code
int ldrPin; int ldrPin1; int ldrPin2;//the number of the LDR pin void setup() { Serial.begin(9600); } void loop() { ldrPin = analogRead(A0); ldrPin1 = analogRead(A1); ldrPin2 = analogRead(A2); Serial.print("A0 : "); Serial.print(ldrPin); Serial.print(" A1 : "); Serial.print(ldrPin1); Serial.print(" A2 : "); Serial.println(ldrPin2); delay(100); }

Data Logger Program Code
#include <SD.h> // Load the SD Library #include <SPI.h> // Load the SPI communication library #include <Wire.h> #include <Adafruit_INA260.h> Adafruit_INA260 ina260 = Adafruit_INA260(); float Voltage; // Variable for holding Voltage float Current; // Variable for holding Current float Power; //Variable for holding Power int chipSelect = 10; int ledPin = 8; File SolarPanelData; void sdFail() { Serial.println("Card failed, or not present."); digitalWrite(ledPin, HIGH); while (1); } void setup() { Serial.begin(9600); Wire.begin(); ina260.begin(); Serial.println("Initializing SD card..."); pinMode(10, OUTPUT); SD.begin(chipSelect); if (!SD.begin(chipSelect)) { sdFail(); } Serial.println("Card initialized."); } void loop() { Current = (ina260.readCurrent())*(0.001); Power = (ina260.readPower())*(0.001); Voltage = (Power/Current); SolarPanelData = SD.open("Data.csv",FILE_WRITE); if(SolarPanelData) { Serial.print("Current: "); Serial.print(Current); Serial.println(" A"); Serial.print("Voltage: "); Serial.print(Voltage); Serial.println(" V"); Serial.print("Power: "); Serial.print(Power); Serial.println(" W"); Serial.println(); SolarPanelData.print(Current); SolarPanelData.print(","); SolarPanelData.print(Voltage); SolarPanelData.print(","); SolarPanelData.println(Power); SolarPanelData.close(); delay(10000); } else { sdFail(); } }

©2021 by Senior Design Proposal. Proudly created with Wix.com

bottom of page