Problem passing values thru nested functions

Status
Not open for further replies.

OhioJim

Well-known member
I am getting these errors:
Telescope: In function 'void displayModeA(double, double, double, double)':

Telescope:118: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double'

hoursToHoursMinutes(RA,hours,minutes);

^

C:\Users\Jim\Documents\Arduino\Telescope\Telescope\Telescope.ino:55:6: note: initializing argument 3 of 'void hoursToHoursMinutes(double&, int&, double&)'

void hoursToHoursMinutes(double &hoursdouble,int &hours,double &minutes)

^

Telescope:123: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double'

degreesToDegreesMinutes(DECL,deg,minutes);

^

C:\Users\Jim\Documents\Arduino\Telescope\Telescope\Telescope.ino:81:6: note: initializing argument 3 of 'void degreesToDegreesMinutes(double, int&, double&)'

void degreesToDegreesMinutes(double degreesdouble,int &deg,double &minutes)

^

invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double'

The errors occur in the function displayModeA.

It has been several decades since I did any C++ programming, and my brain is quite rusty on this. What I am trying to do is pass values into a function. Then inside that function pass them on to another function, and get returned values.

Here is my code. Keep in mind that my sketch is not finished so some of the functions are just stubs, and there may be unused variables.
Code:
#include <Adafruit_GPS.h>
#include <Wire.h>
#include <SerLCD.h> 
SerLCD lcd;

#define GPSSerial Serial2
#define DEBUG 1

// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
int UTChours, UTCminutes, UTCseconds, UTCday, UTCmonth, UTCyear;
int LSThours, LSTminutes, LTCseconds;
double GPSlatitude, GPSlongitude;
double RAinhours, DECindegrees;
int RAhours, RAminutes, RAseconds;
int DECdegrees, DECminutes, DECseconds;


  
void setup()
{

#if DEBUG
 //while (!Serial);  // uncomment to have the sketch wait until Serial is ready
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  Serial.println("Adafruit GPS library basic test!");
#endif
   Wire.begin();

  lcd.begin(Wire);

  lcd.setBacklight(255,255,255); //Set backlight to bright white
  lcd.setContrast(5); //Set contrast. Lower to 0 for higher contrast.

  lcd.clear(); //Clear the display - this moves the cursor to home position as well
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
 

  delay(1000);

  lcd.setCursor(0,0);
 lcd.print("Waiting for GPS fix");
 while (!GPS.newNMEAreceived());
 lcd.clear();
 
}

void hoursToHoursMinutes(double &hoursdouble,int &hours,double &minutes)
/*
 * Convert decimal hours to hours and minutes
 */
{
  hours = floor(hoursdouble);
  minutes = 60.0*(hoursdouble-hours);
}

void hoursToHMS(double hoursdouble,int &hours,int &minutes, int&sec)
/*
 * Convert decimal hours to hours and minutes
 */
{
  hours = floor(hoursdouble);
  minutes = int (60.0*(hoursdouble-hours));
  sec = int (3600.0*hoursdouble - 60.0*minutes);
}

void degreesToDMS(double angle, int &intdegrees, int &intminutes, int &intseconds)
{
  intdegrees = int (floor(angle));
  intminutes = int(floor(60.0*(angle- intdegrees)));
  intseconds = int (3600.0*angle - 60.0*intminutes);
}

void degreesToDegreesMinutes(double degreesdouble,int &deg,double &minutes)
/*
 * Convert decimal angle degrees to degrees and minutes
 */
{
  deg =  floor(degreesdouble);
  minutes = 60.0*(degreesdouble-deg);
}


void displayModeA(double UTC, double LST, double RightAscension, double Declination) 
/*
 * Displays the current UTC, LST, Right Ascension, and Declination on LCD
 */
 {
  int hours, deg;
  int minutes, sec;
  double RA=RightAscension;
  double DECL = Declination;
  hoursToHMS(UTC,hours,minutes, sec);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(hours);
  lcd.print(':');
  lcd.print(minutes);
  lcd.print(':');
  lcd.print(sec);
  lcd.print(" UTC");
 hoursToHMS(LST,hours,minutes, sec);
  lcd.setCursor(0,1);
  lcd.print(hours);
  lcd.print(':');
  lcd.print(minutes);
  lcd.print(":");
  lcd.print(sec);
  lcd.print(" LST");
  lcd.setCursor(0,2);
  hoursToHoursMinutes(RA,hours,minutes);
  lcd.print(hours);
  lcd.print(':');
  lcd.print(minutes,3);
  lcd.print(" RA");
  degreesToDegreesMinutes(DECL,deg,minutes);
  lcd.setCursor(0,3);
  lcd.print(deg);
  lcd.print(':');
  lcd.print(minutes,3);
  lcd.print(" Dec");
  
 }

 void getAzEl(double &Azimuth, double &Elevation)
 {
  
 }

 void getAngles(double LST, double &RightAscension, double &Declination)
 {
  
 }


 void getLST(double hours,int days, int months, int years, double lon,double &LST)
 {
  double JulianDays,  GMST;
  
  //calculate the number of julian days since January 0, 2000
  JulianDays = hours/24.0+367.0*years-floor(7.0*(years+floor((months+9.0)/12.0))/4.0)+floor(275.0*months/9.0)+days-730531.5;

  //this gives the GMST in Degrees!!
  GMST= fmod((280.46061837+360.98564736629*JulianDays),360.0);

  //convert to decimal hours
  LST=GMST*24.0/360.0;
  
 }

 void   getRaDec(double LST, double latitude, double &RightAscension,double &Declination)
 {
  double azimuth, elevation, hourAngle, pi= 3.14159265358979323846 ;
  //get azimuth and elevation angles
  getAzEl(azimuth,elevation);

  //convert to declination in degrees
  Declination = (180.0/pi)*asin(sin(elevation)*sin(latitude)+cos(elevation)*cos(latitude)*cos(azimuth));

  //convert to right ascension in hours
  hourAngle = (24.0/pi)*acos((sin(elevation)-sin(latitude))/(cos(latitude)*cos(Declination)));
  RightAscension = LST+hourAngle;
 }
 
