Solar Tracker

I am trying to get a sketch working where I can put in longitude, latitude, date and time and get back solar elevation and azimuth.

The following looks like it will do exactly that.
https://github.com/KenWillmott/SolarPosition/blob/master/SolarPosition.h

I can’t get it to compile. I’m quite sure I don’t have the right libraries, headers, or whatever in the right places.
I don’t have a good understanding of what goes where. I know there is a folder arduino\libraries.

I tried doing a copy and paste into a file there but I don’t know if that’s right.
At this point, I’m trying to compile this sketch:

// solarTimeNoClockDemo

// Arduino example sketch for SolarPosition library
//
// Calculate solar position from time and location information
// not using any clock to keep time. Just do calculations.

// 2017 Ken Willmott
// Arduino library based on the program "Arduino Uno and Solar Position Calculations"
// (c) David R. Brooks, which can be found at http://www.instesre.org/ArduinoDocuments.htm
// and issued under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License:
// https://creativecommons.org/licenses/by-nc-nd/4.0/

#include <SolarPosition.h>

// number of decimal digits to print
const uint8_t digits = 3;

// some test positions:
SolarPosition Timbuktu(16.775214, -3.007455); // Timbuktu, Mali, Africa

// create a fixed UNIX time to test fixed time method
int someS = 0;  //second
int someM = 0;  //minute
int someH = 12; //hour
int someD = 15; //day
int someMo = 4; //month
int someY = 1985; //year
tmElements_t someTime = {someS, someM, someH, 0, someD, someMo, CalendarYrToTm(someY) };
time_t someEpochTime = makeTime(someTime);

// program begins

void setup()
{
  Serial.begin(9600);
  Serial.println(F("\tSolar Position Demo"));

  // First test the fixed time methods:
  //
  Serial.print(F("The sun was at an elevation of "));
  Serial.print(Timbuktu.getSolarElevation(someEpochTime), 4);
  Serial.print(F(" and an azimuth of "));
  Serial.println(Timbuktu.getSolarAzimuth(someEpochTime), 4);
  Serial.print(F("in Timbuktu at "));
  printTime(someEpochTime);

  Serial.print(F("The earth was "));
  Serial.print(Timbuktu.getSolarDistance(someEpochTime), 0);
  Serial.println(F(" km from the Sun."));
  Serial.println();

  Serial.println(F("Done..."));
  Serial.println();
}

void loop()
{
}

// Print a solar position to serial
//
void printSolarPosition(SolarPosition_t pos, int numDigits)
{
  Serial.print(F("el: "));
  Serial.print(pos.elevation, numDigits);
  Serial.print(F(" deg\t"));

  Serial.print(F("az: "));
  Serial.print(pos.azimuth, numDigits);
  Serial.println(F(" deg"));
}

