0

There is a project of mine which utilizes coordinates to be entered by the user and then processing them for sunrise and sunset time calculations. Although everything seems to work fine but there is something which I noticed recently and that is whenever I enter the longitudes they remain how they are entered until they are on the screen, after the screen (is forward, going backward seems to have no effect) is moved to another screen(for another variable setting) it changes which is fatal as then sunrise and sunset calculations suffer from inaccuracy. What can be done here. Here is the code for the buttons which are responsible for the movement of the screen variable(which controls what is to display on the screen) and the changing of digits variable (which is used to enter the coordinates).

All the variables which are set to 1 and 0 are of unsigned 8bit integers, such as screen, forward_pressed and back_pressed.

These are the variable declarations:-

//---------SUNRISE AND SUNSET VARIABLES-----------
float latitude, longitude, prevlongitude, timezone;
float EElats EEMEM;
float EElongs EEMEM;
float EEtimezone EEMEM;
unsigned int sunrise_minutes = 0, sunrise_hours = 0;
unsigned int sunset_minutes = 0, sunset_hours = 0;
char digits = '0';
char latbuffer[16], longbuffer[16];

//THIS IS HOW I AM CONTROLLING THE DATA VIA KEYS

//FORWARD BUTTON
if((FORWARD_BUTTON == 0) && (forward_pressed == 0))
{
    screen++;
    forward_pressed = 1;
}
else if((FORWARD_BUTTON == 1) && (forward_pressed == 1))
{
    forward_pressed = 0;
}
//BACK BUTTON
if((BACK_BUTTON == 0) && (back_pressed == 0))
{
    screen--;
    if(screen == 0) screen = 6;
    back_pressed = 1;
}
else if((BACK_BUTTON == 1) && (back_pressed == 1))
{
    back_pressed = 0;
}


if((screen > 0) && (screen < 13))
{
    //INCREMENT BUTTON
    if((INCREMENT_BUTTON == 0) && (inc_pressed == 0))
    {
        if(digits == '9') digits = '0';
        else digits++;
        if((screen > 0) && (screen < 7))
        {
            latbuffer[field] = digits;
        }
        else if((screen > 6) && (screen < 13))
        {
            longbuffer[field] = digits;
        }
        inc_pressed = 1;
    }
    else if((INCREMENT_BUTTON == 1) && (inc_pressed == 1))
    {
        inc_pressed = 0;
    }
    //DECREMENT BUTTON
    if((DECREMENT_BUTTON == 0) && (dec_pressed == 0))
    {
        if(digits == '0') digits = '9';
        else digits--;
        if((screen > 0) && (screen < 7))
        {
            latbuffer[field] = digits;
        }
        else if((screen > 6) && (screen < 13))
        {
            longbuffer[field] = digits;
        }
        dec_pressed = 1;
    }
    else if((DECREMENT_BUTTON == 1) && (dec_pressed == 1))
    {
        dec_pressed = 0;
    }
}

Below is what I have been using for displaying of coordinates, although pretty much the same for latitudes and well as longitudes, but still its the longitudes which changes. Here it is :-

if((screen > 0) && (screen < 7))
{
    digits = latbuffer[field];
    sprintf(latbuffer, "%07.4f", latitude);
    lcd_string(0, 0, "ENTER   ");
    lcd_string(8, 0, latbuffer);
    lcd_string(15, 0, "N");
    lcd_string(0, 1, main_line3);
    latitude = atof(latbuffer);
}
else if((screen > 6) && (screen < 13))
{
    digits = longbuffer[field];
    sprintf(longbuffer, "%07.4f", longitude);
    lcd_string(0, 0, "ENTER   ");//changed the spacing to two digits
    lcd_string(8, 0, longbuffer);
    lcd_string(15, 0, "E");
    lcd_string(0, 1, main_line3);
    longitude = atof(longbuffer);
}

Now to summarise :-

  1. Longitude changes after the screen moves forward via the forward button, there is no problem in backing up the display i.e. nothing changes if backward button is used.
  2. I am thinking how I am updating the display with longitudes using atof() is what causing this problem but can't think of any other way. What should be done.

This is what I have tried to done until now:-

  1. Usage of two different variables for longitudes and latitudes long_digits and lat_digits respectively.
  2. Usage of a flag which detects the user pressing the button and enabling the atof() to update only if the user changes value intentionally.

Feel free to ask for any more information. Just guide me don't share any code. Thanks in advance to all the beautiful people on the internet :) .

  • Where and how your variables are declared? Where and how those chunks of code used? If value is changing, that means somewhere its value is overwritten. It is hard to say since you have omitted essential parts of the code – AterLux Jul 21 '23 at 08:07
  • Well I thought that this might be the only thing that will be required. Wait let me post the variable declarations and the places where the coordinate variables are used. – user9728254 Jul 21 '23 at 10:09
  • I have edited the variable declarations and where these variables are being used at this moment. They are being used elsewhere in the code too but I have pinpointed the error in these parts of the code, especially in the usage of sprintf() and atof() function which triggers multiple times as the screen is pushed back and forth. So what I wanted to say is that other parts where these variables are used are separated so there isn't any way for them to be changed there and also I can the odd behaviour in this part of the code and not elsewhere. Please tell me what else is needed :) – user9728254 Jul 21 '23 at 10:48

0 Answers0