void loop() {
  // put your main code here, to run repeatedly:
  int hours, minutes,sec, days, months, years;
  uint numSatellites;
  double lat, lon,LST,Declination,RightAscension;
  byte mode;
  mode = 0;
  sec=0;
  
    // if a sentence is received, we can check the checksum, parse it...
    if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trying to print out data
    //Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
    if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return; // we can fail to parse a sentence in which case we should just wait for another
  }
    hours =  GPS.hour+60.0*minutes+3600*GPS.seconds;
  
  days = GPS.day;
  months = GPS.month;
  years = 2000 + GPS.year;

  lat = GPS.latitudeDegrees;
  lon = GPS.longitudeDegrees;
  getLST(hours,days, months, years,lon,LST);
  getAngles(LST, RightAscension, Declination);
  
  numSatellites=GPS.satellites;
  if (mode!=3)
  {
    displayModeA(hours,  LST,  RightAscension,  Declination);
  }

}
 
I don't have serLCD installed so did not compile it, but looking I see:
lines 118 and 123 marked in Red, Defines in Green
Code:
void displayModeA(double UTC, double LST, double RightAscension, double Declination)
/*
   Displays the current UTC, LST, Right Ascension, and Declination on LCD
*/
{
 [COLOR="#008000"] int hours, deg;
  int minutes, sec;
  double RA = RightAscension;
  double DECL = Declination;[/COLOR]
  hoursToHMS(UTC, hours, minutes, sec);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(hours);
  lcd.print(':');
  lcd.print(minutes);
  lcd.print(':');
  lcd.print(sec);
  lcd.print(" UTC");
  hoursToHMS(LST, hours, minutes, sec);
  lcd.setCursor(0, 1);
  lcd.print(hours);
  lcd.print(':');
  lcd.print(minutes);
  lcd.print(":");
  lcd.print(sec);
  lcd.print(" LST");
  lcd.setCursor(0, 2);
[COLOR="#FF0000"]  hoursToHoursMinutes(RA, hours, minutes);[/COLOR]
  lcd.print(hours);
  lcd.print(':');
  lcd.print(minutes, 3);
  lcd.print(" RA");
[COLOR="#FF0000"]  degreesToDegreesMinutes(DECL, deg, minutes);[/COLOR]
  lcd.setCursor(0, 3);
  lcd.print(deg);
  lcd.print(':');
  lcd.print(minutes, 3);
  lcd.print(" Dec");

}

So looking at degreesToDegreesMinutes called from line 123 hoursToHoursMinutes(RA, hours, minutes);

Code:
void degreesToDegreesMinutes(double degreesdouble, int &deg, double &minutes)
So minutes in the above function is defined as int and you are trying to pass it by reference to the function who is expecting a double

looks like similar with the 118 line: void hoursToHoursMinutes(double &hoursdouble, int &hours, double &minutes)
 
Your parameter declarations are very odd. I have never heard of a type "int &". ("int *" yes) Especially since your are not passing anything that looks like an address or dereferencing the pointer in the function. See:
Code:
void hoursToHoursMinutes(double &hoursdouble,int &hours,double &minutes)
etc.
Lose the "&" and it should work better.
 
Thanks to both of you. That fixed the problems.

The problem was that 'minutes' as an angle is supposed to be a double, but 'minutes' as a time is an int. I had overlooked that.
 
Your parameter declarations are very odd. I have never heard of a type "int &". ("int *" yes) Especially since your are not passing anything that looks like an address or dereferencing the pointer in the function. See:
Code:
void hoursToHoursMinutes(double &hoursdouble,int &hours,double &minutes)
etc.
Lose the "&" and it should work better.

The pass by reference is needed for the function to return its results I beleve.
https://isocpp.org/wiki/faq/references
 
Status
Not open for further replies.
Back
Top