Solar Position. Inserting Lat and Long

Hi,

I'm struggling to get back to writing code for an Arduino, I've tried the web but unable to find the answer, possibly because I'm using the wrong terms. Please put my right.

I'm trying to use and adapt the Solar Position Library by Ken Willmott. My issue is:
I've added Albufeira to the list of locations with its Lat and Long. This works as expected.
So I can use it in anywhere I need to parse Lat and Long of the different locations which come from a GPS module.
Lat and Long should be float values I believe.
This works:
SolarPosition Albufeira(37.08766, -8.25206);

For test purposes I inserted 2 float values for for Lat and Long.
This doesn't work:
float Lat = 37.08766;
float Long = -8.25206;

SolarPosition Albufeira(Lat, Long);

Could somebody explain what I'm doing wrong and what I should do to correct it.

Many thanks

#include <Time.h>
#include <TimeLib.h>

// solarTimeDemo

// Arduino example sketch for SolarPosition library
//
// Calculate solar position from time and location information
// using Time library functions (relying on CPU clock based timer) to keep time.

// 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;
float Lat = 37.08766;
float Long = -8.25206;
// some test positions:
SolarPosition Toronto(43.6777, -79.6248);  // Toronto, Canada airport (YYZ)
SolarPosition Timbuktu(16.775214, -3.007455); // Timbuktu, Mali, Africa
SolarPosition Melbourne(-37.668987, 144.841006); //Melbourne Airport (MEL)
SolarPosition Ulaanbaatar(47.847410, 106.769004); //Ulaanbaatar Airport (ULN)
SolarPosition Albufeira(37.08766, -8.25206);
//My intention is to insert Lat and Long as float values
//something like this. But I get incorrect results
//SolarPosition Albufeira(Lat, Long);

// A solar position structure to demonstrate storing complete positions
SolarPosition_t savedPosition;

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

// program begins

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

  // set Time clock to Jan. 1, 2000
  setTime(SECS_YR_2000);
  Serial.print(F("Setting clock to "));
  printTime(SECS_YR_2000);

  // set the Time library time service as the time provider
  SolarPosition::setTimeProvider(now);

  // save a complete current position
  //savedPosition = Ulaanbaatar.getSolarPosition();
  savedPosition = Albufeira.getSolarPosition();

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

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

  Serial.println(F("Real time sun position reports follow..."));
  Serial.println();
}

void loop()
{
  // now test the real (synchronized) time methods:
  //
  printTime(Toronto.getSolarPosition().time);
  Serial.print(F("Toronto:\t"));
  printSolarPosition(Toronto.getSolarPosition(), digits);
  Serial.print(F("Melbourne:\t"));
  printSolarPosition(Melbourne.getSolarPosition(), digits);
  Serial.print(F("Timbuktu:\t"));
  printSolarPosition(Timbuktu.getSolarPosition(), digits);
  Serial.print(F("Ulaanbaatar:\t"));
  printSolarPosition(Ulaanbaatar.getSolarPosition(), digits);
  Serial.println();
  Serial.print(F("Ulaanb. Saved:\t"));
  printSolarPosition(savedPosition, digits);
  
  Serial.print(F("Albufeira:\t"));
  printSolarPosition(Albufeira.getSolarPosition(), digits);
  Serial.println();
  Serial.print(F("Albu. Saved:\t"));
  printSolarPosition(savedPosition, digits);
  Serial.println();

  delay(15000);
}

// 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));
}

Can you explain exactly what you mean by "This doesn't work:"?

johnwasser:
Can you explain exactly what you mean by "This doesn't work:"?

Sorry.

I get correct results using Ken's method, I can check this using an app on my phone.

When I create the identical float values and and place their names into the code line, the results are incorrect.

In the library documentation:
"In case the geographical locations will be assigned dynamically or radians are preferred as an angular unit, or for any other reason the SolarPosition class is not preferred, direct access to the utility function calculateSolarPosition() is permitted. See the example sketch solarTimeFunctionDemo for details."

Sounds like what you want to do (get location dynamically from a GPS) is not easily done with the "SolarPosition" class.

Thank you for your help, I don't totally understand what his text means.

I will try another library.

Many thanks for your help.

Both forms work fine for me:

// number of decimal digits to print
const uint8_t digits = 3;
const float la=44.0,lo=123.0;
// some test positions:
SolarPosition home(la,lo);

Result: The sun was at an elevation of 46.0896 and an azimuth of 181.6669

// number of decimal digits to print
const uint8_t digits = 3;
// some test positions:
SolarPosition home(44.0,123.0);

Result: The sun was at an elevation of 46.0896 and an azimuth of 181.6669

As an update. I am successfully using the code from here.

That is where the library started. Same core code, but the library is a lot easier to use.