Code not iterating - aquaium light project

Good morning...newbie alert...fresh meat!!!! :rofl:

I am designing a lighting system that will simulate the sun and moon. Don't know if it will go over and aquarium or aquaponics bed yet. I'm new to Arduino but not to astronomical computing.

void loop() {
  while ((now - last_time) < 1000 ){ // set to 60000 for 1 minute 
    now = millis();
    double now1 = now / 10000;
    double last_time = now1; // prepare for next loop 
  }
  float Lat = 44.654392;
  float LatR = .779366;
  float TD = 57.295780;
  float TR = .017453;
  //46 STEP 2
  double dSb = 172;
  double dSb1 = dSb + last_time;
  double D1 = (2021 - 2010) * 365.242189;
  double D2 = D1 + dSb1;
  Serial.print("dSb ");
  Serial.println(dSb, 8);
  Serial.print("dSb1 = ");
  Serial.println(dSb1, 8);
  Serial.print("Days since 2010 epoch: ");
  Serial.println(D2, 8);
  //46 STEP 3
  double ND = (360/365.242189) * D2;

First, I'm telling the arduino to pause for roughly 1 second (I know there are better ways and have a couple RTC's on order).
Then my latitude in degrees and radians followed by conversion numbers (radians to degrees = TD) and (degrees to radians = TR).
dSb = days Since beginning of the year...also known as day of the year...range from 1 to 365...172 corresponds to June 21 (Summer Solstice or largest amount of light day of the year)
D1 is the calculation for number of days since 1/1/2010 and this gets added to dSb to give me a total day number (D2).

dSb1 is the problem. When I print to the Serial Monitor...it reports that dSb is 172.00000000 (TRUE) but reports that dSb1 = 0.00000000 (FALSE)????? It should be 172.10000000.........

My end goal, within the bigger code, is to get the code to cycle completely, illuminate the proper LED's according to other calculations, then come back to the top, iterate by say....1 minute (code does NOT reflect this goal at this time) and then recalculate with the new number and illuminate the proper LEDS and repeat...add more...recalculate...light up...repeat......

If I cannot get my variable to iterate properly...the whole thing is shot...a 1 time Willie.

I read another post where a user was doing something similar with solar calculations and the responses only succeeded in confusing me more...it's bad enough that I must jump back and forth between degrees and radians...let alone multiply by 1000000 in one line, divide by 1000000 in another then stand on my head and sing "Freebird" backwards while playing the piccolo just to get 1 variable to calculate properly (*don't try this at home...the fall risk is detrimental to your health as much as playing the piccolo).

Once this is completed, I don't mind sharing my code in totality but as of now...I'm almost 1,700 lines in and am jammed up at the beginning :frowning_face:

Without needing a PhD in C programming...what is the answer to my quandary (please/thank you)?

Sincerely...a noob

Try printing last_time as well - it's probably zero.

you are correct. last_time = 0??? Do I need to move it outside the {}?

last_time you declare inside the while() compound statement is local to the that statement and not the global variable last_time you might have declared somewhere else

what are you trying to achieve? if you want a pause, just use delay()

Not a delay....of sorts. I want the program to iterate a certain time span...in this case, stop long enough to equal a second....then pass that second onward to be added to the day variable...so for example....midnight would be 172.00000000....a second later, 172.00001574.....a second later, 172.00003148, etc

1 second = 1/86400 a day or .00001574 day

if you want to stop for a second, just do delay(1000); (➜ your while is blocking anyway so it's similar)
(I'm not sure what you are trying to achieve)

That seems to be using unnecessary precision - is there any meaningful change to sun or moon in a single second position that your lighting can simulate?

I'm trying to achieve iteration. I'm aware that delay(1000); will pause for approx 1 sec. I'd like the code to add whatever time limitation I choose to the day number...in this case, day # 172...then continue down the code processing the other variables...turning on the appropriate LEDs, then coming back to the top...add whatever time iteration I choose...say add 1 minute for clarity, cycle through with new information...process...light up...cycle to top...add another minute...process...output...add a minute and so on. My code is not adding the new iteration :yum:

I don't get it... sorry
if you want to pause not for 1s but for a known duration, just do delay(knownDuration);

The Sun moves 15 degrees per hour. The Moon travels a bit differently but still almost as rapidly. I have designed and build an array of 60 LED's (3 rows of 20) and set my conditional statements for roughly every 8-9 degrees of calculation (if sun is 9 degrees above horizon...light up board 1...if sun is 9.5 degrees above horizon, light up board 2 and dim board 1 by 75%, etc)

Sorry. Ummm....I've tried

for (a = 0; a <= 1440; a++){
code
}

This works well if I want to update by whole numbers....my day number becomes 173, 174, 175, etc.
I want the ability to update by smaller increments.
I've tried

for (a = 0; a <=1440, a =(a / 1440)); {
code
}

and it never worked. I've sought other solutions and forums are always my last resort (bad experiences). Hope this helps clear up a bit :slightly_smiling_face:

try to explain in plain English what you are trying to achieve.. the two code tidbits you posted are probably not what you need

Again....I would like to add a small amount of time (a second, a minute, an hour...whichever I choose) to a day variable, process that variable throughout the code, loop to the top...add another small amount of time (another second, another minute, another hour) to the variable and recalculate.

Based upon that, my LED's will appear to move left to right in various intensities simulating the sun and moon independently moving across the "sky".

In the "old days"....it would be as simple as:

please excuse my pseudo code....but

a = a + .00001574
//variable a now = .00001574
day = 172 + a
//variable day now = 172.00001574
//process code and loop back
//variable a now = .00003144
//variable day now = 172.00003144

so on and on.

Had to put it in code brackets because my comments wouldn't show up

A useful technique might be to work in seconds since midnight. You would store it in an unsigned long. Then you can calc how far through the day you are using the variable and divide it by 86400UL.

well your variable need to be able to represent 0.00001574 correctly. Simple or Double precision (float or double) on a small arduino has only 6 digits precision so you'll get weird stuff...

double currentTime = 0;

void setup() {...}

void loop() {
  currentTime += 0.00001574;
  if (currentTime >= XXX) currentTime = 0; // reset at the end of the day
  // your code 
 ...
}

but this will be error prone due to the precision issue. Best would be to use only integers and represent your day in seconds.

an alternative would be to to download the GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC library and use the helper classes DateTime and TimeSpan (which represent time in seconds and do the right maths).

I see where that would be useful. I've read that for that to happen, I'll need to wait until my RTC arrives because every time I send code from the IDE to the board...millis() resets to 0 each time. At this stage of testing, I'm ok with the reset. I haven't gotten past this addition (iteration) problem yet. I simply want to add a number to the previous value...then recalculate based upon the new info. wash rinse repeat....

check answer #17

I've seen that reference in previous posts...will try your example....brb