// Print a time to serial
//
void printTime(time_t t)
{
  tmElements_t someTime;
  breakTime(t, someTime);

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

I get this error message:
Arduino: 1.8.4 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

C:\Users\Ted\AppData\Local\Temp\ccGHZPKu.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_solarTimeNoClockDemo.ino.cpp.o.1895’:

ccGHZPKu.ltrans0.o:(.text.startup+0xa8): undefined reference to `SolarPosition::SolarPosition(float, float)’

C:\Users\Ted\AppData\Local\Temp\ccGHZPKu.ltrans0.ltrans.o: In function `main’:

ccGHZPKu.ltrans0.o:(.text.startup+0x2cc): undefined reference to `SolarPosition::getSolarElevation(unsigned long)’

ccGHZPKu.ltrans0.o:(.text.startup+0x2ee): undefined reference to `SolarPosition::getSolarAzimuth(unsigned long)’

ccGHZPKu.ltrans0.o:(.text.startup+0x526): undefined reference to `SolarPosition::getSolarDistance(unsigned long)’

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Can someone tell me what files I need and where I need to put them? That would be very much appreciated. I’ve been stumbling around trying stuff for hours and I just don’t know what I’m doing.

The problem is you only installed the .h file (which contains declarations), but not the .cpp file (which contains the definitions. You need to take the time to learn how to install Arduino libraries. This is an essential skill. The general instructions are here:

Instructions for installing the SolarPosition library specifically:

  • Delete your failed attempt at installation.
  • Open the link you posted above: SolarPosition/SolarPosition.h at master · KenWillmott/SolarPosition · GitHub
  • Navigate to the home page of the repository by clicking on the "SolarPosition" link at the top of the page.
  • On the home page, you will see a button "Clone or download". Click on it.
  • Click "Download ZIP". This will download a .zip file of the library.
  • Start the Arduino IDE.
  • Sketch > Include Library > Add .ZIP Library
  • Select the downloaded file.
  • Click "Open".

Thank you very much. I'll try that tonight.

Ok, that went well. Also I got a keypad to work by adding a library entry and making some changes.

Now I’m stuck trying to get a compass to work. It’s a GY-271
This is my sketch:

#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
void setup()
{
 Serial.begin(9600);
 Wire.begin();
 compass = HMC5883L();
 compass.SetScale(1.3);
 compass.SetMeasurementMode(Measurement_Continuous);
}
void loop()
{
 MagnetometerRaw raw = compass.ReadRawAxis();
 MagnetometerScaled scaled = compass.ReadScaledAxis();
 float xHeading = atan2(scaled.YAxis, scaled.XAxis);
 float yHeading = atan2(scaled.ZAxis, scaled.XAxis);
 float zHeading = atan2(scaled.ZAxis, scaled.YAxis);
 if(xHeading < 0) xHeading += 2*PI;
 if(xHeading > 2*PI) xHeading -= 2*PI;
 if(yHeading < 0) yHeading += 2*PI; 
 
 if(yHeading > 2*PI) yHeading -= 2*PI;
 if(zHeading < 0) zHeading += 2*PI;
 if(zHeading > 2*PI) zHeading -= 2*PI;
 float xDegrees = xHeading * 180/M_PI;
 float yDegrees = yHeading * 180/M_PI;
 float zDegrees = zHeading * 180/M_PI;
 Serial.print(xDegrees);
 Serial.print(",");
 Serial.print(yDegrees);
 Serial.print(",");
 Serial.print(zDegrees);
 Serial.println(";");
 delay(100);
}

This is where I got the sketch.

It won’t compile. I got the library from github.
This is the error message:
Arduino: 1.8.4 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

C:\Users\Ted\Documents\Arduino\Tracker_Compass\Tracker_Compass.ino: In function ‘void setup()’:

Tracker_Compass:9: error: ‘class HMC5883L’ has no member named ‘SetScale’

compass.SetScale(1.3);

^

Tracker_Compass:10: error: ‘class HMC5883L’ has no member named ‘SetMeasurementMode’

compass.SetMeasurementMode(Measurement_Continuous);

^

Tracker_Compass:10: error: ‘Measurement_Continuous’ was not declared in this scope

compass.SetMeasurementMode(Measurement_Continuous);

^

C:\Users\Ted\Documents\Arduino\Tracker_Compass\Tracker_Compass.ino: In function ‘void loop()’:

Tracker_Compass:14: error: ‘MagnetometerRaw’ was not declared in this scope

MagnetometerRaw raw = compass.ReadRawAxis();

^

Tracker_Compass:15: error: ‘MagnetometerScaled’ was not declared in this scope

MagnetometerScaled scaled = compass.ReadScaledAxis();

^

Tracker_Compass:16: error: ‘scaled’ was not declared in this scope

float xHeading = atan2(scaled.YAxis, scaled.XAxis);

^

exit status 1
‘class HMC5883L’ has no member named ‘SetScale’

Specified folder/zip file does not contain a valid library

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

It seemed like I might have the wrong library even though the name was the same. The .pdf referenced above has a “download” link. I tried that one and that is when I noticed the “Specified folder/zip file does not contain a valid library”

I would delete the libraries involved and take another run at it but I don’t see how to delete and that might not be the right approach anyway.

Hold off on spending too much time on this. I found a different sketch that came with the .zip from github that does compile. Now I just need to see if it will do what I need.

tkolsenattds:
It seemed like I might have the wrong library even though the name was the same.

I think you're right. It's quite common for there to be multiple incompatible Arduino libraries with a generic name like "HMC5883L". You need to make sure to find the one that your code was written for.

tkolsenattds:
The .pdf referenced above has a "download" link. I tried that one and that is when I noticed the "Specified folder/zip file does not contain a valid library"

It's because the .zip file doesn't have the correct structure to allow it to be installed via the Arduino IDE's Sketch > Include Library > Add .ZIP Library. The library must be located in the root of the .zip folder for that to work.
To install it, you need to do this:

  • Download the file from the PDF (you probably already did that).
  • Unzip the downloaded file.
  • (In the Arduino IDE) Sketch > Include Library > Add .ZIP Library
  • Select the HMC5883\HMC5883L(Arduino)[V4]\HMC5883L subfolder from the unzipped folder.
  • Click "Open".

It's not obvious from the menu name, but "Add .ZIP Library" can also install libraries from unzipped folders.

tkolsenattds:
Hold off on spending too much time on this. I found a different sketch that came with the .zip from github that does compile. Now I just need to see if it will do what I need.

OK, I had already typed the above. You might be better off using the library from GitHub. Often the quality of projects hosted on GitHub is higher because it indicates they are using version control and have made collaboration easy. I don't get a lot of confidence from the bad structure of the .zip file from the .pdf. That said, I have spent some time on the electrodragon website in the past and they seem to do some good work.