2

I have been working on this program for a few weeks now and finally got it somewhere near finishing. It uploads and seems to do what I want it to do on a R3 board but will not run on my R4 Wifi board although it says upload is successful.

The serial monitor shows nothing.

I think its something to do with the DS1307 library, as I have tried changing the library to DS3232 which improved the situation and the program did run, But I was unable to get the board to read the RTC. The RTC I am using is the DS3231. What could be causing the issue?

    #include <SolarPosition.h>
    #include <DS1307RTC.h>
#define MOTOR_ON LOW
#define MOTOR_OFF HIGH

const uint8_t digits = 3;


//SolarPosition Home(134.898551, -25.539774);  // Home//RTC CODE
SolarPosition Home(32.898551, -2.539774);  // Home//RTC CODE


const int azEncoder_a = 2;  // Green - pin 2 - Digital
const int azEncoder_b = 4;  // White - pin 4 - Digital
long azEncoder = 0;

const int elEncoder_a = 3;  // Green - pin 3 - Digital
const int elEncoder_b = 5;  // White - pin 5 - Digital
long elEncoder = 0;


const int azLimitSwitchPin = A0;  // DEFINE PIN TO USE FOR AZIMUTH LIMIT SWITCH
const int elLimitSwitchPin = A1;  // DEFINE PIN TO USE FOR AZIMUTH LIMIT SWITCH
const int azMotorPinPositive = 12;
const int azMotorPinNegative = 11;
const int elMotorPinPositive = 10;
const int elMotorPinNegative = 9;

bool nightime;
bool daytime;
bool trackerAzimuthRunning = false;
bool trackerElevationRunning = false;
bool sunRising = false;

int azLimitSwitch = 0;  // VARIABLE TO HOLD THE ABOVE VALUE
int elLimitSwitch = 0;
int hours;
int minutes;
int seconds;
int trackerAzimuthAngle;
int trackerElevationAngle;
int start = 0;
int azimuthSun_position;
float sunElevation;
float newPosition;
float oldPosition;


void setup() {
  Serial.begin(9600);

  pinMode(azEncoder_a, INPUT_PULLUP);
  pinMode(azEncoder_b, INPUT_PULLUP);

  pinMode(elEncoder_a, INPUT_PULLUP);
  pinMode(elEncoder_b, INPUT_PULLUP);

  attachInterrupt(0, azEncoderPinChangeA, RISING);
  attachInterrupt(1, elEncoderPinChangeB, RISING);

  pinMode(azLimitSwitchPin, INPUT);  // LIMIT SWITCH
  pinMode(elLimitSwitchPin, INPUT);  // LIMIT SWITCH

  pinMode(azMotorPinPositive, OUTPUT);
  pinMode(azMotorPinNegative, OUTPUT);
  pinMode(elMotorPinPositive, OUTPUT);
  pinMode(elMotorPinNegative, OUTPUT);


  SolarPosition::setTimeProvider(RTC.get);
}

