Compare a current calculated value with the previous value in a loop function

Hi

Experience:
I have only been tinkering with Arduino coding for a few weeks, so I’m still struggling… alot.

Hardware:
I am using an Arduino Uno along with a DS1307 RTC and 2 x 28BYJ-48 Steppers with their power modules.

Purpose of the program:
The purpose of the program is to calculate the current elevation and azimuth of the sun (Based on my local time from RTC and Geographical Location) using some fancy math I found online (Julian dates and a lot of sin, cos & tan :confused: ). I would then loop this every 60 sec (Can be more or less. does not matter). I would then get the difference between the current elevation and azimuth and that of 60sec ago. This difference will then move 2 stepper motors a corresponding amount of steps, resulting (hopefully one day) in a device/system that can track the sun as it moves through the day sky.

Also:
I do not want to use a system making use of LDRs and relying on direct sunlight. (So we can skip that debate… :))

The Problem:
My problem is storing the previous calculated elevation/azimuth in order to compare it with the current value and get a difference between the 2.

What I tried to do:
From what I found online, the solution to this problem is as simple as…
Calculate the current value => Use Current and Previous Value to get the difference => Store the current value as the previous value (Something like Previous_Value = Current_Value).

This does not work on my code. Not sure why, not sure how to fix it. Please help…

The Full Code:
The problem is in the “//STEPPER PART”

#include "RTClib.h"
#include <Stepper.h>
#include <SPI.h>

#define DEG_TO_RAD 0.01745329
#define PI 3.141592654
#define TWOPI 6.28318531
#define STEPS 32

RTC_DS1307 rtc;
Stepper Stepper_azimuth(STEPS , 6 , 8 , 7 , 9);
Stepper Stepper_elev(STEPS , 2 , 4 , 3 , 5);

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

//RTC Setup
#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled

    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

  //Stepper Setup
  Stepper_azimuth.setSpeed(200);  //Set Speed at "x" RPM
  Stepper_elev.setSpeed(200);     //Set Speed at "x" RPM
  }
void loop() {        

//RTC Part
  DateTime now = rtc.now();

//Position Part
  int hour=now.hour(),minute=now.minute(),second=now.second(),month=now.month(),day=now.day(),year,zone=0;
  float Lon=28.17286*DEG_TO_RAD, Lat=-25.94973*DEG_TO_RAD;
  float T , JD_frac , L0 , M , e , C , L_true , f , R , GrHrAngle , Obl , RA , Decl , HrAngle , elev , azimuth , elev_f , azimuth_f ;
  float azimuth_step_dec , azimuth_step_round , elev_step_dec , elev_step_round ;
  long JD_whole,JDx;
  float elev_diff;
  float elev_prev;

  Serial.print("Longitude and latitude "); Serial.print(Lon/DEG_TO_RAD,3); 
  Serial.print("  "); Serial.println(Lat/DEG_TO_RAD,3);  
  Serial.println("year,month,day,local hour,minute,second,elevation,azimuth");
  year=now.year();
      JD_whole=JulianDate(year,month,day);
      JD_frac=(hour+minute/60.+second/3600.)/24.-.5;
      T=JD_whole-2451545; T=(T+JD_frac)/36525.;
      L0=DEG_TO_RAD*fmod(280.46645+36000.76983*T,360);
      M=DEG_TO_RAD*fmod(357.5291+35999.0503*T,360);
      e=0.016708617-0.000042037*T;
      C=DEG_TO_RAD*((1.9146-0.004847*T)*sin(M)+(0.019993-0.000101*T)*sin(2*M)+0.00029*sin(3*M));
      f=M+C;
      Obl=DEG_TO_RAD*(23+26/60.+21.448/3600.-46.815/3600*T);     
      JDx=JD_whole-2451545;  
      GrHrAngle=280.46061837+(360*JDx)%360+.98564736629*JDx+360.98564736629*JD_frac;
      GrHrAngle=fmod(GrHrAngle,360.);    
      L_true=fmod(C+L0,TWOPI);
      R=1.000001018*(1-e*e)/(1+e*cos(f));
      RA=atan2(sin(L_true)*cos(Obl),cos(L_true));
      Decl=asin(sin(Obl)*sin(L_true));
      HrAngle=DEG_TO_RAD*GrHrAngle+Lon-RA;
      
      elev=asin(sin(Lat)*sin(Decl)+cos(Lat)*(cos(Decl)*cos(HrAngle)));             //Elevation in RADS
      elev_f = elev/DEG_TO_RAD;                                                    //Elevation in Deg

      // Azimuth measured eastward from north.
      azimuth=PI+atan2(sin(HrAngle),cos(HrAngle)*sin(Lat)-tan(Decl)*cos(Lat));     //Azimuth in RADS
      azimuth_f = azimuth/DEG_TO_RAD;                                              //Azimuth in Deg



      Serial.print(year); Serial.print(","); Serial.print(month);
      Serial.print(","); Serial.print(day); Serial.print(", "); 
      Serial.print(hour-zone); Serial.print(",");
      Serial.print(minute); Serial.print(","); Serial.print(second);
      Serial.print(","); Serial.print(elev/DEG_TO_RAD,3); 
      Serial.print(","); Serial.print(azimuth/DEG_TO_RAD,3); Serial.println();

//STEPPER PART
      //For 28BYJ-48 ... 1 Step = 2*5.625deg = 11.25deg of motor rotation.
      //Speed ration is 1/64 ... 1 step = 11.25deg/64 = 0.17578125deg on shaft
      //Direct Drive ... For 1deg change in elev/azimuth (Output Shaft) = 1/0.17578125 = 5.688...steps

      //Calculate angular difference
      //Elevation

      
   
      elev_diff = elev_f-elev_prev;
 
  
      //Calculate Steps
      elev_step_dec=elev_diff/0.17578125;

      int elev_step_int = (elev_step_dec+.5);
    //  Stepper_elev.step(elev_step_int);      

            
      azimuth_step_dec=(azimuth)/0.17578125;                 //Amount of steps in decimals
      azimuth_step_round=round(azimuth_step_dec);            //Amount of steps rounded
   //   Stepper_azimuth.step(azimuth_step_round);
   
      



//Serial Print Stepper Results
      Serial.println(elev_f,30);
      Serial.println(elev_prev,30);
      Serial.println(elev_diff,30);
      Serial.print(elev_step_dec,5);Serial.print(" , ");Serial.print(elev_step_int);Serial.println();
   

      elev_prev=elev_f;        


      
delay(60000);


  }
  
long JulianDate(int year, int month, int day) {
  long JD_whole;
  int A,B;
  if (month<=2) { 
    year--; month+=12;
  }  
  A=year/100; B=2-A+A/4;
  JD_whole=(long)(365.25*(year+4716))+(int)(30.6001*(month+1))+day+B-1524;
  return JD_whole;
}

Make elev_prev a global variable. As you have it now, its value will not persist between calls to loop.

Or declare it in loop() and declare it static so that its value is preserved between calls to loop()

wildbill:
Make elev_prev a global variable. As you have it now, its value will not persist between calls to loop.

Just like that everything falls into place. Thank you very much wildbill. Making the previous values global variables solved this problem.
Thanks a lot... :smiley: :smiley: