Hi, here is an updated code of what I am working. I am having several problems and on the verge of giving up lol.
Positive side:
- The solar calculator is working as intended; it is giving me the correct outputs.
Negative side:
- One of my servos (the horizontal/servoEl) isn't responding.
- I tested the system earlier, it was between 7:00 am - 5:00 pm supposedly the gear would move counter-clockwise but it is moving the opposite, and based on the serial monitor it is closing.
- Also did I do the mapping for moving the servos correctly? Correct me if I'm wrong. I can't seem to test since one servo isn't responding.
- As of this moment, my Arduino isn't sending anything on the serial monitor.
- I have a mini solar panels with ratings of 30V and 360mA. Getting the voltage of the solar through the voltage divider, my R1 is 50000 ohms, and my R2 of 10000ohms. I only have 10k ohms, and 220 ohms available on me and I am not sure how I would wire it.
#include <RTClib.h>
#include <Wire.h>
#include <SolarCalculator.h>
#include <TimeLib.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <time.h>
RTC_DS3231 rtc;
Servo servoAz; //horizontal
int servoAzPin = 9;
int servoh = 180;
int servohLimitHigh = 175;
int servohLimitLow = 5;
Servo servoEl; //Vertical
int servoElPin = 10;
int servov = 0;
int servovLimitHigh = 60;
int servovLimitLow = 0;
int value = 0;
float voltage;
float R1 = 50000.0;
float R2 = 10000.0;
// Location
double latitude = 14.335157;
double longitude = 120.968728;
int utc_offset = 8;
// Refresh interval, in seconds
int interval = 10;
int pos = 0;
int pos2 = 0;
int oldvalue;
int oldvalue2;
const int button1 = 6; // Red Wire
const int motorA = 7; // Black Wire
const int motorB = 8; // White Wire
const int ledPin = 13;
int buttonStateA = 0;
int currentHour = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
servoAz.write(180);
servoEl.write(0);
servoAz.attach(servoAzPin); // attach servo to pin 3
servoEl.attach(servoElPin); // attach servo to pin 5
// Set system time to compile time
//setTime(toUtc(compileTime()));
// Set time manually (hr, min, sec, day, mo, yr)
//setTime(22, 39, 0, 5, 4, 2023);
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
// get the current hour
DateTime now = rtc.now();
int hours = now.hour();
int minutes = now.minute();
int seconds = now.second();
int currentDay = now.day();
int currentMonth = now.month();
Serial.println("Current Time: ");
Serial.print(hours);
Serial.print(" : ");
Serial.print(minutes);
Serial.print(" : ");
Serial.print(seconds);
Serial.print(currentDay);
Serial.print(" / ");
Serial.print(currentMonth);
Serial.print(" / ");
Serial.print(now.year());
delay(1000);
bool panelsOpen = false; // set initial state of panels (assuming they are closed at startup)
if (digitalRead(button1) == HIGH) { // check if the open switch is triggered
panelsOpen = true; // update the state of the panels
Serial.println("Switch not triggered (Panels Open)");
digitalWrite(13, LOW); // turn off the LED light on pin 13
} else if (digitalRead(button1) == LOW) {
panelsOpen = false;
Serial.println("Switch triggered (Panels Closed)");
digitalWrite(13, HIGH); // turn on the LED light on pin 13
}
double transit, sunrise, sunset; // Event times, in hours (UTC)
double eq; // Equation of time, in minutes
double ra, dec, r; // Equatorial coordinates, in degrees and AUs
double az, el; // Horizontal coordinates, in degrees
//static unsigned long next_millis = 0;
time_t utc = ::now();
calcEquationOfTime(utc, eq);
calcEquatorialCoordinates(utc, ra, dec, r);
calcHorizontalCoordinates(utc, latitude, longitude, az, el);
calcSunriseSunset(utc, latitude, longitude, transit, sunrise, sunset);
// Print results
Serial.println(F("Sunrise: "));
printSunTime24h(sunrise + utc_offset);
Serial.print(F("Transit: "));
printSunTime24h(transit + utc_offset);
Serial.print(F("Sunset: "));
printSunTime24h(sunset + utc_offset);
Serial.print(F("Eq of time: "));
Serial.print(eq);
Serial.println(F(" min"));
Serial.print(F("RA: "));
Serial.print(degreesToHours(ra), 3);
Serial.print(F("h Dec: "));
Serial.print(dec);
Serial.print(F("° R: "));
Serial.print(r, 6);
Serial.println(F(" AU"));
Serial.print(F("Az: "));
Serial.print(az);
Serial.print(F("° El: "));
Serial.print(el);
Serial.println(F("°"));
// At every interval
//if (millis() > next_millis) {
if (now.hour() >= 7 && now.hour() < 17) { // if it's between 7:00 am and 5:00 pm, press the button to close the motor
if (!panelsOpen) {
Serial.println("Motor moving counter-clockwise");
Serial.print("Panels Opening...");
digitalWrite(motorA, HIGH); //COUNTER clockwise
digitalWrite(motorB, LOW);
delay(5000); // for 5 seconds
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
} else if (panelsOpen) {
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
}
double az, el;
// Calculate the solar position, in degrees
calcHorizontalCoordinates(utc, latitude, longitude, az, el);
int azPos = map(az, 0, 360, 0, 180);
int elPos = map(el, 0, 90, 0, 180);
azPos = constrain(azPos, 0, 180);
elPos = constrain(elPos, 0, 180);
Serial.println("Working...");
servoAz.write(azPos);
servoEl.write(elPos);
// Print time, date, and servo positions
Serial.print(" Az: ");
Serial.print(az);
Serial.print("° El: ");
Serial.print(el);
Serial.print("° Servo Az: ");
Serial.print(azPos);
Serial.print(" Servo El: ");
Serial.println(elPos);
delay(100);
} else { // if it's between 5:00 pm and 7:00 am, release the button to open the motor
//buttonStateA = digitalRead(button1);
if (panelsOpen) {
Serial.println("Motor moving clockwise");
Serial.print("Panels Closing...");
digitalWrite(motorA, LOW); //clockwise
digitalWrite(motorB, HIGH);
} else {
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
servoAz.write(0);
servoEl.write(0);
Serial.println("System at home mode...");
}
delay(100); // add a small delay to avoid bouncing
}
delay(1000);
value = analogRead(A0);
voltage = value * (5.0 / 1023) * ((R1 + R2) / R2);
Serial.print("Voltage = ");
Serial.println(voltage);
delay(1000);
}
time_t toUtc(time_t local) {
return local - utc_offset * 3600L;
}
double degreesToHours(double deg) {
return deg / 15;
}
// Code from JChristensen/Timezone Clock example
time_t compileTime() {
const uint8_t COMPILE_TIME_DELAY = 8;
const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
char chMon[4], *m;
tmElements_t tm;
strncpy(chMon, compDate, 3);
chMon[3] = '\0';
m = strstr(months, chMon);
tm.Month = ((m - months) / 3 + 1);
tm.Day = atoi(compDate + 4);
tm.Year = atoi(compDate + 7) - 1970;
tm.Hour = atoi(compTime);
tm.Minute = atoi(compTime + 3);
tm.Second = atoi(compTime + 6);
time_t t = makeTime(tm);
return t + COMPILE_TIME_DELAY;
}
void printSunTime24h(double hours) {
int m = int(round(hours * 60));
int hr = (m / 60) % 24;
int mn = m % 60;
printDigits(hr);
Serial.print(':');
printDigits(mn);
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
Here is the wiring diagram:
please be kind