void loop() {

  rtcCode();

  trackerAzimuthAngle = azEncoder / 1.67;    //This change to degrees
  trackerElevationAngle = elEncoder / 1.67;  // This changes to degrees
  azLimitSwitch = digitalRead(azLimitSwitchPin);
  elLimitSwitch = digitalRead(elLimitSwitchPin);

  if (newPosition &gt; oldPosition) {
    Serial.print(&quot;Going Up&quot;);
    Serial.print('\n');
    sunRising = true;
  }

  else if (newPosition &lt; oldPosition) {
    Serial.print(&quot;Going Down&quot;);
    Serial.print('\n');
    sunRising = false;
  }
  oldPosition = newPosition;
  newPosition = sunElevation;


  Serial.print(&quot;AZIMUTH LIMIT SWITCH STATUS =.....&quot;);  //0 = Closed
  Serial.print(azLimitSwitch);
  Serial.print('\n');
  Serial.print(&quot;ELEVATION LIMIT SWITCH STATUS =.....&quot;);  // 0 = Closed
  Serial.print(elLimitSwitch);
  Serial.print('\n');

  if (start &lt; 1) {
    Serial.print(&quot;start &quot;);
    Serial.print(start);
    Serial.print('\n');
    homing();  // This statement checks to see if the programs has just started.
  }

  Serial.print(&quot;TRACKER AZIMUTH ANGLE IS   &quot;);
  Serial.print(trackerAzimuthAngle);
  Serial.print('\n');

  Serial.print(&quot;TRACKER ELEVATION ANGLE IS   &quot;);
  Serial.print(trackerElevationAngle);
  Serial.print('\n');

  //ADJUST THESE TIMES FOR TESTING
  if (hours &gt;= 2 &amp;&amp; hours &lt;= 21 &amp;&amp; minutes &lt;= 59 &amp;&amp; seconds &lt;= 59) {
    daytime = true;
    nightime = false;
  } else {
    nightime = true;
    daytime = false;
  }
  Serial.print(&quot;DAYTIME  &quot;);
  Serial.print(daytime);
  Serial.print('\n');
  Serial.print(&quot;NIGHTIME  &quot;);
  Serial.print(nightime);
  Serial.print('\n');


  // CHANGE THE BELOW FOR TESTING
  if (daytime == true) {
    azimuthRunning();
  } else {
    Serial.print(&quot;PAUSED NOT MOVING UNTIL 6AM&quot;);
    Serial.print('\n');
    digitalWrite(azMotorPinPositive, HIGH);
  }
  // CHECK TO SEE IF ITS TIME TO HOME POSITIONE
  if (hours == 9 &amp;&amp; minutes == 18 &amp;&amp; seconds == 0) {
    homing();
  }

  delay(1000);
}

void homing() {
  digitalWrite(azMotorPinNegative, MOTOR_OFF);
  digitalWrite(azMotorPinPositive, MOTOR_OFF);
  digitalWrite(elMotorPinNegative, MOTOR_OFF);
  digitalWrite(elMotorPinPositive, MOTOR_OFF);

  //HOMING AZIMUTH

  while (digitalRead(azLimitSwitchPin) == LOW) {
    Serial.print(&quot;HOMING AZIMUTH&quot;);
    Serial.print('\n');
    digitalWrite(azMotorPinNegative, MOTOR_ON);
  }

  digitalWrite(azMotorPinNegative, MOTOR_OFF);

  while (digitalRead(azLimitSwitchPin) == HIGH) {
    Serial.print(&quot;BACKING OFF AZIMUTH&quot;);  //Change this to move motor
    Serial.print('\n');
    digitalWrite(azMotorPinPositive, MOTOR_ON);
    azEncoder = 0;  // Sets the encoder back to 0 once homed
  }
  digitalWrite(azMotorPinPositive, MOTOR_OFF);
  start++;

  //HOMING ELEVATION

  while (digitalRead(elLimitSwitchPin) == LOW) {
    Serial.print(&quot;HOMING ELEVATION&quot;);
    Serial.print('\n');
    digitalWrite(elMotorPinNegative, MOTOR_ON);
  }
  digitalWrite(elMotorPinNegative, MOTOR_OFF);

  while (digitalRead(elLimitSwitchPin) == HIGH) {
    Serial.print(&quot;BACKING OFF ELEVATION&quot;);  //Change this to move motor
    Serial.print('\n');
    digitalWrite(elMotorPinPositive, MOTOR_ON);
    elEncoder = 0;  // Sets the encoder back to 0 once homed
  }
  digitalWrite(elMotorPinPositive, MOTOR_OFF);
  start++;
}


void azimuthRunning() {

  if (azimuthSun_position &gt; trackerAzimuthAngle &amp;&amp; daytime == true &amp;&amp; trackerElevationRunning == false) {
    trackerAzimuthRunning = true;
    digitalWrite(azMotorPinPositive, MOTOR_ON);
    Serial.print(&quot;AZ RUNNING &quot;);
    Serial.print('\n');
  } else {
    trackerAzimuthRunning = false;
    digitalWrite(azMotorPinPositive, MOTOR_OFF);
    Serial.print(&quot;AZ Waiting &quot;);
    Serial.print('\n');
  }
  if (trackerAzimuthRunning == false) {
    elevationRunning();  //Elevation Running only gets called if azimuth is not running
  }
}

void elevationRunning() {

  if (sunRising == true) {
    elevationUp();
  } else if (sunRising == false) {
    elevationDown();
  }
}

