I've just discovered a really weird property,

I’ve just discovered a really weird property, I’m not sure if it’s to do with the library, C, Arduino, doubles, or what. Basically I was trying to modify fill_rainbow to take a double delta hue so I could get a smooth transition in a circle with an arbitrary number of leds (59 in this case).

What seems to happen instead is that it starts off rotating very slowly, then starts rotating back the other way faster, then changes direction again, and every time it gains speed and takes a longer time to reach the next change. Until it gets to a flickery white.

I assume this has something to do with int or double overflowing, but still don’t know how it speeds up. There is no delay factor and the hues do not get closer together.

In any case this appears to be an interesting visual demonstration of how doing maths with limited binary precision can have unintended and unusual effects. It could potentially be exploited.

#include “FastSPI_LED2.h”

#define NUM_LEDS 59
#define CONTROL_PIN 11

struct CRGB leds[NUM_LEDS];

void setup() {
LEDS.setBrightness(64);
LEDS.addLeds<WS2812, CONTROL_PIN, GRB>(leds, NUM_LEDS);
LEDS.clear();
}

void loop() {
fill_rainbow_loop(0);
}

void fill_rainbow_loop(int idelay) {
static uint8_t startinghue = 0;
startinghue = startinghue - 1;
fill_rainbow2( leds, NUM_LEDS, startinghue, 2.0 * 256.0/double(NUM_LEDS));
LEDS.show();
if (idelay > 0) delay(idelay);
}

void fill_rainbow2( struct CRGB * pFirstLED, int numToFill,
uint8_t initialhue,
double deltahue ) {
CHSV hsv;
static double doublehue = double(initialhue);
hsv.hue = initialhue;
hsv.val = 255;
hsv.sat = 255;
for( int i = 0; i < numToFill; i++) {
hsv.hue = int(doublehue );
hsv2rgb_rainbow( hsv, pFirstLED[i]);
doublehue += deltahue;
}
}

Why “static” on doublehue? You’re setting it each time fill_rainbow2() is called.

I’m not too clear on statics. I thought it had to be, to hold its value through subsequent calls, like startinghue in fill_rainbow_loop? (I didn’t write that, it’s from the funkboxing demos)

You’re right, I misunderstood it, it’s both unnecessary and the cause of the effect!