Solar Tracker Problem

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:

  1. The solar calculator is working as intended; it is giving me the correct outputs.

Negative side:

  1. One of my servos (the horizontal/servoEl) isn't responding.
  2. 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.
  3. 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.
  4. As of this moment, my Arduino isn't sending anything on the serial monitor.
  5. 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 :smiling_face_with_tear:

Servos cannot be powered from the Arduino 5V pin -- you can damage the Arduino or computer USB port just by trying. Ignore any tutorials that suggest this is OK.

Use a separate power supply (4xAA works for one or two small servos, like SG90), and don't forget to connect the grounds.

Forum members strongly prefer hand-drawn wiring diagrams. Please avoid Fritzing diagrams, as they are usually misleading, difficult to interpret and often wrong. The above is a rare exception.

ohh okay, thanks! My Arduino isn't sending anything on the serial monitor as of the moment, I'm afraid it's damaged already :smiling_face_with_tear:

Remove the servos and test the Arduino with a "Hello World" program.

2 Likes

Check that 6V batt connection, the brown wire (NEG ?) is connected to Arduino 5V.

1 Like

most of the libraries you use are common and easy to find

you should put the URL to the Solar Calculator library on the same line as #include <SolarCalculator.h>, behind a //, so we know which solar library you are using. this creates a link.

Solar Calculator was easy to find. the problem comes with I2CLib.h and lora.h. you can easily have more than one of those on one hard drive

1 Like

My Arduino is working fine with different codes but with the given code above the data isn't sending to the serial monitor. Could there be a problem with the code?

Ohh yeahh, it was supposed to be on the ground power. thanks.

If this does not print, you have something connected to pin 0 and/or pin 1.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.