void elevationUp() {
  if (sunElevation &gt; trackerElevationAngle &amp;&amp; daytime == true) {
    trackerElevationRunning = true;
    digitalWrite(elMotorPinPositive, MOTOR_ON);
    Serial.print(&quot;EL RUNNING GOING UP &quot;);
    Serial.print('\n');
  }

  else {
    trackerElevationRunning = false;
    digitalWrite(elMotorPinPositive, MOTOR_OFF);
    Serial.print(&quot;EL GOING UP WAITING&quot;);
    Serial.print('\n');
  }
}

void elevationDown() {
  if (sunElevation &lt; trackerElevationAngle &amp;&amp; daytime == true) {
    trackerElevationRunning = true;
    digitalWrite(elMotorPinNegative, MOTOR_ON);
    Serial.print(&quot;EL RUNNING GOING DOWN &quot;);
    Serial.print('\n');
  }

  else {
    trackerElevationRunning = false;
    digitalWrite(elMotorPinNegative, MOTOR_OFF);
    Serial.print(&quot;EL GOING DOWN WAITING&quot;);
    Serial.print('\n');
  }
}


void rtcCode() {
  printTime(RTC.get());
  Serial.print(F(&quot;Home:\t&quot;));
  printSolarPosition(Home.getSolarPosition(), digits);
  Serial.println();
  Serial.print('\n');
}

void azEncoderPinChangeA() {
  azEncoder += digitalRead(azEncoder_a) == digitalRead(azEncoder_b) ? -1 : 1;
}
void elEncoderPinChangeB() {
  elEncoder += digitalRead(elEncoder_a) == digitalRead(elEncoder_b) ? 1 : -1;
}


void printSolarPosition(SolarPosition_t pos, int numDigits) {
  Serial.print(F(&quot;el: &quot;));
  Serial.print(pos.elevation, numDigits);
  Serial.print(F(&quot; deg\t&quot;));

  Serial.print(F(&quot;az: &quot;));
  Serial.print(pos.azimuth, numDigits);
  Serial.println(F(&quot; deg&quot;));
  azimuthSun_position = (pos.azimuth);
  sunElevation = (pos.elevation);
}

void printTime(time_t t) {
  tmElements_t someTime;
  breakTime(t, someTime);

  Serial.print(someTime.Hour);
  Serial.print(F(&quot;:&quot;));
  Serial.print(someTime.Minute);
  Serial.print(F(&quot;:&quot;));
  Serial.print(someTime.Second);
  Serial.print(F(&quot; UTC on &quot;));
  Serial.print(dayStr(someTime.Wday));
  Serial.print(F(&quot;, &quot;));
  Serial.print(monthStr(someTime.Month));
  Serial.print(F(&quot; &quot;));
  Serial.print(someTime.Day);
  Serial.print(F(&quot;, &quot;));
  Serial.println(tmYearToCalendar(someTime.Year));

  hours = someTime.Hour;
  minutes = someTime.Minute;
  seconds = someTime.Second;
}

The code uploads and runs with Wire.h in this position but does not read from the RTC.

    #include <Wire.h>
    #include <DS1307RTC.h>
    #include <SolarPosition.h>  

With Wire.h in the below position, the code uploads but does not run or show anything on the Serial monitor.

    #include <DS1307RTC.h>
    #include <SolarPosition.h>
    #include <Wire.h>

Either way round adding Wire.begin() doesn't make any difference to the output.

Greenonline
  • 3,152
  • 7
  • 36
  • 48
user115219
  • 39
  • 4

1 Answers1

1

I have found that adding #include <Wire.h> in the header and Wire.begin() in the set up, the program uploads and runs on the R4 Wifi board. The only issue being the RTC is not working.

I have since changed the RTC library to DS3232RTC.h and added <DS3232RTC myRTC> in the header. I have also amended the functions to get and print the RTC.

    #include <DS3232RTC.h>
    #include <SolarPosition.h>
DS3232RTC myRTC;

Void setup (){
SolarPosition::setTimeProvider(myRTC.get);}

void rtcCode() {
printTime(myRTC.get());}

The program now works as expected and displays the time and date accurately.

Gil Sven
  • 167
  • 8
user115219
  • 39
  • 4