Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does static variable not allow loop() to run?
    text
    copied!<p>I am writing code for an lamp that mimics the sun (ie rises and sets according to the real sun).</p> <p>I am trying to use a static variable in the loop in order to keep count of the days since 1/1/12 (the last leap year (366 days)) </p> <p>for some reason, the program only works when the variable is <em>not</em> declared as static, but i need the counter to persist through the iteration of each day. </p> <p>Any help would be much appreciated. Thank you.</p> <pre><code>// Sun Object #define REDPIN 5 #define GREENPIN 6 #define BLUEPIN 3 //#define FADESPEED 480// make this higher to slow down #define FADESPEED 120 void setup() { pinMode(REDPIN, OUTPUT); pinMode(GREENPIN, OUTPUT); pinMode(BLUEPIN, OUTPUT); } void loop() { // daylengths for 4 year's worth of days starting from jan 1 of Great year (366 days) (1/1/2012) int dayLengths[] = {33540000, 33540000, 33600000, // .... // hundreds of values omitted here // .... 33360000, 33420000, 33480000, 33480000}; //2012 = 366 days + day # from jan 1st (in 2013) //starts on 0th day so subtract 1 //ready to go for march 24st @ sunrise // why does it not function when day is static????? static int day = 449; int r, g, b; long start = millis(); for (r = 0; r &lt;256; r++){ analogWrite(REDPIN, r); delay(FADESPEED); } //analogWrite(REDPIN, 255); for (g = 0; g &lt; 230; g++) { analogWrite(GREENPIN, (g*.4)); delay(FADESPEED*2); } long stopped = millis(); long elapsed = stopped - start; //approx // delays (amount of daylight - ( sunrise/sunset time ) // delay(dayLengths[day]- (97200*2)); delay(dayLengths[day]- (elapsed*2)); for (g = 230; g &gt; 0; g--) { analogWrite(GREENPIN, (g*.4)); delay(FADESPEED*2); } for (r = 256; r &gt;0; r--){ analogWrite(REDPIN, r); delay(FADESPEED); } analogWrite(REDPIN,0); //delays (millis in 1 day) - (length of daylight) delay(86400000 - dayLengths[day]); // once 4 year cycle is over, resets day to 0 if (day &lt; sizeof(dayLengths )){ day++; } else { day = 0; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload