Need help with looping my code

Hi, I need help with my code.

(this is my first project and my first post).

At the moment I got 16 lights (neopixel) lighting up with a delay, representing a minute (each light has 3.75 sec delay totalling a full minute). however, after the minute is done, the lights just stop, but i would like them to continue into the next minute and so on..

the idea is to have white lights for 8 hours (1 round x 60 x 8), then cyan lights for 2 hours, then blue for 4 and so on, until i have a full 24 hour cycle and it starts all over. any help is highly appreciated.

the code:

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif

#define PIN 6

#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500;

void setup() {

pixels.begin();
pixels.setBrightness(50);
}

void loop() {

for(int i=0;i<NUMPIXELS;i++){

// 1 Minute

pixels.setPixelColor(i, pixels.Color(255,255,255)); // WHITE

pixels.show(); //

delay(delayval); // delay for 3,75 seconds (x 16 = 1 minute)
}

}

They don't stop. You light them up in sequence and never turn them off. The second minute the lights you are turning on are already on. Figure out when you want to turn lights off and add that.

The void loop automatically loops so you can calculate the number of milliseconds in 2, 4, and 8 hours, and add more delays. (The Arduino only keeps track of time in milliseconds unless you a library or additional code, or additional hardware.)

There are a couple of issues -

Every line of code takes some time to execute and that time is added to the delay()s. If you have a long delay, a few extra milliseconds or microseconds isn't a big deal, but with short delays and/or with code executed over-and-over in a loop it will introduce a significant error and of course, the error accumulates every time through the loop.

If that's an issue you can use a millis() timer.. The millis() clock runs continuously in the background so errors don't accumulate. The millis() timer/counter hits its maximum count after about 45 days and "rolls-over" to zero. There's a way to deal with that but I can't remember what it is.

Another thing is, the built-in clock isn't very accurate. It's not as accurate as a watch or clock but it might be OK for a one-day cycle. If your code is going to run continuously day-after-day the errors WILL accumulate.

If you need longer term accuracy you can use a real-time clock ("RTC") module. These tell the time of day (and usually the date) and the are much more accurate. Or if you want to get really fancy you can use a GPS or Cell network module (or any network module) to get "perfect time" from the network.

This is not very clear. The way you have it coded you are not delaying for 3.75 seconds but rather 0.5 seconds. Also, you are turning each pixel on and leaving it on until they are all on. When you go back through the loop do you want to turn them all off and start again?

True that, but NeoPixels calls turn off interrupts, so millis() might not be as good as you'd think. Some libraries for smart LEDs attempt to compensate, it isn't clear how well that goes.

Don't ask how I know. :expressionless:

If you want any chance of remaining in sync with the clock on the wall, the inaccuracy of the microprocessor clock cannot in any way be the basis for time keeping. add a cheap and easy enough to use RTC module, a DS3231 type is very good.

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.